25#include "CoinTime.hpp"
26#include "CglPreProcess.hpp"
27#include "CglGomory.hpp"
28#include "CglSimpleRounding.hpp"
29#include "CglMixedIntegerRounding2.hpp"
30#include "CglKnapsackCover.hpp"
31#include "CglFlowCover.hpp"
32#include "CbcModel.hpp"
33#include "CbcBranchActual.hpp"
35#include "OsiClpSolverInterface.hpp"
36#include "OsiSymSolverInterface.hpp"
37#include "OsiVolSolverInterface.hpp"
41#include "OSCommonUtil.h"
53# error "don't have header file for time"
58using std::ostringstream;
66m_CoinPackedMatrix(NULL),
77 cout <<
"inside CoinSolver destructor" << endl;
99 cout <<
"leaving CoinSolver destructor" << endl;
107 clock_t start, finish;
115 duration = (double) (finish - start) / CLOCKS_PER_SEC;
116 cout <<
"Parsing took (seconds): "<< duration << endl;
117 cout <<
"Start Solve with a Coin Solver" << endl;
119 bool solverIsDefined =
false;
120 std::cout <<
"SOLVER NAME = " <<
sSolverName << std::endl;
122 solverIsDefined =
true;
128 solverIsDefined =
true;
133 if(
sSolverName.find(
"cplex") != std::string::npos){
135 solverIsDefined =
true;
140 if(
sSolverName.find(
"glpk") != std::string::npos){
142 solverIsDefined =
true;
143 osiSolver =
new OsiGlpkSolverInterface();
147 if(
sSolverName.find(
"dylp") != std::string::npos){
149 solverIsDefined =
true;
150 osiSolver =
new OsiDylpSolverInterface();
154 if(
sSolverName.find(
"symphony") != std::string::npos) {
155 #ifdef COIN_HAS_SYMPHONY
156 solverIsDefined =
true;
162 solverIsDefined =
true;
171 if(solverIsDefined ==
false)
throw ErrorClass(
"a supported solver was not defined");
187 int *intIndex = NULL;
192 if(numOfIntVars > 0) {
193 intIndex =
new int[ numOfIntVars];
196 if( (varType[i] ==
'B') || (varType[i]) ==
'I' ) {
200 osiSolver->setInteger( intIndex, numOfIntVars);
202 if(numOfIntVars > 0){
209 std::cout <<
"THERE IS AN ERROR" << std::endl;
225 std::map<std::string, OsiHintParam> hintParamMap;
226 hintParamMap[
"OsiDoPresolveInInitial"] = OsiDoPresolveInInitial;
227 hintParamMap[
"OsiDoDualInInitial"] = OsiDoDualInInitial;
228 hintParamMap[
"OsiDoPresolveInResolve"] = OsiDoPresolveInResolve;
229 hintParamMap[
"OsiDoDualInResolve"] = OsiDoDualInResolve;
230 hintParamMap[
"OsiDoScale"] = OsiDoScale;
231 hintParamMap[
"OsiDoCrash"] = OsiDoCrash;
232 hintParamMap[
"OsiDoReducePrint"] = OsiDoReducePrint;
233 hintParamMap[
"OsiDoInBranchAndCut"] = OsiDoInBranchAndCut;
234 hintParamMap[
"OsiLastHintParam"] = OsiLastHintParam;
237 std::map<std::string, OsiHintStrength> hintStrengthMap;
238 hintStrengthMap[
"OsiHintIgnore"] = OsiHintIgnore;
239 hintStrengthMap[
"OsiHintTry"] = OsiHintTry;
240 hintStrengthMap[
"OsiHintDo"] = OsiHintDo;
241 hintStrengthMap[
"OsiForceDo"] = OsiForceDo;
244 std::map<std::string, OsiStrParam> strParamMap;
245 strParamMap[
"OsiProbName"] = OsiProbName;
246 strParamMap[
"OsiSolverName"] = OsiSolverName;
247 strParamMap[
"OsiLastStrParam"] = OsiLastStrParam;
250 std::map<std::string, OsiDblParam> dblParamMap;
251 dblParamMap[
"OsiDualObjectiveLimit"] = OsiDualObjectiveLimit;
252 dblParamMap[
"OsiPrimalObjectiveLimit"] = OsiPrimalObjectiveLimit;
253 dblParamMap[
"OsiDualTolerance"] = OsiDualTolerance;
254 dblParamMap[
"OsiPrimalTolerance"] = OsiPrimalTolerance;
255 dblParamMap[
"OsiObjOffset"] = OsiObjOffset;
256 dblParamMap[
"OsiLastDblParam"] = OsiLastDblParam;
260 std::map<std::string, OsiIntParam> intParamMap;
261 intParamMap[
"OsiMaxNumIteration"] = OsiMaxNumIteration;
262 intParamMap[
"OsiMaxNumIterationHotStart"] = OsiMaxNumIterationHotStart;
263 intParamMap[
"OsiNameDiscipline"] = OsiNameDiscipline;
264 intParamMap[
"OsiLastIntParam"] = OsiLastIntParam;
275 OsiHintStrength hintStrength = OsiHintTry;
276 osiSolver->setHintParam(OsiDoReducePrint,
true, hintStrength);
296 std::vector<SolverOption*> optionsVector;
299 int num_osi_options = optionsVector.size();
304 for(i = 0; i < num_osi_options; i++){
305 std::cout <<
"osi solver option " << optionsVector[ i]->name << std::endl;
306 if (optionsVector[ i]->type ==
"OsiHintStrength" ){
307 if( hintStrengthMap.find( optionsVector[ i]->name ) != hintStrengthMap.end() ){
308 hintStrength = hintStrengthMap[ optionsVector[ i]->name] ;
312 for(i = 0; i < num_osi_options; i++){
313 std::cout <<
"osi solver option " << optionsVector[ i]->name << std::endl;
315 if (optionsVector[ i]->type ==
"OsiHintParam" ){
317 if( optionsVector[ i]->value ==
"true" ) {
323 if( hintParamMap.find( optionsVector[ i]->name ) != hintParamMap.end() ){
325 osiSolver->setHintParam( hintParamMap[ optionsVector[ i]->name] , yesNo, hintStrength);
329 else if(optionsVector[ i]->type ==
"OsiStrParam" ){
331 if( strParamMap.find( optionsVector[ i]->name ) != strParamMap.end() ){
333 osiSolver->setStrParam( strParamMap[ optionsVector[ i]->name] , optionsVector[ i]->value);
337 else if(optionsVector[ i]->type ==
"OsiDblParam" ){
339 if( dblParamMap.find( optionsVector[ i]->name ) != dblParamMap.end() ){
341 osiSolver->setDblParam( dblParamMap[ optionsVector[ i]->name] ,
os_strtod( optionsVector[ i]->value.c_str(), &pEnd ));
345 else if(optionsVector[ i]->type ==
"OsiIntParam" ){
348 if( intParamMap.find( optionsVector[ i]->name ) != intParamMap.end() ){
350 osiSolver->setIntParam( intParamMap[ optionsVector[ i]->name] , atoi( optionsVector[ i]->value.c_str() ) );
365 if(
sSolverName.find(
"cbc") != std::string::npos) {
368 int num_cbc_options = optionsVector.size();
370 std::string cbc_option;
377 cstr =
new char [cbc_option.size() + 1];
378 strcpy (cstr, cbc_option.c_str());
382 for(i = 0; i < num_cbc_options; i++){
383 std::cout <<
"cbc solver option " << optionsVector[ i]->name << std::endl;
384 std::cout <<
"cbc solver value " << optionsVector[ i]->value << std::endl;
385 if(optionsVector[ i]->value.length() > 0){
386 cbc_option =
"-" + optionsVector[ i]->name +
"="+optionsVector[ i]->value;
389 cbc_option =
"-" + optionsVector[ i]->name ;
391 cstr =
new char [cbc_option.size() + 1];
392 strcpy (cstr, cbc_option.c_str());
397 cbc_option =
"-quit";
398 cstr =
new char [cbc_option.size() + 1];
399 strcpy (cstr, cbc_option.c_str());
408 #ifdef COIN_HAS_SYMPHONY
410 if( !optionsVector.empty() ) optionsVector.clear();
412 if(
sSolverName.find(
"symphony") != std::string::npos) {
413 OsiSymSolverInterface * si =
414 dynamic_cast<OsiSymSolverInterface *
>(
osiSolver) ;
417 int num_sym_options = optionsVector.size();
420 for(i = 0; i < num_sym_options; i++){
421 std::cout <<
"symphony solver option " << optionsVector[ i]->name << std::endl;
422 std::cout <<
"symphony solver value " << optionsVector[ i]->value << std::endl;
423 if( optionsVector[ i]->name ==
"max_active_nodes"){
424 si->setSymParam(
"max_active_nodes", optionsVector[ i]->value);
441 cout <<
"number of variables initialed: " << m << endl;
447 cout <<
"get initial values " << endl;
450 double* denseInitVarVector;
451 denseInitVarVector =
new double[n];
453 initialed =
new bool[n];
455 for(k = 0; k < n; k++)
456 initialed[k] =
false;
460 cout <<
"done " << endl;
464 for(k = 0; k < m; k++)
465 { cout <<
"process component " << k <<
" -- index " << initVarVector[k]->
idx << endl;
466 i = initVarVector[k]->
idx;
467 if (initVarVector[k]->idx > n)
468 throw ErrorClass (
"Illegal index value in variable initialization");
470 initval = initVarVector[k]->
value;
473 throw ErrorClass (
"Initial value outside of bounds");
478 throw ErrorClass (
"Initial value outside of bounds");
483 throw ErrorClass (
"Initial value outside of bounds");
486 denseInitVarVector[initVarVector[k]->
idx] = initval;
487 initialed[initVarVector[k]->idx] =
true;
490 double default_initval;
491 default_initval = 0.0;
493 for(k = 0; k < n; k++)
494 { cout <<
"verify component " << k << endl;
498 denseInitVarVector[k] = default_initval;
504 denseInitVarVector[k] = default_initval;
510 denseInitVarVector[k] = default_initval;
516 denseInitVarVector[k] = default_initval;
520 cout <<
"set initial values: " << endl;
521 for (k=0; k < n; k++)
522 cout <<
" " << k <<
": " << denseInitVarVector[k] << endl;
524 osiSolver->setColSolution( denseInitVarVector);
525 delete[] denseInitVarVector;
528 cout <<
"done " << endl;
537 std::cout <<
"THERE IS AN ERROR" << std::endl;
554 osinstance->getLinearConstraintCoefficientNumber(),
555 columnMajor?
osinstance->getLinearConstraintCoefficientsInColumnMajor()->values :
osinstance->getLinearConstraintCoefficientsInRowMajor()->values,
556 columnMajor?
osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes :
osinstance->getLinearConstraintCoefficientsInRowMajor()->indexes,
557 columnMajor?
osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts :
osinstance->getLinearConstraintCoefficientsInRowMajor()->starts,
578 if(
sSolverName.find(
"clp") != std::string::npos)
throw ErrorClass(
"Clp cannot do integer programming");
579 if(
sSolverName.find(
"vol") != std::string::npos)
throw ErrorClass(
"Vol cannot do integer programming");
580 if(
sSolverName.find(
"dylp") != std::string::npos)
throw ErrorClass(
"DyLP cannot do integer programming");
581 if(
sSolverName.find(
"ipopt") != std::string::npos)
throw ErrorClass(
"Ipopt cannot do integer programming");
585 throw ErrorClass(
"This COIN-OR Solver is not configured for nonlinear programming");
591 throw ErrorClass(
"OSResult error: setServiceName");
593 throw ErrorClass(
"OSResult error: setInstanceName");
600 throw ErrorClass(
"OSResult error: setVariableNumer");
602 throw ErrorClass(
"OSResult error: setObjectiveNumber");
604 throw ErrorClass(
"OSResult error: setConstraintNumber");
606 throw ErrorClass(
"OSResult error: setSolutionNumer");
609 double start = CoinCpuTime();
611 if(
sSolverName.find(
"cbc") != std::string::npos){
621 std::string cbc_option;
627 cstr =
new char [cbc_option.size() + 1];
628 strcpy (cstr, cbc_option.c_str());
633 cbc_option =
"-log=0";
634 cstr =
new char [cbc_option.size() + 1];
635 strcpy (cstr, cbc_option.c_str());
640 cbc_option =
"-solve";
641 cstr =
new char [cbc_option.size() + 1];
642 strcpy (cstr, cbc_option.c_str());
646 cbc_option =
"-quit";
647 cstr =
new char [cbc_option.size() + 1];
648 strcpy (cstr, cbc_option.c_str());
652 std::cout <<
"CALLING THE CBC SOLVER CBCMAIN1()" << std::endl;
655 std::cout <<
"Cbc Option: " <<
cbc_argv[ i] << std::endl;
669 OsiSolverInterface *solver = model.solver();
670 cpuTime = CoinCpuTime() - start;
682 cpuTime = CoinCpuTime() - start;
691 errmsg =
"Coin Solver Error: " + e.message() +
"\n" +
" see method "
692 + e.methodName() +
" in class " + e.className();
708 if( lcl_osol.find(
"clp") != std::string::npos){
709 return "coin_solver_glpk";
712 if( lcl_osol.find(
"cbc") != std::string::npos){
713 return "coin_solver_cpx";
716 if( lcl_osol.find(
"cpx") != std::string::npos){
717 return "coin_solver_clp";
720 if(lcl_osol.find(
"glpk") != std::string::npos){
723 else throw ErrorClass(
"a supported solver was not defined");
758 else cout <<
"problem is a maximization" << endl;
783 std::string *rcost = NULL;
786 std::string description =
"";
790 if (solver->isProvenOptimal() ==
true){
800 *(z + 0) = solver->getObjValue();
803 *(x + i) = solver->getColSolution()[i];
809 *(y + i) = solver->getRowPrice()[ i];
818 int numberOfOtherVariableResults = 1;
822 ostringstream outStr;
824 for(i=0; i < numberOfVar; i++){
832 if(solver->isProvenPrimalInfeasible() ==
true)
835 if(solver->isProvenDualInfeasible() ==
true)
std::string os_dtoa_format(double x)
double os_strtod(const char *s00, char **se)
const char ** cbc_argv
when Cbc is the solver, these are the arguments sent to Cbc Solve
virtual void setSolverOptions()
The implementation of the corresponding virtual function.
virtual void buildSolverInstance()
The implementation of the corresponding virtual function.
int num_cbc_argv
the number of arguments in the argument list to the Cbc Solver
CoinPackedMatrix * m_CoinPackedMatrix
m_CoinPackedMatrix is a Coin Packed Matrix ojbect
std::string getCoinSolverType(std::string osol_)
Get the solver type, e.g. clp or glpk.
OSiLReader * m_osilreader
m_osilreader is an OSiLReader object used to create an osinstance from an osil string if needed
OsiSolverInterface * osiSolver
osiSolver is the osi solver object – in this case clp, glpk, cbc, cplex, symphony or dylp
CoinSolver()
The class constructor.
~CoinSolver()
The class destructor.
OSoLReader * m_osolreader
m_osolreader is an OSoLReader object used to create an osoption from an osol string if needed
void writeResult(OsiSolverInterface *solver)
OSrLWriter * osrlwriter
osrlwriter object used to write osrl from an OSResult object
virtual void solve()
The implementation of the corresponding virtual function.
void dataEchoCheck()
Print out problem parameters.
bool setCoinPackedMatrix()
Create a CoinPackedMatrix.
std::string sSolverName
sSolverName is the name of the Coin solver used, e.g.
std::string osol
osol holds the options for the solver
bool bSetSolverOptions
bSetSolverOptions is set to true if setSolverOptions has been called, false otherwise
std::string osrl
osrl holds the solution or result of the model
OSInstance * osinstance
osinstance holds the problem instance in-memory as an OSInstance object
bool bCallbuildSolverInstance
bCallbuildSolverInstance is set to true if buildSolverService has been called
std::string osil
osil holds the problem instance as a std::string
OSOption * osoption
osoption holds the solver options in-memory as an OSOption object
OSResult * osresult
osresult holds the solution or result of the model in-memory as an OSResult object
used for throwing exceptions.
std::string errormsg
errormsg is the error that is causing the exception to be thrown
double value
initial value
Variables * variables
variables is a pointer to a Variables object
Objectives * objectives
objectives is a pointer to a Objectives object
double * getConstraintLowerBounds()
Get constraint lower bounds.
int getNumberOfQuadraticTerms()
Get the number of specified (usually nonzero) qTerms in the quadratic coefficients.
double * getVariableUpperBounds()
Get variable upper bounds.
int getNumberOfIntegerVariables()
getNumberOfIntegerVariables
int getNumberOfBinaryVariables()
getNumberOfBinaryVariables
bool getLinearConstraintCoefficientMajor()
Get whether the constraint coefficients is in column major (true) or row major (false).
std::string getInstanceDescription()
Get instance description.
std::string getInstanceSource()
Get instance source.
int getConstraintNumber()
Get number of constraints.
int getLinearConstraintCoefficientNumber()
Get number of specified (usually nonzero) linear constraint coefficient values.
char * getVariableTypes()
Get variable initial values.
double ** getDenseObjectiveCoefficients()
getDenseObjectiveCoefficients.
InstanceData * instanceData
A pointer to an InstanceData object.
int getNumberOfNonlinearExpressions()
Get number of nonlinear expressions.
double * getObjectiveConstants()
Get objective constants.
double * getVariableLowerBounds()
Get variable lower bounds.
int getVariableNumber()
Get number of variables.
std::string * getVariableNames()
Get variable names.
std::string getInstanceName()
Get instance name.
std::string * getConstraintNames()
Get constraint names.
std::string * getObjectiveMaxOrMins()
Get objective maxOrMins.
double * getConstraintUpperBounds()
Get constraint upper bounds.
int getObjectiveNumber()
Get number of objectives.
InitVarValue ** getInitVarValuesSparse()
Get the initial values associated with the variables in sparse form.
int getNumberOfInitVarValues()
Get the number of initial variable values.
std::vector< SolverOption * > getSolverOptions(std::string solver_name)
Get the options associated with a given solver.
int getNumberOfSolverOptions()
Get the number of solver options.
bool setGeneralMessage(std::string message)
Set the general message.
bool setAnOtherVariableResult(int solIdx, int otherIdx, std::string name, std::string description, int *indexes, std::string *s, int n)
Set the [i]th optimization solution's other (non-standard/solver specific)variable-related results,...
bool setPrimalVariableValues(int solIdx, double *x, int n)
Set the [i]th optimization solution's primal variable values, where i equals the given solution index...
bool setDualVariableValues(int solIdx, double *lbValues, double *ubValues, int n)
Set the [i]th optimization solution's dual variable values, where i equals the given solution index.
bool setSolutionNumber(int number)
set the number of solutions.
bool setInstanceName(std::string instanceName)
Set instance name.
bool setNumberOfOtherVariableResults(int solIdx, int numberOfOtherVariableResults)
Set the [i]th optimization solution's other (non-standard/solver specific) variable-related results,...
bool setGeneralStatusType(std::string type)
Set the general status type, which can be: success, error, warning.
bool setObjectiveNumber(int objectiveNumber)
Set the objective number.
bool setObjectiveValues(int solIdx, double *objectiveValues, int n)
Set the [i]th optimization solution's objective values, where i equals the given solution index.
bool setServiceName(std::string serviceName)
Set service name.
bool setVariableNumber(int variableNumber)
Set the variable number.
bool setSolutionStatus(int solIdx, std::string type, std::string description)
Set the [i]th optimization solution status, where i equals the given solution index.
bool addTimingInformation(std::string type, std::string category, std::string unit, std::string description, double value)
Add timing information.
bool setConstraintNumber(int constraintNumber)
Set the constraint number.
Used to read an OSiL string.
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
Used to read an OSoL string.
OSOption * readOSoL(const std::string &osol)
parse the OSoL solver options.
Take an OSResult object and write a string that validates against OSrL.
std::string writeOSrL(OSResult *theosresult)
create an osrl string from an OSResult object
int numberOfObjectives
numberOfObjectives is the number of objective functions in the instance
Objective ** obj
coef is pointer to an array of ObjCoef object pointers
double ub
ub corresponds to the optional attribute that holds the variable upper bound.
double lb
lb corresponds to the optional attribute that holds the variable lower bound.
Variable ** var
Here we define a pointer to an array of var pointers.
This file defines the OSInstance class along with its supporting classes.