OSCoinSolver.cpp
Go to the documentation of this file.
1 
20 #define DEBUG
21 
22 #include "OSCoinSolver.h"
23 #include "OSInstance.h"
24 #include "OSFileUtil.h"
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" //for CbcSOS
34 
35 #include "OsiClpSolverInterface.hpp"
36 #include "OsiSymSolverInterface.hpp"
37 #include "OsiVolSolverInterface.hpp"
38 
39 #include "OSDataStructures.h"
40 #include "OSParameters.h"
41 #include "OSCommonUtil.h"
42 #include "OSMathUtil.h"
43 
44 #include<map>
45 
46 #include <iostream>
47 #ifdef HAVE_CTIME
48 # include <ctime>
49 #else
50 # ifdef HAVE_TIME_H
51 # include <time.h>
52 # else
53 # error "don't have header file for time"
54 # endif
55 #endif
56 using std::cout;
57 using std::endl;
58 using std::ostringstream;
59 
60 
61 
63 osiSolver(NULL),
64 m_osilreader(NULL),
65 m_osolreader(NULL),
66 m_CoinPackedMatrix(NULL),
67 cbc_argv( NULL),
68 num_cbc_argv( 0),
69 cpuTime( 0)
70 
71 {
72 osrlwriter = new OSrLWriter();
73 }
74 
76  #ifdef DEBUG
77  cout << "inside CoinSolver destructor" << endl;
78  #endif
79  if(m_osilreader != NULL) delete m_osilreader;
80  m_osilreader = NULL;
81  if(m_osolreader != NULL) delete m_osolreader;
82  m_osolreader = NULL;
83  delete m_CoinPackedMatrix;
84  m_CoinPackedMatrix = NULL;
85  delete osiSolver;
86  if(osiSolver != NULL) osiSolver = NULL;
87  delete osrlwriter;
88  osrlwriter = NULL;
89  delete osresult;
90  osresult = NULL;
91  if(num_cbc_argv > 0){
92  int i;
93  for(i = 0; i < num_cbc_argv; i++){
94  //delete cbc_argv[ i];
95  }
96  //delete[] cbc_argv;
97  cbc_argv = NULL;
98  }
99  cout << "leaving CoinSolver destructor" << endl;
100 }
101 
102 
104  try{
105  osresult = new OSResult();
106  if(osil.length() == 0 && osinstance == NULL) throw ErrorClass("there is no instance");
107  clock_t start, finish;
108  double duration;
109  start = clock();
110  if(osinstance == NULL){
111  m_osilreader = new OSiLReader();
113  }
114  finish = clock();
115  duration = (double) (finish - start) / CLOCKS_PER_SEC;
116  cout << "Parsing took (seconds): "<< duration << endl;
117  cout << "Start Solve with a Coin Solver" << endl;
118  // get the type of solver requested from OSoL string
119  bool solverIsDefined = false;
120  std::cout << "SOLVER NAME = " << sSolverName << std::endl;
121  if( sSolverName.find("clp") != std::string::npos){
122  solverIsDefined = true;
123  osiSolver = new OsiClpSolverInterface();
124  }
125  else{
126  if( sSolverName.find("vol") != std::string::npos){
127  #ifdef COIN_HAS_VOL
128  solverIsDefined = true;
129  osiSolver = new OsiVolSolverInterface();
130  #endif
131  }
132  else{
133  if( sSolverName.find( "cplex") != std::string::npos){
134  #ifdef COIN_HAS_CPX
135  solverIsDefined = true;
136  osiSolver = new OsiCpxSolverInterface();
137  #endif
138  }
139  else{
140  if(sSolverName.find( "glpk") != std::string::npos){
141  #ifdef COIN_HAS_GLPK
142  solverIsDefined = true;
143  osiSolver = new OsiGlpkSolverInterface();
144  #endif
145  }
146  else{
147  if(sSolverName.find( "dylp") != std::string::npos){
148  #ifdef COIN_HAS_DYLP
149  solverIsDefined = true;
150  osiSolver = new OsiDylpSolverInterface();
151  #endif
152  }
153  else{
154  if( sSolverName.find( "symphony") != std::string::npos) {
155  #ifdef COIN_HAS_SYMPHONY
156  solverIsDefined = true;
157  osiSolver = new OsiSymSolverInterface();
158  #endif
159  }
160  else{
161  // default solver is CBC
162  solverIsDefined = true;
163  osiSolver = new OsiClpSolverInterface();
164  }
165  }
166  }
167  }
168  }
169  }
170 
171  if(solverIsDefined == false) throw ErrorClass("a supported solver was not defined");
172  if(osinstance->getConstraintNumber() <= 0)throw ErrorClass("Coin solver Needs Constraints");
173  if(osinstance->getVariableNumber() <= 0)throw ErrorClass("Coin solver requires decision variables");
174  if(osinstance->getObjectiveNumber() <= 0) throw ErrorClass("Coin solver needs an objective function");
175  if(osinstance->getLinearConstraintCoefficientNumber() <= 0) throw ErrorClass("Coin solver needs linear constraints");
176  if(!setCoinPackedMatrix() ) throw ErrorClass("Problem generating coin packed matrix");
181  );
182  //dataEchoCheck();
183  if(osinstance->getObjectiveNumber() == 0) throw ErrorClass("there is no objective function");
184  if( osinstance->getObjectiveMaxOrMins()[0] == "min") osiSolver->setObjSense(1.0);
185  else osiSolver->setObjSense(-1.0);
186  // set the integer variables
187  int *intIndex = NULL;
188  int i = 0;
189  int k = 0;
190  char *varType;
192  if(numOfIntVars > 0) {
193  intIndex = new int[ numOfIntVars];
194  varType = osinstance->getVariableTypes();
195  for(i = 0; i < osinstance->getVariableNumber(); i++){
196  if( (varType[i] == 'B') || (varType[i]) == 'I' ) {
197  intIndex[k++] = i;
198  }
199  }
200  osiSolver->setInteger( intIndex, numOfIntVars);
201  }
202  if(numOfIntVars > 0){
203  delete[] intIndex;
204  intIndex = NULL;
205  }
207  }
208  catch(const ErrorClass& eclass){
209  std::cout << "THERE IS AN ERROR" << std::endl;
211  osresult->setGeneralStatusType( "error");
213  throw ErrorClass( osrl) ;
214  }
215 }//end buildSolverInstance()
216 
217 
218 
219 void CoinSolver::setSolverOptions() throw (ErrorClass) {
220 
221 
222 
223  // the osi maps
224  // the OsiHintParamameter Map
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;
235  //
236  // the OsiHintStrength Map
237  std::map<std::string, OsiHintStrength> hintStrengthMap;
238  hintStrengthMap["OsiHintIgnore"] = OsiHintIgnore;
239  hintStrengthMap["OsiHintTry"] = OsiHintTry;
240  hintStrengthMap["OsiHintDo"] = OsiHintDo;
241  hintStrengthMap["OsiForceDo"] = OsiForceDo;
242  //
243  // the OsiStrParam Map
244  std::map<std::string, OsiStrParam> strParamMap;
245  strParamMap["OsiProbName"] = OsiProbName;
246  strParamMap["OsiSolverName"] = OsiSolverName;
247  strParamMap["OsiLastStrParam"] = OsiLastStrParam;
248  //
249  // the OsiDblParam Map
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;
257  //
258  //
259  // the OsiIntParam Map
260  std::map<std::string, OsiIntParam> intParamMap;
261  intParamMap["OsiMaxNumIteration"] = OsiMaxNumIteration;
262  intParamMap["OsiMaxNumIterationHotStart"] = OsiMaxNumIterationHotStart;
263  intParamMap["OsiNameDiscipline"] = OsiNameDiscipline;
264  intParamMap["OsiLastIntParam"] = OsiLastIntParam;
265  //
266  //
267  // initialize low level of printing
268 
269 
270  /*
271  * start default settings -- these get set
272  * even when the OSOption object is NULL
273  *
274  * */
275  OsiHintStrength hintStrength = OsiHintTry; //don't want too much output
276  osiSolver->setHintParam(OsiDoReducePrint, true, hintStrength);
277  osiSolver->setDblParam(OsiObjOffset, osinstance->getObjectiveConstants()[0]);
278  /*
279  * end default settings
280  *
281  * */
282 
283  //
284  try{
285  if(osoption == NULL && osol.length() > 0)
286  {
287  m_osolreader = new OSoLReader();
289  }
290 
291  if(osoption != NULL){
292 
293  std::cout << "number of solver options " << osoption->getNumberOfSolverOptions() << std::endl;
294  if( osoption->getNumberOfSolverOptions() <= 0) return;
295  this->bSetSolverOptions = true;
296  std::vector<SolverOption*> optionsVector;
297  //get the osi options
298  optionsVector = osoption->getSolverOptions( "osi");
299  int num_osi_options = optionsVector.size();
300  int i;
301  char *pEnd;
302  bool yesNo;
303 
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] ;
309  }
310  }
311  }
312  for(i = 0; i < num_osi_options; i++){
313  std::cout << "osi solver option " << optionsVector[ i]->name << std::endl;
314 
315  if (optionsVector[ i]->type == "OsiHintParam" ){
316 
317  if( optionsVector[ i]->value == "true" ) {
318  yesNo = true;
319  }
320  else{
321  yesNo = false;
322  }
323  if( hintParamMap.find( optionsVector[ i]->name ) != hintParamMap.end() ){
324 
325  osiSolver->setHintParam( hintParamMap[ optionsVector[ i]->name] , yesNo, hintStrength);
326  }
327 
328  }
329  else if(optionsVector[ i]->type == "OsiStrParam" ){
330 
331  if( strParamMap.find( optionsVector[ i]->name ) != strParamMap.end() ){
332 
333  osiSolver->setStrParam( strParamMap[ optionsVector[ i]->name] , optionsVector[ i]->value);
334  }
335 
336  }
337  else if(optionsVector[ i]->type == "OsiDblParam" ){
338 
339  if( dblParamMap.find( optionsVector[ i]->name ) != dblParamMap.end() ){
340 
341  osiSolver->setDblParam( dblParamMap[ optionsVector[ i]->name] , os_strtod( optionsVector[ i]->value.c_str(), &pEnd ));
342  }
343 
344  }
345  else if(optionsVector[ i]->type == "OsiIntParam" ){
346 
347 
348  if( intParamMap.find( optionsVector[ i]->name ) != intParamMap.end() ){
349 
350  osiSolver->setIntParam( intParamMap[ optionsVector[ i]->name] , atoi( optionsVector[ i]->value.c_str() ) );
351  }
352 
353  }
354  }
355 
356  // treat Cbc separately to take advantage of CbcMain1()
357 
358 
359 
360 
361 
362  //if(optionsVector.size() > 0) optionsVector.clear();
363 // if( !optionsVector.empty() ) optionsVector.clear(); //HIG: This must eventually come out
364 
365  if( sSolverName.find( "cbc") != std::string::npos) {
366  // get Cbc options
367  optionsVector = osoption->getSolverOptions( "cbc");
368  int num_cbc_options = optionsVector.size();
369  char *cstr;
370  std::string cbc_option;
371  // we are going to add a log level option -- it can be overridden
372  num_cbc_argv = optionsVector.size() + 2;
373  cbc_argv = new const char*[ num_cbc_argv];
374 
375  // the first option
376  cbc_option = "OS";
377  cstr = new char [cbc_option.size() + 1];
378  strcpy (cstr, cbc_option.c_str());
379  cbc_argv[ 0] = cstr;
380 
381 
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;
387  }
388  else{
389  cbc_option = "-" + optionsVector[ i]->name ;
390  }
391  cstr = new char [cbc_option.size() + 1];
392  strcpy (cstr, cbc_option.c_str());
393  cbc_argv[i + 1] = cstr;
394  }
395 
396  // the quit option
397  cbc_option = "-quit";
398  cstr = new char [cbc_option.size() + 1];
399  strcpy (cstr, cbc_option.c_str());
400  cbc_argv[ num_cbc_argv - 1] = cstr;
401 
402  }//end of cbc if
403 
404  // also need to treat SYMPHONY differently
405 
406 
407  // set some OSI options
408  #ifdef COIN_HAS_SYMPHONY
409  //if(optionsVector.size() > 0) optionsVector.clear();
410  if( !optionsVector.empty() ) optionsVector.clear();
411  //first the number of processors -- applies only to SYMPHONY
412  if( sSolverName.find( "symphony") != std::string::npos) {
413  OsiSymSolverInterface * si =
414  dynamic_cast<OsiSymSolverInterface *>(osiSolver) ;
415 
416  optionsVector = osoption->getSolverOptions( "symphony");
417  int num_sym_options = optionsVector.size();
418 
419 
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);
425  }
426  else{
427  //ignore for now
428  }
429  }
430  }
431  #endif //symphony end
432 
433 
434  //now set initial values
435  int n,m,k;
436  if (osoption != NULL)
438  else
439  m = 0;
440 #ifdef DEBUG
441  cout << "number of variables initialed: " << m << endl;
442 #endif
443 
444  if (m > 0)
445  {
446 #ifdef DEBUG
447  cout << "get initial values " << endl;
448 #endif
450  double* denseInitVarVector;
451  denseInitVarVector = new double[n];
452  bool* initialed;
453  initialed = new bool[n];
454 
455  for(k = 0; k < n; k++)
456  initialed[k] = false;
457 
458  InitVarValue** initVarVector = osoption->getInitVarValuesSparse();
459 #ifdef DEBUG
460  cout << "done " << endl;
461 #endif
462 
463  double initval;
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");
469 
470  initval = initVarVector[k]->value;
472  { if (osinstance->instanceData->variables->var[k]->lb > initval)
473  throw ErrorClass ("Initial value outside of bounds");
474  }
475  else
477  { if (osinstance->instanceData->variables->var[k]->ub < initval)
478  throw ErrorClass ("Initial value outside of bounds");
479  }
480  else
481  { if ((osinstance->instanceData->variables->var[k]->lb > initval) ||
482  (osinstance->instanceData->variables->var[k]->ub < initval))
483  throw ErrorClass ("Initial value outside of bounds");
484  }
485 
486  denseInitVarVector[initVarVector[k]->idx] = initval;
487  initialed[initVarVector[k]->idx] = true;
488  }
489 
490  double default_initval;
491  default_initval = 0.0;
492 
493  for(k = 0; k < n; k++)
494  { cout << "verify component " << k << endl;
495  if (!initialed[k])
497  if (osinstance->instanceData->variables->var[k]->lb <= default_initval)
498  denseInitVarVector[k] = default_initval;
499  else
500  denseInitVarVector[k] = osinstance->instanceData->variables->var[k]->lb;
501  else
503  if (osinstance->instanceData->variables->var[k]->ub >= default_initval)
504  denseInitVarVector[k] = default_initval;
505  else
506  denseInitVarVector[k] = osinstance->instanceData->variables->var[k]->ub;
507  else
508  if ((osinstance->instanceData->variables->var[k]->lb <= default_initval) &&
509  (osinstance->instanceData->variables->var[k]->ub >= default_initval))
510  denseInitVarVector[k] = default_initval;
511  else
512  if (osinstance->instanceData->variables->var[k]->lb > default_initval)
513  denseInitVarVector[k] = osinstance->instanceData->variables->var[k]->lb;
514  else
515  denseInitVarVector[k] = osinstance->instanceData->variables->var[k]->ub;
516  denseInitVarVector[k] = default_initval;
517  denseInitVarVector[k] = osinstance->instanceData->variables->var[k]->lb;
518  }
519 #ifdef DEBUG
520  cout << "set initial values: " << endl;
521  for (k=0; k < n; k++)
522  cout << " " << k << ": " << denseInitVarVector[k] << endl;
523 #endif
524  osiSolver->setColSolution( denseInitVarVector);
525  delete[] denseInitVarVector;
526  delete[] initialed;
527 #ifdef DEBUG
528  cout << "done " << endl;
529 #endif
530 
531  } // end if (m > 0)
532  }// end of osoption if
533 
534 
535  }//end of try
536  catch(const ErrorClass& eclass){
537  std::cout << "THERE IS AN ERROR" << std::endl;
539  osresult->setGeneralStatusType( "error");
541  throw ErrorClass( osrl) ;
542  }
543 }//end setSolverOptions()
544 
545 
547  bool columnMajor = osinstance->getLinearConstraintCoefficientMajor();
548  try{
549  int maxGap = 0;
550  m_CoinPackedMatrix = new CoinPackedMatrix(
551  columnMajor, //Column or Row Major
552  columnMajor? osinstance->getConstraintNumber() : osinstance->getVariableNumber(), //Minor Dimension
553  columnMajor? osinstance->getVariableNumber() : osinstance->getConstraintNumber(), //Major Dimension
554  osinstance->getLinearConstraintCoefficientNumber(), //Number of nonzeroes
556  columnMajor? osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes : osinstance->getLinearConstraintCoefficientsInRowMajor()->indexes, //Pointer to start of minor dimension indexes -- change to allow for row storage
558  0, 0, maxGap );
559 
560  return true;
561  }
562  catch(const ErrorClass& eclass){
564  osresult->setGeneralStatusType( "error");
566  throw ;
567  }
568 } // end setCoinPackedMatrix
569 
570 void CoinSolver::solve() throw (ErrorClass) {
571  // make sure the solver instance exists
572  if( this->bCallbuildSolverInstance == false) buildSolverInstance();
573  if( this->bSetSolverOptions == false) setSolverOptions();
574 
575  // first check the various solvers and see if they are of the proper problem type
577  // throw an exception if we have a solver that cannot do integer programming
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");
582  }
585  throw ErrorClass( "This COIN-OR Solver is not configured for nonlinear programming");
586  }
587  // if we are throw an exception if the problem is nonlinear
588 
589  // resultHeader information
590  if(osresult->setServiceName("Solved with Coin Solver: " + sSolverName) != true)
591  throw ErrorClass("OSResult error: setServiceName");
593  throw ErrorClass("OSResult error: setInstanceName");
594  //if(osresult->setJobID( osresultdata->jobID) != true)
595  // throw ErrorClass("OSResult error: setJobID");
596  //if(osresult->setGeneralMessage( osresultdata->message) != true)
597  // throw ErrorClass("OSResult error: setGeneralMessage");
598  // set basic problem parameters
600  throw ErrorClass("OSResult error: setVariableNumer");
601  if(osresult->setObjectiveNumber( 1) != true)
602  throw ErrorClass("OSResult error: setObjectiveNumber");
604  throw ErrorClass("OSResult error: setConstraintNumber");
605  if(osresult->setSolutionNumber( 1) != true)
606  throw ErrorClass("OSResult error: setSolutionNumer");
607  //
608  try{
609  double start = CoinCpuTime();
610  try{
611  if( sSolverName.find( "cbc") != std::string::npos){
612  //if( osinstance->getNumberOfIntegerVariables() + osinstance->getNumberOfBinaryVariables() > 0){
613  // just use simple branch and bound for anything but cbc
614  CbcModel model( *osiSolver);
615  CbcMain0( model);
616 
617 
618  // make sure we define cbc_argv if not done already when reading options
619  if(num_cbc_argv <= 0){
620  char *cstr;
621  std::string cbc_option;
622  num_cbc_argv = 4;
623  cbc_argv = new const char*[ num_cbc_argv];
624 
625  // the first option
626  cbc_option = "OS";
627  cstr = new char [cbc_option.size() + 1];
628  strcpy (cstr, cbc_option.c_str());
629  cbc_argv[ 0] = cstr;
630 
631 
632  // the log option -- by default minimal printing
633  cbc_option = "-log=0";
634  cstr = new char [cbc_option.size() + 1];
635  strcpy (cstr, cbc_option.c_str());
636  cbc_argv[ 1] = cstr;
637 
638 
639  // the solve option
640  cbc_option = "-solve";
641  cstr = new char [cbc_option.size() + 1];
642  strcpy (cstr, cbc_option.c_str());
643  cbc_argv[ 2] = cstr;
644 
645  // the quit option
646  cbc_option = "-quit";
647  cstr = new char [cbc_option.size() + 1];
648  strcpy (cstr, cbc_option.c_str());
649  cbc_argv[ 3] = cstr;
650 
651  }
652  std::cout << "CALLING THE CBC SOLVER CBCMAIN1()" << std::endl;
653  int i;
654  for(i = 0; i < num_cbc_argv; i++){
655  std::cout << "Cbc Option: " << cbc_argv[ i] << std::endl;
656  }
657  CbcMain1( num_cbc_argv, cbc_argv, model);
658 
659  //do the garbage collection on cbc_argv
660  for(i = 0; i < num_cbc_argv; i++){
661  delete[] cbc_argv[ i];
662  cbc_argv[i] = NULL;
663  }
664  delete[] cbc_argv;
665  cbc_argv = NULL;
666 
667 
668  // create a solver
669  OsiSolverInterface *solver = model.solver();
670  cpuTime = CoinCpuTime() - start;
671 
672  writeResult( solver);
673  }
674  else{ // use other solvers
675  //if an LP just do initial solve
677  osiSolver->branchAndBound();
678  }
679  else{
680  osiSolver->initialSolve();
681  }
682  cpuTime = CoinCpuTime() - start;
683 
685  }
686 
687 
688  }
689  catch(CoinError e){
690  std::string errmsg;
691  errmsg = "Coin Solver Error: " + e.message() + "\n" + " see method "
692  + e.methodName() + " in class " + e.className();
693  throw ErrorClass( errmsg );
694  }
695 
696  }
697  catch(const ErrorClass& eclass){
699  osresult->setGeneralStatusType( "error");
701  throw ;
702  }
703 } // end solve
704 
705 std::string CoinSolver::getCoinSolverType(std::string lcl_osol){
706 // this is deprecated, but keep it around
707  try{
708  if( lcl_osol.find( "clp") != std::string::npos){
709  return "coin_solver_glpk";
710  }
711  else{
712  if( lcl_osol.find( "cbc") != std::string::npos){
713  return "coin_solver_cpx";
714  }
715  else{
716  if( lcl_osol.find( "cpx") != std::string::npos){
717  return "coin_solver_clp";
718  }
719  else{
720  if(lcl_osol.find( "glpk") != std::string::npos){
721  return "";
722  }
723  else throw ErrorClass("a supported solver was not defined");
724  }
725  }
726  }
727  }
728  catch(const ErrorClass& eclass){
730  osresult->setGeneralStatusType( "error");
732  throw ;
733  }
734 } // end getCoinSolverType
735 
737  int i;
738  // print out problem parameters
739  cout << "This is problem: " << osinstance->getInstanceName() << endl;
740  cout << "The problem source is: " << osinstance->getInstanceSource() << endl;
741  cout << "The problem description is: " << osinstance->getInstanceDescription() << endl;
742  cout << "number of variables = " << osinstance->getVariableNumber() << endl;
743  cout << "number of Rows = " << osinstance->getConstraintNumber() << endl;
744 
745  // print out the variable information
746  if(osinstance->getVariableNumber() > 0){
747  for(i = 0; i < osinstance->getVariableNumber(); i++){
748  if(osinstance->getVariableNames() != NULL) cout << "variable Names " << osinstance->getVariableNames()[ i] << endl;
749  if(osinstance->getVariableTypes() != NULL) cout << "variable Types " << osinstance->getVariableTypes()[ i] << endl;
750  if(osinstance->getVariableLowerBounds() != NULL) cout << "variable Lower Bounds " << osinstance->getVariableLowerBounds()[ i] << endl;
751  if(osinstance->getVariableUpperBounds() != NULL) cout << "variable Upper Bounds " << osinstance->getVariableUpperBounds()[i] << endl;
752  }
753  }
754 
755  // print out objective function information
757  if( osinstance->getObjectiveMaxOrMins()[0] == "min") cout << "problem is a minimization" << endl;
758  else cout << "problem is a maximization" << endl;
759  for(i = 0; i < osinstance->getVariableNumber(); i++){
760  cout << "OBJ COEFFICIENT = " << osinstance->getDenseObjectiveCoefficients()[0][i] << endl;
761  }
762  }
763  // print out constraint information
764  if(osinstance->getConstraintNumber() > 0){
765  for(i = 0; i < osinstance->getConstraintNumber(); i++){
766  if(osinstance->getConstraintNames() != NULL) cout << "row name = " << osinstance->getConstraintNames()[i] << endl;
767  if(osinstance->getConstraintLowerBounds() != NULL) cout << "row lower bound = " << osinstance->getConstraintLowerBounds()[i] << endl;
768  if(osinstance->getConstraintUpperBounds() != NULL) cout << "row upper bound = " << osinstance->getConstraintUpperBounds()[i] << endl;
769  }
770  }
771 
772  // print out linear constraint data
773  if(m_CoinPackedMatrix != NULL) m_CoinPackedMatrix->dumpMatrix();
774 } // end dataEchoCheck
775 
776 
777 
778 void CoinSolver::writeResult(OsiSolverInterface *solver){
779  double *x = NULL;
780  double *y = NULL;
781  double *z = NULL;
782  int i = 0;
783  std::string *rcost = NULL;
784  int solIdx = 0;
785  int n, m;
786  std::string description = "";
787  osresult->setGeneralStatusType("normal");
788 // osresult->resultHeader->time = os_dtoa_format( cpuTime);
789  osresult->addTimingInformation("cpuTime","total","second","",cpuTime);
790  if (solver->isProvenOptimal() == true){
791  osresult->setSolutionStatus(solIdx, "optimal", description);
792  /* Retrieve the solution */
793  x = new double[osinstance->getVariableNumber() ];
794  y = new double[osinstance->getConstraintNumber() ];
795  z = new double[1];
798  rcost = new std::string[ osinstance->getVariableNumber()];
799  //
800  *(z + 0) = solver->getObjValue();
801  osresult->setObjectiveValues(solIdx, z, 1);
802  for(i=0; i < osinstance->getVariableNumber(); i++){
803  *(x + i) = solver->getColSolution()[i];
804  }
805  osresult->setPrimalVariableValues(solIdx, x, n);
806  // Symphony does not get dual prices
807  if( sSolverName.find( "symphony") == std::string::npos && osinstance->getNumberOfIntegerVariables() == 0 && osinstance->getNumberOfBinaryVariables() == 0) {
808  for(i=0; i < osinstance->getConstraintNumber(); i++){
809  *(y + i) = solver->getRowPrice()[ i];
810  }
812  }
813  //
814  //
815  // now put the reduced costs into the osrl
816  // Symphony does not get reduced costs
817  if( sSolverName.find( "symphony") == std::string::npos && osinstance->getNumberOfIntegerVariables() == 0 && osinstance->getNumberOfBinaryVariables() == 0){
818  int numberOfOtherVariableResults = 1;
819  int otherIdx = 0;
820  // first set the number of Other Variable Results
821  osresult->setNumberOfOtherVariableResults(solIdx, numberOfOtherVariableResults);
822  ostringstream outStr;
823  int numberOfVar = osinstance->getVariableNumber();
824  for(i=0; i < numberOfVar; i++){
825  rcost[ i] = os_dtoa_format( solver->getReducedCost()[ i]);
826  }
827  osresult->setAnOtherVariableResult(solIdx, otherIdx, "reduced costs", "the variable reduced costs", rcost, osinstance->getVariableNumber());
828  // end of settiing reduced costs
829  }
830  }
831  else{
832  if(solver->isProvenPrimalInfeasible() == true)
833  osresult->setSolutionStatus(solIdx, "infeasible", description);
834  else
835  if(solver->isProvenDualInfeasible() == true)
836  osresult->setSolutionStatus(solIdx, "dualinfeasible", description);
837  else
838  osresult->setSolutionStatus(solIdx, "other", description);
839  }
841  if(osinstance->getVariableNumber() > 0) delete[] x;
842  x = NULL;
843  if(osinstance->getConstraintNumber()) delete[] y;
844  y = NULL;
845  delete[] z;
846  z = NULL;
847  if(osinstance->getVariableNumber() > 0){
848  delete[] rcost;
849  rcost = NULL;
850  }
851 }
852 
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)
Definition: OSdtoa.cpp:2541
int getNumberOfInitVarValues()
Get the number of initial variable values.
Definition: OSOption.cpp:2051
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&#39;s dual variable values, where i equals the given solution index...
Definition: OSResult.cpp:1184
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.
Definition: OSResult.cpp:4712
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
Definition: OSErrorClass.h:42
OsiSolverInterface * osiSolver
osiSolver is the osi solver object – in this case clp, glpk, cbc, cplex, symphony or dylp ...
Definition: OSCoinSolver.h:93
int getLinearConstraintCoefficientNumber()
Get number of specified (usually nonzero) linear constraint coefficient values.
the InitVarValue class.
Definition: OSOption.h:1159
std::vector< SolverOption * > getSolverOptions(std::string solver_name)
Get the options associated with a given solver.
Definition: OSOption.cpp:4508
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&#39;s other (non-standard/solver specific)variable-related results...
Definition: OSResult.cpp:1080
The Result Class.
Definition: OSResult.h:2548
CoinPackedMatrix * m_CoinPackedMatrix
m_CoinPackedMatrix is a Coin Packed Matrix ojbect
Definition: OSCoinSolver.h:126
bool bCallbuildSolverInstance
bCallbuildSolverInstance is set to true if buildSolverService has been called
Take an OSResult object and write a string that validates against OSrL.
Definition: OSrLWriter.h:30
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.
Definition: OSResult.cpp:4721
bool setInstanceName(std::string instanceName)
Set instance name.
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
Definition: OSiLReader.cpp:53
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
Definition: OSrLWriter.cpp:45
int numberOfObjectives
numberOfObjectives is the number of objective functions in the instance
Definition: OSInstance.h:201
virtual void buildSolverInstance()
The implementation of the corresponding virtual function.
int idx
variable index
Definition: OSOption.h:1164
int * indexes
indexes holds an integer array of rowIdx (or colIdx) elements in coefMatrix (AMatrix).
Definition: OSGeneral.h:258
std::string osol
osol holds the options for the solver
bool setSolutionNumber(int number)
set the number of solutions.
Definition: OSResult.cpp:4740
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
double cpuTime
Definition: OSCoinSolver.h:140
bool setObjectiveValues(int solIdx, double *objectiveValues, int n)
Set the [i]th optimization solution&#39;s objective values, where i equals the given solution index...
Definition: OSResult.cpp:1130
Used to read an OSiL string.
Definition: OSiLReader.h:37
bool setNumberOfOtherVariableResults(int solIdx, int numberOfOtherVariableResults)
Set the [i]th optimization solution&#39;s other (non-standard/solver specific) variable-related results...
Definition: OSResult.cpp:5236
const char ** cbc_argv
when Cbc is the solver, these are the arguments sent to Cbc Solve
Definition: OSCoinSolver.h:134
void writeResult(OsiSolverInterface *solver)
OSiLReader * m_osilreader
m_osilreader is an OSiLReader object used to create an osinstance from an osil string if needed ...
Definition: OSCoinSolver.h:101
double lb
lb corresponds to the optional attribute that holds the variable lower bound.
Definition: OSInstance.h:56
bool setPrimalVariableValues(int solIdx, double *x, int n)
Set the [i]th optimization solution&#39;s primal variable values, where i equals the given solution index...
Definition: OSResult.cpp:1028
Variable ** var
Here we define a pointer to an array of var pointers.
Definition: OSInstance.h:97
int num_cbc_argv
the number of arguments in the argument list to the Cbc Solver
Definition: OSCoinSolver.h:138
SparseMatrix * getLinearConstraintCoefficientsInRowMajor()
Get linear constraint coefficients in row major.
double value
initial value
Definition: OSOption.h:1170
OSOption * osoption
osoption holds the solver options in-memory as an OSOption object
void fint fint * k
const double OSDBL_MAX
Definition: OSParameters.h:93
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.
Definition: OSInstance.h:61
Variables * variables
variables is a pointer to a Variables object
Definition: OSInstance.h:2185
OSoLReader * m_osolreader
m_osolreader is an OSoLReader object used to create an osoption from an osol string if needed ...
Definition: OSCoinSolver.h:107
SparseMatrix * getLinearConstraintCoefficientsInColumnMajor()
Get linear constraint coefficients in column major.
OSOption * readOSoL(const std::string &osol)
parse the OSoL solver options.
Definition: OSoLReader.cpp:76
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...
Definition: OSGeneral.h:264
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.
Definition: OSInstance.h:2278
OSrLWriter * osrlwriter
osrlwriter object used to write osrl from an OSResult object
Definition: OSCoinSolver.h:129
int getConstraintNumber()
Get number of constraints.
Objective ** obj
coef is pointer to an array of ObjCoef object pointers
Definition: OSInstance.h:205
bool getLinearConstraintCoefficientMajor()
Get whether the constraint coefficients is in column major (true) or row major (false).
bool setConstraintNumber(int constraintNumber)
Set the constraint number.
Definition: OSResult.cpp:4731
std::string sSolverName
sSolverName is the name of the Coin solver used, e.g.
int getNumberOfSolverOptions()
Get the number of solver options.
Definition: OSOption.cpp:2207
int getNumberOfQuadraticTerms()
Get the number of specified (usually nonzero) qTerms in the quadratic coefficients.
Used to read an OSoL string.
Definition: OSoLReader.h:37
Objectives * objectives
objectives is a pointer to a Objectives object
Definition: OSInstance.h:2188
std::string getInstanceSource()
Get instance source.
InitVarValue ** getInitVarValuesSparse()
Get the initial values associated with the variables in sparse form.
Definition: OSOption.cpp:2719
std::string os_dtoa_format(double x)
Definition: OSMathUtil.cpp:154
int * starts
starts holds an integer array of start elements in coefMatrix (AMatrix), which points to the start of...
Definition: OSGeneral.h:252
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.
Definition: OSResult.cpp:4487
void fint * m
virtual void solve()
The implementation of the corresponding virtual function.
std::string * getConstraintNames()
Get constraint names.
used for throwing exceptions.
Definition: OSErrorClass.h:31
void fint * n
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