23 namespace module_lp_solver
37 static bool init =
false;
38 static const char* name =
"lpsolver";
41 logger <<
"Warning: Initializing module lpsolver more than once." << endl;
47 PyGILState_STATE state = PyGILState_Ensure();
53 PyGILState_Release(state);
55 catch (
const exception &e)
57 PyGILState_Release(state);
58 logger <<
"Error: " << e.what() << endl;
62 PyGILState_Release(state);
63 logger <<
"Error: unknown exception" << endl;
75 Object::createString<LPSolver>);
82 void LPSolver::solveObjective(
const string& colname)
85 if (colname.empty())
throw DataException(
"Empty objective name");
86 int col = glp_find_col(lp, colname.c_str());
88 throw DataException(
"Unknown objective name '" +
string(colname) +
"'");
89 lpx_set_obj_coef(lp, col, 1.0);
93 logger <<
"Solving for " << colname <<
"..." << endl;
96 int result = glp_simplex(lp, ¶meters);
99 double val = lpx_get_obj_val(lp);
103 logger <<
" Error " << result << endl;
105 logger <<
" Optimum " << val <<
" found at " << Date::now() << endl;
109 lpx_set_col_bnds(lp, col, LPX_DB,
114 lpx_set_obj_coef(lp, col, 0.0);
117 if (parameters.presolve) parameters.presolve = 0;
124 logger <<
"Start running the solver at " << Date::now() << endl;
127 glp_term_hook(solveroutputredirect,NULL);
130 glp_init_smcp(¶meters);
132 parameters.msg_lev = GLP_MSG_OFF;
134 parameters.msg_lev = GLP_MSG_ERR;
136 parameters.msg_lev = GLP_MSG_ON;
138 parameters.msg_lev = GLP_MSG_ALL;
141 if (modelfilename.empty())
143 if (datafilename.empty())
144 lp = lpx_read_model(modelfilename.c_str(), NULL, NULL);
146 lp = lpx_read_model(modelfilename.c_str(), datafilename.c_str(), NULL);
154 string c = modelfilename +
".mps";
155 lpx_write_mps(lp,c.c_str());
163 parameters.presolve = 1;
166 glp_set_obj_dir(lp, minimum ? GLP_MIN : GLP_MAX);
169 glp_create_index(lp);
172 logger <<
"Finished solver initialisation at " << Date::now() << endl;
175 if (objectives.empty())
177 for (list<string>::const_iterator i = objectives.begin();
178 i != objectives.end(); ++i)
182 if (!solutionfilename.empty())
183 lpx_print_sol(lp,solutionfilename.c_str());
187 glp_term_hook(NULL,NULL);
190 logger <<
"Finished running the solver at " << Date::now() << endl;
194 string LPSolver::replaceSpaces(
const string& input)
197 for (string::iterator i = x.begin(); i != x.end(); ++i)
198 if (*i ==
' ') *i =
'_';
224 for (list<string>::const_iterator i = objectives.begin();
225 i != objectives.end(); ++i)
227 Solver::writeElement(o, tag,
NOHEAD);
237 else if (pAttr.
isA(tag_datafile))
239 else if (pAttr.
isA(tag_modelfile))
241 else if (pAttr.
isA(tag_solutionfile))
243 else if (pAttr.
isA(tag_objective))
247 Solver::endElement(pIn, pAttr, pElement);
257 else if (attr.
isA(tag_datafile))
259 else if (attr.
isA(tag_modelfile))
261 else if (attr.
isA(tag_solutionfile))
263 else if (attr.
isA(tag_objective))
273 return Solver::getattro(attr);
283 else if (attr.
isA(tag_datafile))
285 else if (attr.
isA(tag_modelfile))
287 else if (attr.
isA(tag_solutionfile))
289 else if (attr.
isA(tag_objective))
292 PyObject* seq = PySequence_Fast(static_cast<PyObject*>(field),
"expected a list");
293 if (!PyList_Check(seq))
298 int len = PySequence_Size(static_cast<PyObject*>(field));
300 for (
int i = 0; i < len; i++)
302 item = PyList_GET_ITEM(seq, i);
307 return Solver::setattro(attr, field);