1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
// Copyright (c) 2018 Liangliang Nan. All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/Solver_interface/include/CGAL/SCIP_mixed_integer_program_traits.h $
// $Id: include/CGAL/SCIP_mixed_integer_program_traits.h b26b07a1242 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Liangliang Nan
#ifndef CGAL_SCIP_MIXED_INTEGER_PROGRAM_TRAITS_H
#define CGAL_SCIP_MIXED_INTEGER_PROGRAM_TRAITS_H
#include <CGAL/Mixed_integer_program_traits.h>
#if defined(CGAL_USE_SCIP) || defined(DOXYGEN_RUNNING)
#include "scip/scip.h"
#include "scip/scipdefplugins.h"
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
namespace CGAL {
#if (_MSC_VER == 1500)
#undef SCIP_CALL(x)
#define SCIP_CALL(x) (x)
#endif
/// \ingroup PkgSolverInterfaceMIP
///
/// This class provides an interface for formulating and solving
/// constrained or unconstrained mixed integer programs using
/// \ref thirdpartySCIP (which must be available on the system).
///
/// \cgalModels{MixedIntegerProgramTraits}
///
/// \sa `GLPK_mixed_integer_program_traits`
template <typename FT>
class SCIP_mixed_integer_program_traits
: public Mixed_integer_program_traits<FT>
{
/// \cond SKIP_IN_MANUAL
public:
typedef CGAL::Mixed_integer_program_traits<FT> Base_class;
typedef typename Base_class::Variable Variable;
typedef typename Base_class::Linear_constraint Linear_constraint;
typedef typename Base_class::Linear_objective Linear_objective;
typedef typename Linear_objective::Sense Sense;
typedef typename Variable::Variable_type Variable_type;
public:
/// Solves the program. Returns `false` if fails.
virtual bool solve()
{
Base_class::error_message_.clear();
Scip* scip = 0;
SCIP_CALL(SCIPcreate(&scip));
SCIP_CALL(SCIPincludeDefaultPlugins(scip));
// Disables scip output to stdout
SCIPmessagehdlrSetQuiet(SCIPgetMessagehdlr(scip), TRUE);
// Uses wall clock time because getting CPU user seconds
// involves calling times() which is very expensive
SCIP_CALL(SCIPsetIntParam(scip, "timing/clocktype", SCIP_CLOCKTYPE_WALL));
// Creates empty problem
SCIP_CALL(SCIPcreateProbBasic(scip, "Solver_interface"));
// Creates variables
std::vector<SCIP_VAR*> scip_variables;
for (std::size_t i = 0; i < Base_class::variables_.size(); ++i) {
const Variable* var = Base_class::variables_[i];
SCIP_VAR* v = 0;
double lb, ub;
var->get_bounds(lb, ub);
switch (var->variable_type())
{
case Variable::CONTINUOUS:
SCIP_CALL(SCIPcreateVar(scip, &v, var->name().c_str(), lb, ub, 0.0, SCIP_VARTYPE_CONTINUOUS, TRUE, FALSE, 0, 0, 0, 0, 0));
break;
case Variable::INTEGER:
SCIP_CALL(SCIPcreateVar(scip, &v, var->name().c_str(), lb, ub, 0.0, SCIP_VARTYPE_INTEGER, TRUE, FALSE, 0, 0, 0, 0, 0));
break;
case Variable::BINARY:
SCIP_CALL(SCIPcreateVar(scip, &v, var->name().c_str(), 0, 1, 0.0, SCIP_VARTYPE_BINARY, TRUE, FALSE, 0, 0, 0, 0, 0));
break;
}
// Adds the SCIP_VAR object to the scip problem
SCIP_CALL(SCIPaddVar(scip, v));
// Stores the SCIP_VAR pointer for later access
scip_variables.push_back(v);
}
// Adds constraints
std::vector<SCIP_CONS*> scip_constraints;
for (std::size_t i = 0; i < Base_class::constraints_.size(); ++i) {
const Linear_constraint* c = Base_class::constraints_[i];
const std::unordered_map<const Variable*, double>& coeffs = c->coefficients();
typename std::unordered_map<const Variable*, double>::const_iterator cur = coeffs.begin();
std::vector<SCIP_VAR*> cstr_variables(coeffs.size());
std::vector<double> cstr_values(coeffs.size());
std::size_t idx = 0;
for (; cur != coeffs.end(); ++cur) {
std::size_t var_idx = cur->first->index();
double coeff = cur->second;
cstr_variables[idx] = scip_variables[var_idx];
cstr_values[idx] = coeff;
++idx;
}
// Creates SCIP_CONS object
SCIP_CONS* cons = 0;
const std::string& name = c->name();
double lb, ub;
c->get_bounds(lb, ub);
SCIP_CALL(SCIPcreateConsLinear(scip, &cons, name.c_str(), int(coeffs.size()), cstr_variables.data(), cstr_values.data(), lb, ub, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE));
// Adds the constraint to scip
SCIP_CALL(SCIPaddCons(scip, cons));
// Stores the constraint for later on
scip_constraints.push_back(cons);
}
// Sets objective
// Determines the coefficient of each variable in the objective function
const std::unordered_map<const Variable*, double>& obj_coeffs = Base_class::objective_->coefficients();
typename std::unordered_map<const Variable*, double>::const_iterator cur = obj_coeffs.begin();
for (; cur != obj_coeffs.end(); ++cur) {
const Variable* var = cur->first;
double coeff = cur->second;
SCIP_CALL(SCIPchgVarObj(scip, scip_variables[var->index()], coeff));
}
// Sets the objective sense
bool minimize = (Base_class::objective_->sense() == Linear_objective::MINIMIZE);
SCIP_CALL(SCIPsetObjsense(scip, minimize ? SCIP_OBJSENSE_MINIMIZE : SCIP_OBJSENSE_MAXIMIZE));
// Turns presolve on (it's the SCIP default).
bool presolve = true;
if (presolve)
SCIP_CALL(SCIPsetIntParam(scip, "presolving/maxrounds", -1)); // maximal number of presolving rounds (-1: unlimited, 0: off)
else
SCIP_CALL(SCIPsetIntParam(scip, "presolving/maxrounds", 0)); // disable presolve
bool status = false;
// This tells scip to start the solution process
if (SCIPsolve(scip) == SCIP_OKAY) {
// Gets the best found solution from scip
SCIP_SOL* sol = SCIPgetBestSol(scip);
if (sol) {
// If optimal or feasible solution is found.
Base_class::result_.resize(Base_class::variables_.size());
for (std::size_t i = 0; i < Base_class::variables_.size(); ++i) {
FT x = SCIPgetSolVal(scip, sol, scip_variables[i]);
Variable* v = Base_class::variables_[i];
if (v->variable_type() != Variable::CONTINUOUS)
x = static_cast<int>(std::round(x));
v->set_solution_value(x);
Base_class::result_[i] = x;
}
status = true;
}
}
// Reports the status: optimal, infeasible, etc.
SCIP_STATUS scip_status = SCIPgetStatus(scip);
switch (scip_status) {
case SCIP_STATUS_OPTIMAL:
// Provides info only if fails.
break;
case SCIP_STATUS_GAPLIMIT:
// To be consistent with the other solvers.
// Provides info only if fails.
break;
case SCIP_STATUS_INFEASIBLE:
Base_class::error_message_ = "model was infeasible";
break;
case SCIP_STATUS_UNBOUNDED:
Base_class::error_message_ = "model was unbounded";
break;
case SCIP_STATUS_INFORUNBD:
Base_class::error_message_ = "model was either infeasible or unbounded";
break;
case SCIP_STATUS_TIMELIMIT:
Base_class::error_message_ = "aborted due to time limit";
break;
default:
Base_class::error_message_ = "aborted with status: " + std::to_string(scip_status);
break;
}
SCIP_CALL(SCIPresetParams(scip));
// Since the SCIPcreateVar captures all variables, we have to release them now
for (std::size_t i = 0; i < scip_variables.size(); ++i)
SCIP_CALL(SCIPreleaseVar(scip, &scip_variables[i]));
scip_variables.clear();
// The same for the constraints
for (std::size_t i = 0; i < scip_constraints.size(); ++i)
SCIP_CALL(SCIPreleaseCons(scip, &scip_constraints[i]));
scip_constraints.clear();
// After releasing all vars and cons we can free the scip problem.
// Remember this has always to be the last call to scip
SCIP_CALL(SCIPfree(&scip));
return status;
}
/// \endcond
};
} // namespace CGAL
#endif // CGAL_USE_SCIP or DOXYGEN_RUNNING
#endif // CGAL_SCIP_MIXED_INTEGER_PROGRAM_TRAITS_H
|