8#ifndef LIMBO_SOLVERS_API_GUROBIAPI_H
9#define LIMBO_SOLVERS_API_GUROBIAPI_H
15#include <boost/lexical_cast.hpp>
16#include <boost/shared_ptr.hpp>
17#include <boost/assert.hpp>
49 GRBsetintparam(env, GRB_INT_PAR_OUTPUTFLAG,
m_outputFlag);
74template <typename T, std::size_t = std::numeric_limits<T>::is_integer>
77 inline T operator()(
double value)
const
84struct SmartRound<T, 1>
86 inline T operator()(
double value)
const
88 return std::round(value);
95template <
typename T,
typename V>
128 bool defaultParam =
false;
139 int error = GRBloadenv(&env, NULL);
141 param->operator()(env);
143 error = GRBnewmodel(env, &
m_grbModel,
"GurobiLinearApi",
m_model->numVariables(), NULL, NULL, NULL, NULL, NULL);
149 for (
unsigned int i = 0, ie =
m_model->numVariables(); i < ie; ++i)
151 variable_type var (i);
152 error = GRBsetdblattrelement(
m_grbModel, GRB_DBL_ATTR_LB, var.
id(),
m_model->variableLowerBound(var));
154 error = GRBsetdblattrelement(
m_grbModel, GRB_DBL_ATTR_UB, var.
id(),
m_model->variableUpperBound(var));
156 error = GRBsetdblattrelement(
m_grbModel, GRB_DBL_ATTR_START, var.
id(),
m_model->variableSolution(var));
158 error = GRBsetcharattrelement(
m_grbModel, GRB_CHAR_ATTR_VTYPE, var.
id(),
m_model->variableNumericType(var) ==
CONTINUOUS? GRB_CONTINUOUS : GRB_INTEGER);
161 "LinearModel<T, V> is declared as V = integer type, but variable %s is CONTINUOUS",
m_model->variableName(var).c_str());
162 error = GRBsetstrattrelement(
m_grbModel, GRB_STR_ATTR_VARNAME, var.
id(),
m_model->variableName(var).c_str());
167 std::vector<int> vIdx;
168 std::vector<double> vValue;
169 for (
unsigned int i = 0, ie =
m_model->constraints().size(); i < ie; ++i)
171 constraint_type
const& constr =
m_model->constraints().at(i);
175 for (
typename std::vector<term_type>::const_iterator it = constr.
expression().
terms().begin(), ite = constr.
expression().
terms().end(); it != ite; ++it)
177 vIdx.push_back(it->variable().id());
178 vValue.push_back(it->coefficient());
186 for (
typename std::vector<term_type>::const_iterator it =
m_model->objective().terms().begin(), ite =
m_model->objective().terms().end(); it != ite; ++it)
188 error = GRBsetdblattrelement(
m_grbModel, GRB_DBL_ATTR_OBJ, it->variable().id(), it->coefficient());
191 error = GRBsetintattr(
m_grbModel, GRB_INT_ATTR_MODELSENSE,
m_model->optimizeType() ==
MIN? GRB_MINIMIZE : GRB_MAXIMIZE);
199#ifdef DEBUG_GUROBIAPI
205 error = GRBgetintattr(
m_grbModel, GRB_INT_ATTR_STATUS, &status);
208 if (status == GRB_INFEASIBLE)
213 limboPrint(kERROR,
"Model is infeasible, compute IIS and write to problem.ilp\n");
215#ifdef DEBUG_GUROBIAPI
221 for (
unsigned int i = 0; i <
m_model->numVariables(); ++i)
223 variable_type var =
m_model->variable(i);
225 error = GRBgetdblattrelement(
m_grbModel, GRB_DBL_ATTR_X, var.
id(), &value);
227 m_model->setVariableSolution(
m_model->variable(i), sround(value));
243 case GRB_INF_OR_UNBD:
267template <
typename T,
typename V>
275#if GUROBIFILEAPI == 1
276#include "gurobi_c++.h"
284 typedef T value_type;
290 std::list<std::pair<std::string, value_type> > vVariable;
295 virtual boost::shared_ptr<solution_type> operator()(std::string
const& fileName,
bool =
true)
const
298 boost::shared_ptr<solution_type> pSol (
new solution_type);
300 std::cout <<
"rm -rf "+fileName+
".sol" << std::endl;
301 std::cout << system((
"rm -rf "+fileName+
".sol").c_str()) << std::endl;;
303 std::cout <<
"solve linear program "+fileName << std::endl;
304 this->solve_lp(fileName);
308 std::ifstream solFile ((fileName+
".sol").c_str(), std::ifstream::in);
309 if (!solFile.good()) BOOST_ASSERT_MSG(
false, (
"failed to open " + fileName +
".sol").c_str());
315 solFile >> var >> var >> var >> var >> value;
318 while (!solFile.eof())
320 solFile >> var >> value;
321 pSol->vVariable.push_back(make_pair(var, value));
332 virtual void solve_lp(std::string fileName)
const
336 GRBEnv env = GRBEnv();
337 GRBModel model = GRBModel(env, fileName+
".lp");
341 int optimstatus = model.get(GRB_IntAttr_Status);
343 if (optimstatus == GRB_INF_OR_UNBD)
345 model.getEnv().set(GRB_IntParam_Presolve, 0);
347 optimstatus = model.get(GRB_IntAttr_Status);
350 if (optimstatus == GRB_OPTIMAL)
352 double objval = model.get(GRB_DoubleAttr_ObjVal);
353 std::cout <<
"Optimal objective: " << objval << std::endl;
355 model.write(fileName+
".sol");
357 else if (optimstatus == GRB_INFEASIBLE)
359 std::cout <<
"Model is infeasible" << std::endl;
364 model.write(fileName+
".ilp");
366 else if (optimstatus == GRB_UNBOUNDED)
368 std::cout <<
"Model is unbounded" << std::endl;
372 std::cout <<
"Optimization was stopped with status = "
373 << optimstatus << std::endl;
376 catch(GRBException e)
378 std::cout <<
"Error code = " << e.getErrorCode() << std::endl;
379 std::cout << e.getMessage() << std::endl;
383 std::cout <<
"Error during optimization" << std::endl;
#define limboAssertMsg(condition, args...)
custom assertion with message
Basic utilities such as variables and linear expressions in solvers.
void errorHandler(GRBenv *env, int error) const
error handler
SolverProperty operator()(parameter_type *param=NULL)
API to run the algorithm.
LinearModel< model_type::coefficient_value_type, model_type::variable_value_type > model_type
GurobiLinearApi(model_type *model)
constructor
~GurobiLinearApi()
destructor
GurobiLinearApi & operator=(GurobiLinearApi const &rhs)
assignment, forbidden
GurobiLinearApi(GurobiLinearApi const &rhs)
copy constructor, forbidden
Base class for custom Gurobi parameters.
virtual ~GurobiParameters()
destructor
virtual void operator()(GRBenv *env) const
customize environment
void setNumThreads(int v)
set number of threads
virtual void operator()(GRBmodel *) const
customize model
GurobiParameters()
constructor
void setOutputFlag(int v)
set output flag
int m_numThreads
number of threads
int m_outputFlag
control log from Gurobi
coefficient_value_type rightHandSide() const
expression_type const & expression() const
std::vector< term_type > const & terms() const
model to describe an optimization problem
V variable_value_type
V variable.
VariableProperty< variable_value_type > property_type
variable property type
Variable< coefficient_value_type > variable_type
variable type
T coefficient_value_type
T coefficient.
LinearConstraint< coefficient_value_type > constraint_type
constraint type
LinearExpression< coefficient_value_type > expression_type
expression type
LinearTerm< coefficient_value_type > term_type
term type
namespace for Limbo.Solvers
SolverProperty
Some enums used in solver.
@ OPTIMAL
optimally solved
@ CONTINUOUS
floating point number
@ INFEASIBLE
the model is infeasible
@ UNBOUNDED
the model is unbounded
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
int limboPrint(MessageType m, const char *format,...)
formatted print with prefix
Round floating point solutions to integer if the variable type is integer. If not rounded,...