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