OSSolverDemo.cpp
Go to the documentation of this file.
1 /* $Id: OSSolverDemo.cpp 3300 2010-03-17 07:32:54Z kmartin $ */
15 #include "OSConfig.h"
16 #include "OSCoinSolver.h"
17 #include "OSIpoptSolver.h"
18 #include "OSResult.h"
19 #include "OSiLReader.h"
20 #include "OSiLWriter.h"
21 #include "OSrLReader.h"
22 #include "OSrLWriter.h"
23 #include "OSInstance.h"
24 #include "OSOption.h"
25 #include "OSoLWriter.h"
26 #include "OSFileUtil.h"
27 #include "OSDefaultSolver.h"
28 #include "OShL.h"
29 #include "OSErrorClass.h"
30 #include "OSmps2osil.h"
31 #include "OSnl2osil.h"
32 #include "OSBase64.h"
33 #include "OSErrorClass.h"
34 #include "OSMathUtil.h"
35 #include "CoinError.hpp"
36 #include "CoinHelperFunctions.hpp"
37 #include <iostream>
38 
39 
40 
41 #ifdef OS_HAS_ASL
42 #include "OSnl2osil.h"
43 #endif
44 
45 #ifdef OS_HAS_BONMIN
46 #include "OSBonminSolver.h"
47 #endif
48 
49 #ifdef OS_HAS_COUENNE
50 #include "OSCouenneSolver.h"
51 #endif
52 
53 #ifdef OS_HAS_IPOPT
54 #include "OSIpoptSolver.h"
55 #endif
56 
57 
58 using std::string;
59 using std::cout;
60 using std::endl;
61 
62 void getOSResult(std::string osrl);
63 
64 //int main(int argC, char* argV[]){
65 int main( ){
66  WindowsErrorPopupBlocker();
67  FileUtil *fileUtil = NULL;
68  fileUtil = new FileUtil();
69  cout << "Start Building the Model" << endl;
70  //int i;
71  try{
72 
73 
74  const char dirsep = CoinFindDirSeparator();
75  std::string osil;
76  // Set directory containing mps data files.
77  std::string dataDir;
78  std::string osilFileName;
79  //dataDir = dirsep == '/' ? "../../data/" : "..\\..\\data\\";
80  dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
81  // first declare a generic solver
82  DefaultSolver *solver = NULL;
83 
84  OSiLReader *osilreader = NULL;
85  OSInstance *osinstance = NULL;
86  OSoLWriter *osolwriter = NULL;
87  OSOption* osoption = NULL;
88 
89  // set initial/starting values of 0 for the variables
90  //double* xinitial = NULL;
91  int numVar;
92  double *xinitial;
93  int i;
94 
95 
96 
97  /******************** Start Clp Example *************************/
98  std::cout << std::endl << std::endl;
99  std::cout << "CLP EXAMPLE" << std::endl;
100 
101  /******************** STEP 1 ************************
102  * Get an instance in mps format, and create an OSInstance object
103  */
104  std::string mpsFileName;
105  mpsFileName = dataDir + "mpsFiles" + dirsep + "parinc.mps";
106  // convert to the OS native format
107  OSmps2osil *mps2osil = NULL;
108  mps2osil = new OSmps2osil( mpsFileName);
109  // create the first in-memory OSInstance
110  mps2osil->createOSInstance() ;
111  osinstance = mps2osil->osinstance;
112 
113  /******************** STEP 2 ************************
114  * Create an OSOption object and give the solver options
115  */
116  osoption = new OSOption();
122  // normally most output is turned off, here we turn it back on
123  osoption->setAnotherSolverOption("OsiHintTry","","osi","","OsiHintStrength","");
124  osoption->setAnotherSolverOption("OsiDoReducePrint","false","osi","","OsiHintParam","");
125  osolwriter = new OSoLWriter();
126  std::cout << osolwriter-> writeOSoL( osoption);
127 
128 
129  /******************** STEP 3 ************************
130  * Create the solver object -- for a CoinSolver we must specify
131  * which solver to use
132  */
133  solver = new CoinSolver();
134  solver->sSolverName ="clp";
135 
136  /******************** STEP 4 ************************
137  * Give the solver the instance and options and solve
138  */
139  solver->osinstance = osinstance;
140  solver->osoption = osoption;
141  solver->solve();
142 
143 
144  /******************** STEP 5 ************************
145  * Create a result object and get the optimal objective
146  * and primal variable values
147  */
148  //getOSResult( solver->osrl);
149 
150  //OSResult *osr = solver->osresult;
151  //int numOfBasisVar = osr->getNumberOfBasisVar(0);
152  //std::cout << "NUMBER OF BASIS VARS = " << numOfBasisVar << std::endl;
153 
154 
155 
156 
157  //do garbage collection
158  delete mps2osil;
159  mps2osil = NULL;
160  delete solver;
161  solver = NULL;
162  delete osoption;
163  osoption = NULL;
164  delete osolwriter;
165  osolwriter = NULL;
166  //finish garbage collection
167 
168 
169  /******************** End Clp Example *************************/
170 
171 
172 
173 
174  /******************** Start Cbc Example *************************/
175  std::cout << std::endl << std::endl;
176  std::cout << "CBC EXAMPLE" << std::endl;
177 
178  /******************** STEP 1 ************************
179  * Get an instance in native OSiL format and create an OSInstance object
180  */
181  osilFileName = dataDir + "osilFiles" + dirsep + "p0033.osil";
182  osil = fileUtil->getFileAsString( osilFileName.c_str() );
183  osilreader = new OSiLReader();
184  osinstance = osilreader->readOSiL( osil);
185 
186  /******************** STEP 2 ************************
187  * Create an OSOption object and give the solver options
188  */
189  osoption = new OSOption();
195  // tell Cbc limit the number of nodes in the branch and bound tree
196  osoption->setAnotherSolverOption("maxN","1000","cbc","","integer","");
197  // tell Cbc limit the number of seconds
198  osoption->setAnotherSolverOption("sec",".01","cbc","","integer","");
199  // tell Cbc not to use cutting planes
200  osoption->setAnotherSolverOption("cuts","off","cbc","","string","");
201  //set a high-level of log reporting
202  osoption->setAnotherSolverOption("log","10","cbc","","integer","");
203  osolwriter = new OSoLWriter();
204  std::cout << osolwriter-> writeOSoL( osoption);
205 
206  /******************** STEP 3 ************************
207  * Create the solver object -- for a CoinSolver we must specify
208  * which solver to use
209  */
210  solver = new CoinSolver();
211  solver->sSolverName ="cbc";
212 
213  /******************** STEP 4 ************************
214  * Give the solver the instance and options and solve
215  */
216  solver->osinstance = osinstance;
217  solver->osoption = osoption;
218  solver->solve();
219 
220  /******************** STEP 5 ************************
221  * Create a result object and get the optimal objective
222  * and primal variable values
223  */
224  getOSResult( solver->osrl);
225 
226 
227  // start garbage collection
228  delete osilreader;
229  osilreader = NULL;
230  delete solver;
231  solver = NULL;
232  delete osoption;
233  osoption = NULL;
234  delete osolwriter;
235  osolwriter = NULL;
236  // finish garbage collection
237 
238  //return 0;
239 
240  /******************** End Cbc Example *************************/
241 
242 
243  #if 1
244 #ifdef OS_HAS_COUENNE
245  /******************** Start Couenne Example *************************/
246 
247  std::cout << std::endl << std::endl;
248  std::cout << "COUENNE EXAMPLE" << std::endl;
249 
250  /******************** STEP 1 ************************
251  * Get an instance in AMPL nl format, and create an OSInstance object
252  */
253  std::string nlFileName;
254  nlFileName = dataDir + "amplFiles" + dirsep + "bonminEx1.nl";
255  // convert to the OS native format
256  OSnl2osil *nl2osil = NULL;
257  nl2osil = new OSnl2osil( nlFileName);
258  // create the first in-memory OSInstance
259  nl2osil->createOSInstance() ;
260  osinstance = nl2osil->osinstance;
261 
262  /******************** STEP 2 ************************
263  * Create an OSOption object and give the solver options
264  */
265  osoption = new OSOption();
272  // set Bonmin options through Couenne
273  // set a limit of 50000 nodes -- this is on Cbc
274  osoption->setAnotherSolverOption("node_limit","50000","couenne","bonmin","integer","");
275  // control some Bonmin output
276  osoption->setAnotherSolverOption("bb_log_level","3","couenne","bonmin","integer","");
277  osoption->setAnotherSolverOption("nlp_log_level","2","couenne","bonmin","integer","");
278  //solve 3 times at each node and get best solution
279  osoption->setAnotherSolverOption("num_resolve_at_node","3","couenne","bonmin","integer","");
280  //solve 5 times at root node and get best solution
281  //osoption->setAnotherSolverOption("num_resolve_at_root","5","couenne","bonmin","integer","");
282  // set Ipopt options through Couenne
283  osoption->setAnotherSolverOption("max_iter","100","couenne","ipopt","integer","");
284 
285  // set a Couenne time limit option -- this seems to have no effect
286  osoption->setAnotherSolverOption("time_limit","100","couenne","","numeric","");
287 
288 
289  numVar =osinstance->getVariableNumber();
290  xinitial = new double[numVar];
291  for(i = 0; i < numVar; i++){
292  xinitial[ i] = 0.0;
293  }
294  osoption->setInitVarValuesDense(numVar, xinitial);
295  osolwriter = new OSoLWriter();
296  std::cout << osolwriter-> writeOSoL( osoption);
297 
298  /******************** STEP 3 ************************
299  * Create the solver object
300  */
301  solver = new CouenneSolver();
302 
303 
304  /******************** STEP 4 ************************
305  * Give the solver the instance and options and solve
306  */
307 
308  solver->osinstance = osinstance;
309  //solver->osoption = osoption;
310  solver->osol = "";
311  solver->buildSolverInstance();
312  solver->setSolverOptions();
313  solver->solve() ;
314 
315 
316 
317  /******************** STEP 5 ************************
318  * Create a result object and get the optimal objective
319  * and primal variable values
320  */
321  std::cout << "call get osresult" << std::endl;
322  std::cout << solver->osrl << std::endl;
323  getOSResult( solver->osrl);
324 
325  // start garbage collection
326  delete[] xinitial;
327  xinitial = NULL;
328  delete osilreader;
329  osilreader = NULL;
330  delete solver;
331  solver = NULL;
332  delete osoption;
333  osoption = NULL;
334  delete osolwriter;
335  osolwriter = NULL;
336  delete nl2osil;
337  nl2osil = NULL;
338  // finish garbage collection
339 
340  /******************** End Couenne Example *************************/
341 
342 #endif //end of OS_HAS_COUENNE
343 #endif //end #if 0/1
344 
345 
346 
347 
348  /******************** Start SYMPHONY Example *************************/
349  std::cout << std::endl << std::endl;
350  std::cout << "SYMPHONY EXAMPLE" << std::endl;
351 
352 
353  /******************** STEP 1 ************************
354  * Get an instance in native OSiL format and create an OSInstance object
355  */
356  osilFileName = dataDir + "osilFiles" + dirsep + "p0033.osil";
357  osil = fileUtil->getFileAsString( osilFileName.c_str() );
358  osilreader = new OSiLReader();
359  osinstance = osilreader->readOSiL( osil);
360 
361  /******************** STEP 2 ************************
362  * Create an OSOption object and give the solver options
363  */
364  osoption = new OSOption();
371  //turn on SYMPHONY output
372  osoption->setAnotherSolverOption("verbosity","0","symphony","","","");
373  osolwriter = new OSoLWriter();
374  std::cout << osolwriter-> writeOSoL( osoption);
375 
376  /******************** STEP 3 ************************
377  * Create the solver object -- for a CoinSolver we must specify
378  * which solver to use
379  */
380  solver = new CoinSolver();
381  solver->sSolverName ="symphony";
382 
383  /******************** STEP 4 ************************
384  * Give the solver the instance and options and solve
385  */
386  solver->osinstance = osinstance;
387  solver->osoption = osoption;
388  solver->solve();
389 
390  /******************** STEP 5 ************************
391  * Create a result object and get the optimal objective
392  * and primal variable values
393  */
394  getOSResult( solver->osrl);
395  // start garbage collection
396  delete osilreader;
397  osilreader = NULL;
398  delete solver;
399  solver = NULL;
400  delete osoption;
401  osoption = NULL;
402  delete osolwriter;
403  osolwriter = NULL;
404  //finish garbage collection
405 //
406 /******************** End SYMPHONY Example *************************/
407 
408 
409 
410 
411 
412 #ifdef OS_HAS_IPOPT
413  /******************** Start Ipopt Example *************************/
414 
415  std::cout << std::endl << std::endl;
416  std::cout << "IPOPT EXAMPLE" << std::endl;
417 
418  /******************** STEP 1 ************************
419  * Get an instance in OSiL format, and create an OSiL string
420  */
421  osilFileName = dataDir + "osilFiles" + dirsep + "rosenbrockmod.osil";
422  //get an osil string
423  osil = fileUtil->getFileAsString( osilFileName.c_str() );
424 
425  /******************** STEP 2 ************************
426  * Create an OSOption object and give the solver options
427  */
428  osoption = new OSOption();
435  // set iteration limit
436  osoption->setAnotherSolverOption("max_iter","100","ipopt","","integer","");
437  osoption->setAnotherSolverOption("output_file","ipopt_out.txt","ipopt","","string","");
438 
439 
440  // set initial/starting values of 0 for the variables
441  numVar = 2; //rosenbrock mod has two variables
442  xinitial = new double[numVar];
443  for(i = 0; i < numVar; i++){
444  xinitial[ i] = 1.0;
445  }
446  osoption->setInitVarValuesDense(numVar, xinitial);
447  osolwriter = new OSoLWriter();
448  std::cout << osolwriter-> writeOSoL( osoption);
449 
450  /******************** STEP 3 ************************
451  * Create the solver object
452  */
453  solver = new IpoptSolver();
454 
455 
456  /******************** STEP 4 ************************
457  * Give the solver the instance and options and solve
458  */
459  solver->osil = osil;
460  solver->osoption = osoption;
461  solver->solve();
462 
463 
464  /******************** STEP 5 ************************
465  * Create a result object and get the optimal objective
466  * and primal variable values
467  */
468 
469 
470  getOSResult( solver->osrl);
471 
472 
473  // start garbage collection
474  delete[] xinitial;
475  xinitial = NULL;
476  delete solver;
477  solver = NULL;
478  delete osoption;
479  osoption = NULL;
480  delete osolwriter;
481  osolwriter = NULL;
482  // finish garbage collection
483 
484  /******************** End Ipopt Example *************************/
485 
486 
487 
488 #endif //end of OS_HAS_IPOPT
489 
490 
491 #ifdef OS_HAS_BONMIN
492  /******************** Start Bonmin Example *************************/
493 
494  std::cout << std::endl << std::endl;
495  std::cout << "BONMIN EXAMPLE" << std::endl;
496 
497  /******************** STEP 1 ************************
498  * Get an instance in OSiL format, and create an OSInstance object
499  */
500  osilFileName = dataDir + "osilFiles" + dirsep + "wayneQuadratic.osil";
501  osil = fileUtil->getFileAsString( osilFileName.c_str() );
502  osilreader = new OSiLReader();
503  osinstance = osilreader->readOSiL( osil);
504 
505  /******************** STEP 2 ************************
506  * Create an OSOption object and give the solver options
507  */
508  osoption = new OSOption();
515  // we are going to limit thenumber of nodes and terminate early
516  // set a limit of 0 nodes
517  osoption->setAnotherSolverOption("node_limit","0","bonmin","","integer","");
518 
519 
520  osolwriter = new OSoLWriter();
521  std::cout << osolwriter-> writeOSoL( osoption);
522 
523  /******************** STEP 3 ************************
524  * Create the solver object
525  */
526  solver = new BonminSolver();
527 
528 
529  /******************** STEP 4 ************************
530  * Give the solver the instance and options and solve
531  */
532  solver->osinstance = osinstance;
533  solver->osoption = osoption;
534  solver->solve();
535 
536 
537  /******************** STEP 5 ************************
538  * Create a result object and get the optimal objective
539  * and primal variable values
540  */
541  std::cout << "call get osresult" << std::endl;
542  std::cout << solver->osrl << std::endl;
543  getOSResult( solver->osrl);
544  std::cout << "finish call get osresult" << std::endl;
545  // start garbage collection
546  delete osilreader;
547  osilreader = NULL;
548  delete solver;
549  solver = NULL;
550  delete osoption;
551  osoption = NULL;
552  delete osolwriter;
553  osolwriter = NULL;
554  // finish garbage collection
555 
556  /******************** End Bonmin Example *************************/
557 
558 #endif //end of OS_HAS_BONMIN
559 
560 
561  delete fileUtil;
562  fileUtil = NULL;
563  std::cout << "\nSolverDemo COMPLETED WITHOUT ERROR\n";
564  return 0;
565  //
566  }
567  catch(const ErrorClass& eclass){
568  delete fileUtil;
569  std::cout << eclass.errormsg << std::endl;
570  std::cout << "\nSolverDemo COMPLETED, BUT THERE WERE ERRORS\n";
571  return 0;
572  }
573 }// end main
574 
575 
576 //create result object and get solution information
577 void getOSResult(std::string osrl){
578 //see the example OSResultDemo for a more detailed example
579  std::cout << std::endl << std::endl << std::endl;
580  OSrLReader *osrlreader = NULL;
581  OSResult *osresult = NULL;
582  osrlreader = new OSrLReader();
583  osresult = osrlreader->readOSrL( osrl);
584 
585  //now use the OSResult API -- first make sure we got an optimal solution
586  //get the status
587  std::string solStatus;
588  double optSolValue;
589  // the argument is the solution index
590  solStatus = osresult->getSolutionStatusType( 0 );
591  // if solStatus is optimal get the optimal solution value
592  if( solStatus.find("ptimal") != string::npos ){
593  //first index is objIdx, second is solution index
594  optSolValue = osresult->getOptimalObjValue( -1, 0);
595  std::cout << "OPTIMAL SOLUTION VALUE " << optSolValue << std::endl;
596  }else{
597  std::cout << std::endl;
598  std::cout << "We are not optimal!" << std::endl;
599  std::cout << solStatus << std::endl;
600  }
601 
602  int i;
603  int vecSize;
604  // now get the primal solution
605  std::vector<IndexValuePair*> primalValPair;
606  primalValPair = osresult->getOptimalPrimalVariableValues( 0);
607  vecSize = primalValPair.size();
608  for(i = 0; i < vecSize; i++){
609  if(primalValPair[ i]->value > 0 || primalValPair[ i]->value < 0){
610  std::cout << "index = " << primalValPair[ i]->idx ;
611  std::cout << " value = " << primalValPair[ i]->value << std::endl;
612  }
613  }
614  // write a description of the solution status
615 
616 
617  std::cout << osresult->getSolutionStatusDescription( 0)<< std::endl;
618  delete osrlreader;
619 }// get OSResult
620 
std::string getSolutionStatusType(int solIdx)
Get the [i]th optimization solution status type, where i equals the given solution index...
Definition: OSResult.cpp:2051
The CouenneSolver class solves problems using Ipopt.
int getVariableNumber()
Get number of variables.
bool setAnotherSolverOption(std::string name, std::string value, std::string solver, std::string category, std::string type, std::string description)
Definition: OSOption.cpp:9509
std::string osrl
osrl holds the solution or result of the model
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
int main(int argc, char *argv[])
Definition: BB_tm.cpp:32
std::vector< IndexValuePair * > getOptimalPrimalVariableValues(int solIdx)
Get one solution of optimal primal variable values.
Definition: OSResult.cpp:2215
bool setInitVarValuesDense(int numberOfVar, double *value)
Definition: OSOption.cpp:8429
OSResult * readOSrL(const std::string &posrl)
Get an OSResult object from an OSrL string.
Definition: OSrLReader.cpp:97
The Result Class.
Definition: OSResult.h:2548
OSOption * osoption
double getOptimalObjValue(int objIdx, int solIdx)
Get one solution&#39;s optimal objective value.
Definition: OSResult.cpp:3065
Take an OSOption object and write a string that validates against the OSoL schema.
Definition: OSoLWriter.h:29
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
Definition: OSiLReader.cpp:53
The IpoptSolver class solves problems using Ipopt.
void getOSResult(std::string osrl)
The Option Class.
Definition: OSOption.h:3564
std::string osol
osol holds the options for the solver
virtual void solve()=0
solve is a virtual function – the actual solvers will implement their own solve method ...
Used to read an OSiL string.
Definition: OSiLReader.h:37
OSResult * osresult
OSOption * osoption
osoption holds the solver options in-memory as an OSOption object
The Default Solver Class.
The OSmps2osil Class.
Definition: OSmps2osil.h:39
OSInstance * osinstance
osinstance holds the problem instance in-memory as an OSInstance object
std::string sSolverName
sSolverName is the name of the Coin solver used, e.g.
bool createOSInstance()
create an OSInstance from the MPS instance representation
Definition: OSmps2osil.cpp:143
virtual void setSolverOptions()=0
setSolverOptions is a virtual function – the actual solvers will implement their own setSolverOptions...
std::string getSolutionStatusDescription(int solIdx)
Get the [i]th optimization solution status description, where i equals the given solution index...
Definition: OSResult.cpp:2062
Implements a solve method for the Coin solvers.
Definition: OSCoinSolver.h:37
std::string getFileAsString(const char *fname)
read a file and return contents as a string.
Definition: OSFileUtil.cpp:35
The OSrLReader Class.
Definition: OSrLReader.h:42
The BonminSolver class solves problems using Ipopt.
virtual void buildSolverInstance()=0
buildSolverInstance is a virtual function – the actual solvers will implement their own buildSolverIn...
The in-memory representation of an OSiL instance..
Definition: OSInstance.h:2262
OSInstance * osinstance
class used to make it easy to read and write files.
Definition: OSFileUtil.h:37
OSInstance * osinstance
osinstance is a pointer to the OSInstance object that gets created from the instance represented in N...
Definition: OSmps2osil.h:58
used for throwing exceptions.
Definition: OSErrorClass.h:31