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"
58 using std::ostringstream;
66 m_CoinPackedMatrix(NULL),
77 cout <<
"inside CoinSolver destructor" << endl;
99 cout <<
"leaving CoinSolver destructor" << endl;
106 if(
osil.length() == 0 &&
osinstance == NULL)
throw ErrorClass(
"there is no instance");
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){
208 catch(
const ErrorClass& eclass){
209 std::cout <<
"THERE IS AN ERROR" << std::endl;
213 throw ErrorClass(
osrl) ;
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());
393 cbc_argv[i + 1] = cstr;
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);
431 #endif //symphony end
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;
536 catch(
const ErrorClass& eclass){
537 std::cout <<
"THERE IS AN ERROR" << std::endl;
541 throw ErrorClass(
osrl) ;
562 catch(
const ErrorClass& eclass){
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;
657 CbcMain1( num_cbc_argv, cbc_argv, model);
661 delete[] cbc_argv[ i];
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();
693 throw ErrorClass( errmsg );
697 catch(
const ErrorClass& eclass){
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");
728 catch(
const ErrorClass& eclass){
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)
double * getConstraintLowerBounds()
Get constraint lower bounds.
double * getVariableLowerBounds()
Get variable lower bounds.
double * getConstraintUpperBounds()
Get constraint upper bounds.
double os_strtod(const char *s00, char **se)
int getNumberOfInitVarValues()
Get the number of initial variable values.
int getNumberOfIntegerVariables()
getNumberOfIntegerVariables
char * getVariableTypes()
Get variable initial values.
~CoinSolver()
The class destructor.
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 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...
int getVariableNumber()
Get number of variables.
std::string osrl
osrl holds the solution or result of the model
bool setServiceName(std::string serviceName)
Set service name.
bool setVariableNumber(int variableNumber)
Set the variable number.
std::string osil
osil holds the problem instance as a std::string
std::string errormsg
errormsg is the error that is causing the exception to be thrown
OsiSolverInterface * osiSolver
osiSolver is the osi solver object – in this case clp, glpk, cbc, cplex, symphony or dylp ...
int getLinearConstraintCoefficientNumber()
Get number of specified (usually nonzero) linear constraint coefficient values.
std::vector< SolverOption * > getSolverOptions(std::string solver_name)
Get the options associated with a given solver.
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...
CoinPackedMatrix * m_CoinPackedMatrix
m_CoinPackedMatrix is a Coin Packed Matrix ojbect
bool bCallbuildSolverInstance
bCallbuildSolverInstance is set to true if buildSolverService has been called
Take an OSResult object and write a string that validates against OSrL.
OSResult * osresult
osresult holds the solution or result of the model in-memory as an OSResult object ...
bool bSetSolverOptions
bSetSolverOptions is set to true if setSolverOptions has been called, false otherwise ...
int getObjectiveNumber()
Get number of objectives.
bool setObjectiveNumber(int objectiveNumber)
Set the objective number.
bool setInstanceName(std::string instanceName)
Set instance name.
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
std::string * getVariableNames()
Get variable names.
CoinSolver()
The class constructor.
int getNumberOfNonlinearExpressions()
Get number of nonlinear expressions.
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
virtual void buildSolverInstance()
The implementation of the corresponding virtual function.
int * indexes
indexes holds an integer array of rowIdx (or colIdx) elements in coefMatrix (AMatrix).
std::string osol
osol holds the options for the solver
bool setSolutionNumber(int number)
set the number of solutions.
std::string getCoinSolverType(std::string osol_)
Get the solver type, e.g. clp or glpk.
double ** getDenseObjectiveCoefficients()
getDenseObjectiveCoefficients.
void fint fint fint real fint real real real real real real real real real * e
bool setObjectiveValues(int solIdx, double *objectiveValues, int n)
Set the [i]th optimization solution's objective values, where i equals the given solution index...
Used to read an OSiL string.
bool setNumberOfOtherVariableResults(int solIdx, int numberOfOtherVariableResults)
Set the [i]th optimization solution's other (non-standard/solver specific) variable-related results...
const char ** cbc_argv
when Cbc is the solver, these are the arguments sent to Cbc Solve
void writeResult(OsiSolverInterface *solver)
OSiLReader * m_osilreader
m_osilreader is an OSiLReader object used to create an osinstance from an osil string if needed ...
double lb
lb corresponds to the optional attribute that holds the variable lower bound.
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...
Variable ** var
Here we define a pointer to an array of var pointers.
int num_cbc_argv
the number of arguments in the argument list to the Cbc Solver
SparseMatrix * getLinearConstraintCoefficientsInRowMajor()
Get linear constraint coefficients in row major.
double value
initial value
OSOption * osoption
osoption holds the solver options in-memory as an OSOption object
std::string * getObjectiveMaxOrMins()
Get objective maxOrMins.
bool setCoinPackedMatrix()
Create a CoinPackedMatrix.
double ub
ub corresponds to the optional attribute that holds the variable upper bound.
Variables * variables
variables is a pointer to a Variables object
OSoLReader * m_osolreader
m_osolreader is an OSoLReader object used to create an osoption from an osol string if needed ...
SparseMatrix * getLinearConstraintCoefficientsInColumnMajor()
Get linear constraint coefficients in column major.
OSOption * readOSoL(const std::string &osol)
parse the OSoL solver options.
bool setGeneralMessage(std::string message)
Set the general message.
double * values
values holds a double array of value elements in coefMatrix (AMatrix), which contains nonzero element...
OSInstance * osinstance
osinstance holds the problem instance in-memory as an OSInstance object
double * getObjectiveConstants()
Get objective constants.
bool setGeneralStatusType(std::string type)
Set the general status type, which can be: success, error, warning.
std::string getInstanceName()
Get instance name.
InstanceData * instanceData
A pointer to an InstanceData object.
OSrLWriter * osrlwriter
osrlwriter object used to write osrl from an OSResult object
int getConstraintNumber()
Get number of constraints.
Objective ** obj
coef is pointer to an array of ObjCoef object pointers
bool getLinearConstraintCoefficientMajor()
Get whether the constraint coefficients is in column major (true) or row major (false).
bool setConstraintNumber(int constraintNumber)
Set the constraint number.
std::string sSolverName
sSolverName is the name of the Coin solver used, e.g.
int getNumberOfSolverOptions()
Get the number of solver options.
int getNumberOfQuadraticTerms()
Get the number of specified (usually nonzero) qTerms in the quadratic coefficients.
Used to read an OSoL string.
Objectives * objectives
objectives is a pointer to a Objectives object
std::string getInstanceSource()
Get instance source.
InitVarValue ** getInitVarValuesSparse()
Get the initial values associated with the variables in sparse form.
std::string os_dtoa_format(double x)
int * starts
starts holds an integer array of start elements in coefMatrix (AMatrix), which points to the start of...
double * getVariableUpperBounds()
Get variable upper bounds.
void dataEchoCheck()
Print out problem parameters.
bool addTimingInformation(std::string type, std::string category, std::string unit, std::string description, double value)
Add timing information.
virtual void solve()
The implementation of the corresponding virtual function.
std::string * getConstraintNames()
Get constraint names.
used for throwing exceptions.
virtual void setSolverOptions()
The implementation of the corresponding virtual function.
int getNumberOfBinaryVariables()
getNumberOfBinaryVariables
std::string getInstanceDescription()
Get instance description.
void fint fint fint real fint real * x