22 #include "gurobi_c++.h"
27 vector<GRBEnv> threadEnv;
29 vector<GRBVar> generateProblem(
const ILPModel& m, GRBModel& model);
32 string identification =
"Gurobi";
33 int major = -1, minor = -1, release = -1;
34 GRBversion(&major, &minor, &release);
35 if (major != -1 && minor != -1 && release != -1)
36 identification +=
" "+to_string(major)+
"."+to_string(minor)+
"."+to_string(release);
37 return identification;
41 threadEnv.resize(numberOfThreads);
49 if (threadEnv.size() <= (uint32_t) threadId) {
50 string msg =
"Either the Gurobi environments were not initialized or the number of concurrent threads\n";
51 msg +=
"was specified incorrectly in the initializeLocalEnvironments method!";
55 threadEnv[threadId].set(GRB_IntParam_Threads, numberOfThreads);
57 threadEnv[threadId].set(GRB_DoubleParam_TimeLimit, timeLimit);
59 threadEnv[threadId].set(GRB_DoubleParam_MIPGap, gap);
62 threadEnv[threadId].set(GRB_IntParam_OutputFlag, 1);
63 threadEnv[threadId].set(GRB_IntParam_LogToConsole, 1);
65 threadEnv[threadId].set(GRB_IntParam_OutputFlag, 0);
66 threadEnv[threadId].set(GRB_IntParam_LogToConsole, 0);
69 GRBModel model(threadEnv[threadId]);
70 const vector<GRBVar>& vars = generateProblem(m, model);
73 switch (model.get(GRB_IntAttr_Status)) {
81 if (model.get(GRB_IntAttr_SolCount) > 0)
96 if (s.
status == ILP_OPTIMAL || s.
status == ILP_FEASIBLE) {
97 s.
criterion = model.get(GRB_DoubleAttr_ObjVal);
98 for (
long v = 0; v < model.get(GRB_IntAttr_NumVars); ++v)
99 s.
solution.push_back(vars[v].get(GRB_DoubleAttr_X));
102 if (model.get(GRB_IntAttr_IsMIP))
103 s.
bound = model.get(GRB_DoubleAttr_ObjBound);
104 else if (s.
status == ILP_OPTIMAL)
107 }
catch (GRBException& e) {
110 throw_with_nested(
ILPSolverException(caller(),
"Error during solving the ILP problem!"));
116 vector<GRBVar> generateProblem(
const ILPModel& m, GRBModel& model) {
118 model.set(GRB_IntAttr_ModelSense, (m.
obj == MINIMIZE ? GRB_MINIMIZE : GRB_MAXIMIZE));
119 for (
unsigned long v = 0; v < m.numberOfVariables(); ++v) {
121 switch (m.
x[v].type) {
123 var = model.addVar(m.
x[v].lowerBound, m.
x[v].upperBound, m.
c[v], GRB_CONTINUOUS, m.
varDesc[v]);
126 var = model.addVar(m.
x[v].lowerBound, m.
x[v].upperBound, m.
c[v], GRB_BINARY, m.
varDesc[v]);
129 var = model.addVar(m.
x[v].lowerBound, m.
x[v].upperBound, m.
c[v], GRB_INTEGER, m.
varDesc[v]);
137 assert(m.numberOfVariables() == m.
gurobiC.size() &&
"Invalid initialization of gurobi criterion functions!");
138 for (
unsigned long v = 0; v < m.numberOfVariables(); ++v) {
139 const tuple<vector<double>, vector<double>>* cf = m.
gurobiC[v];
141 const vector<double>& x = get<0>(*cf), & y = get<1>(*cf);
142 assert(x.size() == y.size() &&
"Invalid initialization of Gurobi functions!");
143 if (!x.empty() && !y.empty()) {
145 model.setPWLObj(vars[v], x.size(), (
double*) x.data(), (
double*) y.data());
153 for (
unsigned long c = 0; c < m.numberOfConstraints(); ++c) {
155 for (
const pair<uint32_t, double>& p : m.
A[c])
156 expr += p.second*vars[p.first];
160 model.addConstr(expr <= m.
b[c], m.
conDesc[c]);
163 model.addConstr(expr == m.
b[c], m.
conDesc[c]);
166 model.addConstr(expr >= m.
b[c], m.
conDesc[c]);
void initializeLocalEnvironments(int numberOfThreads)
It enables the solver to initialize all the data structures (e.g. expensive to construct) required fo...
std::vector< double > b
Constants in the constraints, i.e. the right-hand side vector of .
SparseMatrix< double > A
Constraint matrix of the problem.
Integer Linear Programming problem is stored in this data structure.
std::vector< double > solution
The best found solution.
Structure storing a solution of an Integer Linear Programming problem.
std::vector< std::string > varDesc
Optional description of the variables.
std::vector< const std::tuple< std::vector< double >, std::vector< double > > * > gurobiC
Convex functions in the criterion in the format suitable for Gurobi solver.
Solver-independent interface for solving Integer Linear Programming problems.
Exception dedicated to problems with Integer Linear Programming solvers.
double criterion
The criterion value of the solution.
std::vector< Operator > ops
Operators of the constraints, see Operator enum.
std::string solverIdentification()
Returns an identification of the used solver, e.g. 'Gurobi 6.0.4'.
double bound
The best known lower or upper bound.
std::vector< double > c
Vector of the criterion coefficients.
Objective obj
Sense of the objective function (minimization/maximization).
Status status
Solution status, see Status enum.
std::vector< Variable > x
Variables of the problem.
SolutionILP solveILP(const ILPModel &m, bool verbose, double gap=0.0, double timeLimit=0.0, int numberOfThreads=1, int threadId=0)
Integer Linear Programming solver is called to solve the problem and the solution is returned...
std::vector< std::string > conDesc
Optional description of the constraints.