SCIP++ refs/heads/main
 
Loading...
Searching...
No Matches
model.hpp
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <filesystem>
6#include <functional>
7#include <optional>
8#include <scip/scip.h>
9#include <string>
10#include <type_traits>
11#include <vector>
12
13#include "scippp/constant_coefficient.hpp"
14#include "scippp/iis.hpp"
15#include "scippp/initial_solution.hpp"
16#include "scippp/lin_expr.hpp"
17#include "scippp/lin_ineq.hpp"
18#include "scippp/param.hpp"
19#include "scippp/solution.hpp"
20#include "scippp/statistics.hpp"
21#include "scippp/var.hpp"
22#include "scippp/var_type.hpp"
23
25namespace scippp {
26
28enum class Sense {
30 MAXIMIZE = SCIP_OBJSENSE_MAXIMIZE,
32 MINIMIZE = SCIP_OBJSENSE_MINIMIZE
33};
34
41class Model {
43 Scip* m_scip;
45 bool m_weCreatedANewScipObject { false };
47 std::vector<Var> m_vars {};
49 std::vector<SCIP_Cons*> m_cons {};
51 mutable SCIP_Retcode m_lastReturnCode;
53 std::function<void(SCIP_Retcode)> m_scipCallWrapper;
54
55public:
66 explicit Model(const std::string& name, SCIP* scip = nullptr, bool includeDefaultPlugins = true);
67
73
79 [[nodiscard]] SCIP_Retcode getLastReturnCode() const;
80
86 void setScipCallWrapper(std::function<void(SCIP_Retcode)> wrapper);
87
99 const std::string& name,
100 SCIP_Real coeff = 0.0,
102 std::optional<SCIP_Real> lb = 0.0,
103 std::optional<SCIP_Real> ub = 1.0);
104
118 template <typename CoeffType = ConstantCoefficient>
119 std::vector<Var> addVars(
120 const std::string& prefix,
121 size_t numVars,
122 const CoeffType& coeffs = COEFF_ZERO,
124 std::optional<SCIP_Real> lb = 0.0,
125 std::optional<SCIP_Real> ub = 1.0)
126 {
127 std::vector<Var> result;
128 result.reserve(numVars);
129 for (size_t index { 0 }; index < numVars; index++) {
130 result.push_back(addVar(prefix + std::to_string(index), coeffs[index], varType, lb, ub));
131 }
132 return result;
133 }
134
152 template <size_t NumVars, typename CoeffType = ConstantCoefficient>
153 std::array<Var, NumVars> addVars(
154 const std::string& prefix,
155 const CoeffType& coeffs = COEFF_ZERO,
157 std::optional<SCIP_Real> lb = 0.0,
158 std::optional<SCIP_Real> ub = 1.0)
159 {
160 std::array<Var, NumVars> result;
161 auto vec { addVars(prefix, NumVars, coeffs, varType, lb, ub) };
162 std::copy_n(std::make_move_iterator(vec.begin()), NumVars, result.begin());
163 return result;
164 }
165
172 void addConstr(const LinIneq& ineq, const std::string& name);
173
179 [[nodiscard]] SCIP_Real infinity() const;
180
186 [[nodiscard]] SCIP_Real epsilon() const;
187
194 [[nodiscard]] SCIP_Real round(SCIP_Real value) const;
195
202 [[nodiscard]] bool isZero(SCIP_Real value) const;
203
208 void solve() const;
209
215 void setObjsense(Sense objsense) const;
216
222 [[nodiscard]] SCIP_Status getStatus() const;
223
232 template <typename T>
233 [[nodiscard]] T getSolvingStatistic(const statistics::Statistic<T>& statistic) const
234 {
235 return statistic(m_scip);
236 }
237
243 [[nodiscard]] int getNSols() const;
244
250 [[nodiscard]] Solution getBestSol() const;
251
258 [[deprecated("use getSolvingStatistic with statistics::PRIMALBOUND instead")]] //
259 [[nodiscard]] double
261
273 template <typename T, typename PT>
274 void setParam(params::Param<PT> parameter, T value) const
275 {
276 auto ptValue { static_cast<PT>(value) };
277 const auto* cName { parameter.scipName.data() };
278 if constexpr (std::is_same_v<PT, int>) {
279 m_scipCallWrapper(SCIPsetIntParam(m_scip, cName, ptValue));
280 } else if constexpr (std::is_same_v<PT, double>) {
281 m_scipCallWrapper(SCIPsetRealParam(m_scip, cName, ptValue));
282 } else if constexpr (std::is_same_v<PT, char>) {
283 m_scipCallWrapper(SCIPsetCharParam(m_scip, cName, value));
284 } else if constexpr (std::is_same_v<PT, bool>) {
285 m_scipCallWrapper(SCIPsetBoolParam(m_scip, cName, value));
286 } else if constexpr (std::is_same_v<PT, SCIP_Longint>) {
287 m_scipCallWrapper(SCIPsetLongintParam(m_scip, cName, value));
288 } else if constexpr (std::is_same_v<PT, std::string>) {
289 m_scipCallWrapper(SCIPsetStringParam(m_scip, cName, value.c_str()));
290 } else {
291 // make this not compile
292 // https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2593r0.html#workarounds
293 static_assert(sizeof(T) == 0);
294 }
295 }
296
307 void writeOrigProblem(const std::filesystem::directory_entry& filename, bool genericNames = false) const;
308
317 void writeOrigProblem(const std::string& extension, bool genericNames = false) const;
318
327 [[deprecated(R"(Use this to access the raw SCIP object.
328 That is only required for use-cases not supported by SCIP++.
329 Consider adding the feature you are using to SCIP++!)")]] [[nodiscard]] Scip*
330 scip() const;
331
345 const InitialSolution& initialSolution,
346 bool printReason = true,
347 bool completely = true,
348 bool checkBounds = true,
349 bool checkIntegrality = true,
350 bool checkLpRows = true) const;
351
358 [[nodiscard]] IIS generateIIS() const;
359};
360}
A primal solution to be added to SCIP's solution pool.
Represents a linear inequality: lhs <= expr <= rhs.
Definition lin_ineq.hpp:16
A SCIP optimization model.
Definition model.hpp:41
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)
Adds multiple variables to the model.
Definition model.hpp:119
void writeOrigProblem(const std::filesystem::directory_entry &filename, bool genericNames=false) const
Writes original problem to file.
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)
Adds multiple variables to the model.
Definition model.hpp:153
Scip * scip() const
Returns a pointer to the underlying SCIP object.
Model(const std::string &name, SCIP *scip=nullptr, bool includeDefaultPlugins=true)
Creates an empty problem and sets the optimization goal to Sense::MINIMIZE.
void setScipCallWrapper(std::function< void(SCIP_Retcode)> wrapper)
Replace the current wrapper for every call to SCIP's C API.
void solve() const
Solve the model.
void setParam(params::Param< PT > parameter, T value) const
Sets a parameter.
Definition model.hpp:274
void writeOrigProblem(const std::string &extension, bool genericNames=false) const
Writes original problem to standard output.
SCIP_Retcode getLastReturnCode() const
Gets the return code of the last call to SCIP's C API when the default call wrapper is used.
T getSolvingStatistic(const statistics::Statistic< T > &statistic) const
Query statistics about the solving process.
Definition model.hpp:233
SCIP_Real infinity() const
Infinity according the SCIP config.
void setObjsense(Sense objsense) const
Set objective goal.
bool isZero(SCIP_Real value) const
Checks, if value is in range epsilon of 0.0.
void addConstr(const LinIneq &ineq, const std::string &name)
Adds a constraint to the model.
SCIP_Real epsilon() const
Value treated as zero.
~Model()
Releases the variables and constraints.
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 a variable to the model.
bool addSolution(const InitialSolution &initialSolution, bool printReason=true, bool completely=true, bool checkBounds=true, bool checkIntegrality=true, bool checkLpRows=true) const
Adds a solution to SCIP's solution pool.
IIS generateIIS() const
Creates an Irreducible Infeasible Subsystem (IIS) of the current model.
SCIP_Status getStatus() const
Returns the solution status.
double getPrimalbound() const
Returns the objective value of best solution.
int getNSols() const
Returns the number of feasible primal solutions stored in the solution storage.
SCIP_Real round(SCIP_Real value) const
Rounds value to the nearest integer with epsilon tolerance.
Solution getBestSol() const
Returns the best feasible primal solution found so far or best solution candidate.
C++ wrapper for SCIP.
Sense
Optimization goal.
Definition model.hpp:28
@ MINIMIZE
Minimize.
@ MAXIMIZE
Maximize.
static constexpr ConstantCoefficient COEFF_ZERO
An object which index operator always returns 0.
VarType
Type of a variable.
Definition var_type.hpp:11
@ CONTINUOUS
Continuous variable.
A data structure to hold an IIS (Irreducible Infeasible Subsystem).
Definition iis.hpp:12
Wrapper for a SCIP solution.
Definition solution.hpp:13
Wrapper for a SCIP variable.
Definition var.hpp:17
Stores the argument type and string representation of a parameter.
Definition param.hpp:14
std::string_view scipName
Original name of the parameter in SCIP.
Definition param.hpp:16
Storage for a function pointer.