13#include "scippp/constant_coefficient.hpp"
14#include "scippp/lin_expr.hpp"
15#include "scippp/lin_ineq.hpp"
16#include "scippp/param.hpp"
17#include "scippp/solution.hpp"
18#include "scippp/statistics.hpp"
19#include "scippp/var.hpp"
20#include "scippp/var_type.hpp"
43 bool m_weCreatedANewScipObject {
false };
45 std::vector<Var> m_vars {};
47 std::vector<SCIP_Cons*> m_cons {};
49 SCIP_Retcode m_lastReturnCode;
51 std::function<void(SCIP_Retcode)> m_scipCallWrapper;
64 explicit Model(
const std::string& name, SCIP*
scip =
nullptr,
bool includeDefaultPlugins =
true);
97 const std::string& name,
98 SCIP_Real coeff = 0.0,
100 std::optional<SCIP_Real> lb = 0.0,
101 std::optional<SCIP_Real> ub = 1.0);
116 template <
typename CoeffType = ConstantCoefficient>
118 const std::string& prefix,
122 std::optional<SCIP_Real> lb = 0.0,
123 std::optional<SCIP_Real> ub = 1.0)
125 std::vector<Var> result;
126 result.reserve(numVars);
127 for (
size_t index { 0 }; index < numVars; index++) {
128 result.push_back(
addVar(prefix + std::to_string(index), coeffs[index], varType, lb, ub));
150 template <
size_t NumVars,
typename CoeffType = ConstantCoefficient>
152 const std::string& prefix,
155 std::optional<SCIP_Real> lb = 0.0,
156 std::optional<SCIP_Real> ub = 1.0)
158 std::array<Var, NumVars> result;
159 auto vec {
addVars(prefix, NumVars, coeffs, varType, lb, ub) };
160 std::copy_n(std::make_move_iterator(vec.begin()), NumVars, result.begin());
192 [[nodiscard]] SCIP_Real
round(SCIP_Real value)
const;
200 [[nodiscard]]
bool isZero(SCIP_Real value)
const;
230 template <
typename T>
233 return statistic(m_scip);
256 [[deprecated(
"use getSolvingStatistic with statistics::PRIMALBOUND instead")]]
271 template <
typename T,
typename PT>
274 auto ptValue {
static_cast<PT
>(value) };
275 const auto* cName { parameter.
scipName.data() };
276 if constexpr (std::is_same_v<PT, int>) {
277 m_scipCallWrapper(SCIPsetIntParam(m_scip, cName, ptValue));
278 }
else if constexpr (std::is_same_v<PT, double>) {
279 m_scipCallWrapper(SCIPsetRealParam(m_scip, cName, ptValue));
280 }
else if constexpr (std::is_same_v<PT, char>) {
281 m_scipCallWrapper(SCIPsetCharParam(m_scip, cName, value));
282 }
else if constexpr (std::is_same_v<PT, bool>) {
283 m_scipCallWrapper(SCIPsetBoolParam(m_scip, cName, value));
284 }
else if constexpr (std::is_same_v<PT, SCIP_Longint>) {
285 m_scipCallWrapper(SCIPsetLongintParam(m_scip, cName, value));
286 }
else if constexpr (std::is_same_v<PT, std::string>) {
287 m_scipCallWrapper(SCIPsetStringParam(m_scip, cName, value.c_str()));
291 static_assert(
sizeof(T) == 0);
305 void writeOrigProblem(
const std::filesystem::directory_entry& filename,
bool genericNames =
false)
const;
325 [[deprecated(R
"(Use this to access the raw SCIP object.
326 That is only required for use-cases not supported by SCIP++.
327 Consider adding the feature you are using to SCIP++!)")]] [[nodiscard]] Scip*
Represents a linear inequality: lhs <= expr <= rhs.
A SCIP optimization model.
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.
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.
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.
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.
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.
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.
static constexpr ConstantCoefficient COEFF_ZERO
An object which index operator always returns 0.
VarType
Type of a variable.
@ CONTINUOUS
Continuous variable.
Wrapper for a SCIP solution.
Wrapper for a SCIP variable.
Stores the argument type and string representation of a parameter.
std::string_view scipName
Original name of the parameter in SCIP.
Storage for a function pointer.