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);
75 template <
typename T,
typename V>
108 bool defaultParam =
false;
119 int error = GRBloadenv(&env, NULL);
121 param->operator()(env);
131 variable_type var (i);
145 std::vector<int> vIdx;
146 std::vector<double> vValue;
153 for (
typename std::vector<term_type>::const_iterator it = constr.
expression().
terms().begin(), ite = constr.
expression().
terms().end(); it != ite; ++it)
155 vIdx.push_back(it->variable().id());
156 vValue.push_back(it->coefficient());
166 error = GRBsetdblattrelement(
m_grbModel, GRB_DBL_ATTR_OBJ, it->variable().id(), it->coefficient());
177 #ifdef DEBUG_GUROBIAPI
183 error = GRBgetintattr(
m_grbModel, GRB_INT_ATTR_STATUS, &status);
186 if (status == GRB_INFEASIBLE)
191 limboPrint(kERROR,
"Model is infeasible, compute IIS and write to problem.ilp\n");
193 #ifdef DEBUG_GUROBIAPI
201 error = GRBgetdblattrelement(
m_grbModel, GRB_DBL_ATTR_X, var.
id(), &value);
219 case GRB_INF_OR_UNBD:
243 template <
typename T,
typename V>
251 #if GUROBIFILEAPI == 1
252 #include "gurobi_c++.h"
256 template <
typename T>
260 typedef T value_type;
266 std::list<std::pair<std::string, value_type> > vVariable;
271 virtual boost::shared_ptr<solution_type> operator()(std::string
const& fileName,
bool =
true)
const
274 boost::shared_ptr<solution_type> pSol (
new solution_type);
276 std::cout <<
"rm -rf "+fileName+
".sol" << std::endl;
277 std::cout << system((
"rm -rf "+fileName+
".sol").c_str()) << std::endl;;
279 std::cout <<
"solve linear program "+fileName << std::endl;
280 this->solve_lp(fileName);
284 std::ifstream solFile ((fileName+
".sol").c_str(), std::ifstream::in);
285 if (!solFile.good()) BOOST_ASSERT_MSG(
false, (
"failed to open " + fileName +
".sol").c_str());
291 solFile >> var >> var >> var >> var >> value;
294 while (!solFile.eof())
296 solFile >> var >> value;
297 pSol->vVariable.push_back(make_pair(var, value));
308 virtual void solve_lp(std::string fileName)
const
312 GRBEnv env = GRBEnv();
313 GRBModel model = GRBModel(env, fileName+
".lp");
317 int optimstatus = model.get(GRB_IntAttr_Status);
319 if (optimstatus == GRB_INF_OR_UNBD)
321 model.getEnv().set(GRB_IntParam_Presolve, 0);
323 optimstatus = model.get(GRB_IntAttr_Status);
326 if (optimstatus == GRB_OPTIMAL)
328 double objval = model.get(GRB_DoubleAttr_ObjVal);
329 std::cout <<
"Optimal objective: " << objval << std::endl;
331 model.write(fileName+
".sol");
333 else if (optimstatus == GRB_INFEASIBLE)
335 std::cout <<
"Model is infeasible" << std::endl;
340 model.write(fileName+
".ilp");
342 else if (optimstatus == GRB_UNBOUNDED)
344 std::cout <<
"Model is unbounded" << std::endl;
348 std::cout <<
"Optimization was stopped with status = "
349 << optimstatus << std::endl;
352 catch(GRBException e)
354 std::cout <<
"Error code = " << e.getErrorCode() << std::endl;
355 std::cout << e.getMessage() << std::endl;
359 std::cout <<
"Error during optimization" << std::endl;
Gurobi API with limbo::solvers::LinearModel.
void setNumThreads(int v)
set number of threads
variable_value_type variableSolution(variable_type const &var) const
Base class for custom Gurobi parameters.
Describe properties of a variable.
SolverProperty
Some enums used in solver.
SolverProperty optimizeType() const
model_type * m_model
model for the problem
Basic utilities such as variables and linear expressions in solvers.
std::string const & constraintName(constraint_type const &constr) const
SolverProperty variableNumericType(variable_type const &var)
V variable_value_type
V variable.
T coefficient_value_type
T coefficient.
expression_type const & objective() const
Describe linear constraint.
int m_numThreads
number of threads
void setOutputFlag(int v)
set output flag
std::vector< constraint_type > const & constraints() const
unsigned int numVariables() const
virtual void operator()(GRBmodel *) const
customize model
virtual void operator()(GRBenv *env) const
customize environment
GurobiLinearApi(model_type *model)
constructor
expression_type const & expression() const
variable_type variable(unsigned int id) const
void errorHandler(GRBenv *env, int error) const
error handler
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
std::string variableName(variable_type const &var) const
coefficient_value_type rightHandSide() const
GurobiLinearApi & operator=(GurobiLinearApi const &rhs)
assignment, forbidden
GurobiParameters()
constructor
int limboPrint(MessageType m, const char *format,...)
formatted print with prefix
std::vector< term_type > const & terms() const
GRBmodel * m_grbModel
model for Gurobi
void setVariableSolution(variable_type const &var, variable_value_type v)
variable_value_type variableLowerBound(variable_type const &var)
model to describe an optimization problem
int m_outputFlag
control log from Gurobi
variable_value_type variableUpperBound(variable_type const &var)
#define limboAssertMsg(condition, args...)
custom assertion with message
SolverProperty operator()(parameter_type *param=NULL)
API to run the algorithm.
LinearModel< T, V > model_type
linear model type for the problem
~GurobiLinearApi()
destructor
virtual ~GurobiParameters()
destructor