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/initial_solution.hpp"
15#include "scippp/lin_expr.hpp"
16#include "scippp/lin_ineq.hpp"
17#include "scippp/param.hpp"
18#include "scippp/solution.hpp"
19#include "scippp/statistics.hpp"
20#include "scippp/var.hpp"
21#include "scippp/var_type.hpp"
22
24namespace scippp {
25
27enum class Sense {
29 MAXIMIZE = SCIP_OBJSENSE_MAXIMIZE,
31 MINIMIZE = SCIP_OBJSENSE_MINIMIZE
32};
33
40class Model {
42 Scip* m_scip;
44 bool m_weCreatedANewScipObject { false };
46 std::vector<Var> m_vars {};
48 std::vector<SCIP_Cons*> m_cons {};
50 SCIP_Retcode m_lastReturnCode;
52 std::function<void(SCIP_Retcode)> m_scipCallWrapper;
53
54public:
65 explicit Model(const std::string& name, SCIP* scip = nullptr, bool includeDefaultPlugins = true);
66
72
78 [[nodiscard]] SCIP_Retcode getLastReturnCode() const;
79
85 void setScipCallWrapper(std::function<void(SCIP_Retcode)> wrapper);
86
98 const std::string& name,
99 SCIP_Real coeff = 0.0,
101 std::optional<SCIP_Real> lb = 0.0,
102 std::optional<SCIP_Real> ub = 1.0);
103
117 template <typename CoeffType = ConstantCoefficient>
118 std::vector<Var> addVars(
119 const std::string& prefix,
120 size_t numVars,
121 const CoeffType& coeffs = COEFF_ZERO,
123 std::optional<SCIP_Real> lb = 0.0,
124 std::optional<SCIP_Real> ub = 1.0)
125 {
126 std::vector<Var> result;
127 result.reserve(numVars);
128 for (size_t index { 0 }; index < numVars; index++) {
129 result.push_back(addVar(prefix + std::to_string(index), coeffs[index], varType, lb, ub));
130 }
131 return result;
132 }
133
151 template <size_t NumVars, typename CoeffType = ConstantCoefficient>
152 std::array<Var, NumVars> addVars(
153 const std::string& prefix,
154 const CoeffType& coeffs = COEFF_ZERO,
156 std::optional<SCIP_Real> lb = 0.0,
157 std::optional<SCIP_Real> ub = 1.0)
158 {
159 std::array<Var, NumVars> result;
160 auto vec { addVars(prefix, NumVars, coeffs, varType, lb, ub) };
161 std::copy_n(std::make_move_iterator(vec.begin()), NumVars, result.begin());
162 return result;
163 }
164
171 void addConstr(const LinIneq& ineq, const std::string& name);
172
178 [[nodiscard]] SCIP_Real infinity() const;
179
185 [[nodiscard]] SCIP_Real epsilon() const;
186
193 [[nodiscard]] SCIP_Real round(SCIP_Real value) const;
194
201 [[nodiscard]] bool isZero(SCIP_Real value) const;
202
207 void solve();
208
214 void setObjsense(Sense objsense);
215
221 [[nodiscard]] SCIP_Status getStatus() const;
222
231 template <typename T>
232 [[nodiscard]] T getSolvingStatistic(const statistics::Statistic<T>& statistic) const
233 {
234 return statistic(m_scip);
235 }
236
242 [[nodiscard]] int getNSols() const;
243
249 [[nodiscard]] Solution getBestSol() const;
250
257 [[deprecated("use getSolvingStatistic with statistics::PRIMALBOUND instead")]] //
258 [[nodiscard]] double
260
272 template <typename T, typename PT>
273 void setParam(params::Param<PT> parameter, T value)
274 {
275 auto ptValue { static_cast<PT>(value) };
276 const auto* cName { parameter.scipName.data() };
277 if constexpr (std::is_same_v<PT, int>) {
278 m_scipCallWrapper(SCIPsetIntParam(m_scip, cName, ptValue));
279 } else if constexpr (std::is_same_v<PT, double>) {
280 m_scipCallWrapper(SCIPsetRealParam(m_scip, cName, ptValue));
281 } else if constexpr (std::is_same_v<PT, char>) {
282 m_scipCallWrapper(SCIPsetCharParam(m_scip, cName, value));
283 } else if constexpr (std::is_same_v<PT, bool>) {
284 m_scipCallWrapper(SCIPsetBoolParam(m_scip, cName, value));
285 } else if constexpr (std::is_same_v<PT, SCIP_Longint>) {
286 m_scipCallWrapper(SCIPsetLongintParam(m_scip, cName, value));
287 } else if constexpr (std::is_same_v<PT, std::string>) {
288 m_scipCallWrapper(SCIPsetStringParam(m_scip, cName, value.c_str()));
289 } else {
290 // make this not compile
291 // https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2593r0.html#workarounds
292 static_assert(sizeof(T) == 0);
293 }
294 }
295
306 void writeOrigProblem(const std::filesystem::directory_entry& filename, bool genericNames = false) const;
307
316 void writeOrigProblem(const std::string& extension, bool genericNames = false) const;
317
326 [[deprecated(R"(Use this to access the raw SCIP object.
327 That is only required for use-cases not supported by SCIP++.
328 Consider adding the feature you are using to SCIP++!)")]] [[nodiscard]] Scip*
329 scip() const;
330
344 const InitialSolution& initialSolution,
345 bool printReason = true,
346 bool completely = true,
347 bool checkBounds = true,
348 bool checkIntegrality = true,
349 bool checkLpRows = true);
350};
351}
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:40
void setObjsense(Sense objsense)
Set objective goal.
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:118
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:152
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 writeOrigProblem(const std::string &extension, bool genericNames=false) const
Writes original problem to standard output.
bool addSolution(const InitialSolution &initialSolution, bool printReason=true, bool completely=true, bool checkBounds=true, bool checkIntegrality=true, bool checkLpRows=true)
Adds a solution to SCIP's solution pool.
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:232
SCIP_Real infinity() const
Infinity according the SCIP config.
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.
void setParam(params::Param< PT > parameter, T value)
Sets a parameter.
Definition model.hpp:273
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.
void solve()
Solve the model.
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:27
@ 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.
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.