-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodel.hpp
More file actions
360 lines (330 loc) · 12.5 KB
/
model.hpp
File metadata and controls
360 lines (330 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#pragma once
#include <algorithm>
#include <array>
#include <filesystem>
#include <functional>
#include <optional>
#include <scip/scip.h>
#include <string>
#include <type_traits>
#include <vector>
#include "scippp/constant_coefficient.hpp"
#include "scippp/iis.hpp"
#include "scippp/initial_solution.hpp"
#include "scippp/lin_expr.hpp"
#include "scippp/lin_ineq.hpp"
#include "scippp/param.hpp"
#include "scippp/solution.hpp"
#include "scippp/statistics.hpp"
#include "scippp/var.hpp"
#include "scippp/var_type.hpp"
//! C++ wrapper for %SCIP.
namespace scippp {
//! Optimization goal.
enum class Sense {
//! Maximize
MAXIMIZE = SCIP_OBJSENSE_MAXIMIZE,
//! Minimize
MINIMIZE = SCIP_OBJSENSE_MINIMIZE
};
/**
* A %SCIP optimization model.
*
* Variables and constraints are automatically released when the model is destructed.
* @since 1.0.0
*/
class Model {
//! Pointer to the underlying %SCIP object.
Scip* m_scip;
//! \c true if %SCIPcreate was called in the c'tor and %SCIPfree has to be called in the d'tor.
bool m_weCreatedANewScipObject { false };
//! Variables.
std::vector<Var> m_vars {};
//! Constraints.
std::vector<SCIP_Cons*> m_cons {};
//! Stores the return of the last %SCIP call when the default call wrapper is used.
mutable SCIP_Retcode m_lastReturnCode;
//! Wrapper for every call to %SCIP's %C %API.
std::function<void(SCIP_Retcode)> m_scipCallWrapper;
public:
/**
* Creates an empty problem and sets the optimization goal to Sense::MINIMIZE.
*
* By default, all calls to the underlying %C %API are wrapped and the last return code is stored.
*
* @since 1.0.0
* @param name for the problem.
* @param scip to create the problem in. If \c nullptr, a new %SCIP data structure will be created.
* @param includeDefaultPlugins if \c true, the default plugins are added to \p scip.
*/
explicit Model(const std::string& name, SCIP* scip = nullptr, bool includeDefaultPlugins = true);
/**
* Releases the variables and constraints.
* @since 1.0.0
*/
~Model();
/**
* Gets the return code of the last call to %SCIP's %C %API when the default call wrapper is used.
* @since 1.0.0
* @return return code of the last call to %SCIP's %C %API.
*/
[[nodiscard]] SCIP_Retcode getLastReturnCode() const;
/**
* Replace the current wrapper for every call to %SCIP's %C %API.
* @since 1.0.0
* @param wrapper New wrapper tp use.
*/
void setScipCallWrapper(std::function<void(SCIP_Retcode)> wrapper);
/**
* Adds a variable to the model.
* @since 1.0.0
* @param name of the variable when the model is written.
* @param coeff Coefficient in the objective function.
* @param varType variable type.
* @param lb lower bound. \c std::nullopt is interpreted as -infinity.
* @param ub upper bound. \c std::nullopt is interpreted as infinity.
* @return Reference to the newly created variable.
*/
Var& addVar(
const std::string& name,
SCIP_Real coeff = 0.0,
VarType varType = VarType::CONTINUOUS,
std::optional<SCIP_Real> lb = 0.0,
std::optional<SCIP_Real> ub = 1.0);
/**
* Adds multiple variables to the model.
* @since 1.0.0
* @tparam CoeffType Type of the object holding the coefficients. They are accessed via \c [i] where i goes from 0
* to \p numVars - 1.
* @param prefix to construct variable names from: prefix + index.
* @param numVars number of variables to create.
* @param coeffs Object holding the coefficients for the objective function.
* @param varType variable type.
* @param lb lower bound. \c std::nullopt is interpreted as -infinity.
* @param ub upper bound. \c std::nullopt is interpreted as infinity.
* @return Vector of variables.
*/
template <typename CoeffType = ConstantCoefficient>
std::vector<Var> addVars(
const std::string& prefix,
size_t numVars,
const CoeffType& coeffs = COEFF_ZERO,
VarType varType = VarType::CONTINUOUS,
std::optional<SCIP_Real> lb = 0.0,
std::optional<SCIP_Real> ub = 1.0)
{
std::vector<Var> result;
result.reserve(numVars);
for (size_t index { 0 }; index < numVars; index++) {
result.push_back(addVar(prefix + std::to_string(index), coeffs[index], varType, lb, ub));
}
return result;
}
/**
* Adds multiple variables to the model.
*
* This method can be used when the number of variables to add is known at compile time. The result can be used in a
* structured binding.
*
* @since 1.0.0
* @tparam NumVars Number of variables to add.
* @tparam CoeffType Type of the object holding the coefficients. They are accessed via \c [i] where i goes from 0
* to \p NumVars - 1.
* @param prefix to construct variable names from: prefix + index.
* @param coeffs Object holding the coefficients for the objective function.
* @param varType variable type.
* @param lb lower bound.
* @param ub upper bound.
* @return Array of variables.
*/
template <size_t NumVars, typename CoeffType = ConstantCoefficient>
std::array<Var, NumVars> addVars(
const std::string& prefix,
const CoeffType& coeffs = COEFF_ZERO,
VarType varType = VarType::CONTINUOUS,
std::optional<SCIP_Real> lb = 0.0,
std::optional<SCIP_Real> ub = 1.0)
{
std::array<Var, NumVars> result;
auto vec { addVars(prefix, NumVars, coeffs, varType, lb, ub) };
std::copy_n(std::make_move_iterator(vec.begin()), NumVars, result.begin());
return result;
}
/**
* Adds a constraint to the model.
* @since 1.0.0
* @param ineq linear inequality to add.
* @param name for the constraint when the model is written.
*/
void addConstr(const LinIneq& ineq, const std::string& name);
/**
* Infinity according the %SCIP config. To be used in variable bounds and constants in constraints.
* @since 1.0.0
* @return infinity according the %SCIP config.
*/
[[nodiscard]] SCIP_Real infinity() const;
/**
* Value treated as zero.
* @since 1.1.0
* @return value treated as zero.
*/
[[nodiscard]] SCIP_Real epsilon() const;
/**
* Rounds value to the nearest integer with epsilon tolerance.
* @since 1.1.0
* @param value to round
* @return nearest integer as double
*/
[[nodiscard]] SCIP_Real round(SCIP_Real value) const;
/**
* Checks, if value is in range epsilon of 0.0.
* @since 1.1.0
* @param value to check
* @return \c true iff the value is in range epsilon of 0.0.
*/
[[nodiscard]] bool isZero(SCIP_Real value) const;
/**
* Solve the model.
* @since 1.0.0
*/
void solve() const;
/**
* Set objective goal.
* @since 1.0.0
* @param objsense Minimize or Maximize.
*/
void setObjsense(Sense objsense) const;
/**
* Returns the solution status.
* @since 1.0.0
* @return solution status.
*/
[[nodiscard]] SCIP_Status getStatus() const;
/**
* Query statistics about the solving process.
*
* @tparam T Type of the statistics value.
* @since 1.2.0
* @param statistic Statistics value to access.
* @return Statistics value.
*/
template <typename T>
[[nodiscard]] T getSolvingStatistic(const statistics::Statistic<T>& statistic) const
{
return statistic(m_scip);
}
/**
* Returns the number of feasible primal solutions stored in the solution storage.
* @since 1.0.0
* @return number of solutions.
*/
[[nodiscard]] int getNSols() const;
/**
* Returns the best feasible primal solution found so far or best solution candidate.
* @since 1.0.0
* @return best feasible primal solution.
*/
[[nodiscard]] Solution getBestSol() const;
/**
* Returns the objective value of best solution.
* @since 1.0.0
* @deprecated since 1.2.0, use getSolvingStatistic with statistics::PRIMALBOUND instead.
* @return objective value of best solution.
*/
[[deprecated("use getSolvingStatistic with statistics::PRIMALBOUND instead")]] //
[[nodiscard]] double
getPrimalbound() const;
/**
* Sets a parameter.
*
* See the namespace scippp::params for a list of parameters, or create new ones using params::Param.
*
* @since 1.0.0
* @tparam T Type of the value.
* @tparam PT Value type the parameter expects
* @param parameter to set.
* @param value to set the parameter to.
*/
template <typename T, typename PT>
void setParam(params::Param<PT> parameter, T value) const
{
auto ptValue { static_cast<PT>(value) };
const auto* cName { parameter.scipName.data() };
if constexpr (std::is_same_v<PT, int>) {
m_scipCallWrapper(SCIPsetIntParam(m_scip, cName, ptValue));
} else if constexpr (std::is_same_v<PT, double>) {
m_scipCallWrapper(SCIPsetRealParam(m_scip, cName, ptValue));
} else if constexpr (std::is_same_v<PT, char>) {
m_scipCallWrapper(SCIPsetCharParam(m_scip, cName, value));
} else if constexpr (std::is_same_v<PT, bool>) {
m_scipCallWrapper(SCIPsetBoolParam(m_scip, cName, value));
} else if constexpr (std::is_same_v<PT, SCIP_Longint>) {
m_scipCallWrapper(SCIPsetLongintParam(m_scip, cName, value));
} else if constexpr (std::is_same_v<PT, std::string>) {
m_scipCallWrapper(SCIPsetStringParam(m_scip, cName, value.c_str()));
} else {
// make this not compile
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2593r0.html#workarounds
static_assert(sizeof(T) == 0);
}
}
/**
* Writes original problem to file.
*
* @since 1.1.0
* @param filename output file name including extension
* @param genericNames using generic variable (x0, x1, ...) and constraint names (c0, c1, ...) instead of the
* user-given names?
* @attention Do not use an std::string or std::filesystem::path as argument \p filename,
* as this will call the other overload instead!
*/
void writeOrigProblem(const std::filesystem::directory_entry& filename, bool genericNames = false) const;
/**
* Writes original problem to standard output.
*
* @since 1.1.0
* @param extension file extension to derive the output format from
* @param genericNames using generic variable (x0, x1, ...) and constraint names (c0, c1, ...) instead of the
* user-given names?
*/
void writeOrigProblem(const std::string& extension, bool genericNames = false) const;
/**
* Returns a pointer to the underlying %SCIP object.
*
* @since 1.0.0
* @attention Use this to access the raw SCIP object. That is required only for use-cases not supported by SCIP++.
* Consider adding the feature you are using to SCIP++!
* @return the underlying %SCIP object.
*/
[[deprecated(R"(Use this to access the raw SCIP object.
That is only required for use-cases not supported by SCIP++.
Consider adding the feature you are using to SCIP++!)")]] [[nodiscard]] Scip*
scip() const;
/**
* Adds a solution to %SCIP's solution pool.
*
* @since 1.3.0
* @param initialSolution to add to the solution pool.
* @param printReason Should all reasons of violations be printed?
* @param completely Should all violations be checked if \p printReason is true?
* @param checkBounds Should the bounds of the variables be checked?
* @param checkIntegrality Should integrality be checked?
* @param checkLpRows Do constraints represented by rows in the current LP have to be checked?
* @return \c true iff the solution was feasible and stored in the solution storage (i.e, good enough to keep).
*/
bool addSolution(
const InitialSolution& initialSolution,
bool printReason = true,
bool completely = true,
bool checkBounds = true,
bool checkIntegrality = true,
bool checkLpRows = true) const;
/**
* Creates an %Irreducible %Infeasible %Subsystem (%IIS) of the current model.
*
* @since 1.4.0
* @return The generated %IIS.
*/
[[nodiscard]] IIS generateIIS() const;
};
}