21 #include <ilcplex/ilocplex.h>
28 void generateProblem(
const ILPModel& m, IloModel& model, IloNumVarArray& x, IloRangeArray& con);
33 string identification =
"Cplex "+string(cplex.getVersion());
36 return identification;
46 IloObjective obj(env);
47 IloNumVarArray var(env);
48 IloRangeArray con(env);
49 generateProblem(m, model, var, con);
51 IloCplex cplex(model);
52 cplex.setParam(IloCplex::Threads, numberOfThreads);
54 cplex.setOut(env.getNullStream());
56 cplex.setParam(IloCplex::EpGap, gap);
58 cplex.setParam(IloCplex::TiLim, timeLimit);
61 switch (cplex.getStatus()) {
62 case IloAlgorithm::Optimal:
63 solution.
status = ILP_OPTIMAL;
65 case IloAlgorithm::Feasible:
66 solution.
status = ILP_FEASIBLE;
68 case IloAlgorithm::Infeasible:
69 solution.
status = ILP_INFEASIBLE;
71 case IloAlgorithm::Unbounded:
72 solution.
status = ILP_UNBOUNDED;
75 solution.
status = ILP_UNKNOWN;
78 if (solution.
status == ILP_OPTIMAL || solution.
status == ILP_FEASIBLE) {
80 cplex.getValues(sol, var);
82 for (
long v = 0; v < sol.getSize(); ++v)
86 solution.
bound = cplex.getBestObjValue();
89 }
catch (IloException& e) {
94 throw_with_nested(
ILPSolverException(caller(),
"Error during solving the ILP problem!"));
100 void generateProblem(
const ILPModel& m, IloModel& model, IloNumVarArray& x, IloRangeArray& con) {
101 IloEnv env = model.getEnv();
102 IloObjective obj = (m.
obj == MINIMIZE ? IloMinimize(env) : IloMaximize(env));
103 for (
unsigned long v = 0; v < m.numberOfVariables(); ++v) {
104 switch (m.
x[v].type) {
106 x.add(IloNumVar(env, m.
x[v].lowerBound, m.
x[v].upperBound, IloNumVar::Float));
109 x.add(IloNumVar(env, m.
x[v].lowerBound, m.
x[v].upperBound, IloNumVar::Bool));
112 x.add(IloNumVar(env, m.
x[v].lowerBound, m.
x[v].upperBound, IloNumVar::Int));
115 obj.setLinearCoef(x[v], m.
c[v]);
116 x[v].setName(m.
varDesc[v].c_str());
119 for (
unsigned long c = 0; c < m.numberOfConstraints(); ++c) {
122 con.add(IloRange(env, -IloInfinity, m.
b[c]));
125 con.add(IloRange(env, m.
b[c], m.
b[c]));
128 con.add(IloRange(env, m.
b[c], IloInfinity));
131 for (
const pair<uint32_t, double>& p : m.
A[c])
132 con[c].setLinearCoef(x[p.first], p.second);
134 con[c].setName(m.
conDesc[c].c_str());
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.
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.