- Main Page
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/OS-2.3.5/ApplicationTemplates/osSolverDemo/OSSolverDemo.cpp

Go to the documentation of this file.

    00001 /* $Id: OSSolverDemo.cpp 3300 2010-03-17 07:32:54Z kmartin $ */
    00017 #include "OSConfig.h"
    00018 #include "OSCoinSolver.h"
    00019 #include "OSIpoptSolver.h"
    00020 #include "OSResult.h"
    00021 #include "OSiLReader.h"
    00022 #include "OSiLWriter.h"
    00023 #include "OSrLReader.h"
    00024 #include "OSrLWriter.h"
    00025 #include "OSInstance.h"
    00026 #include "OSOption.h"
    00027 #include "OSoLWriter.h"
    00028 #include "OSFileUtil.h"
    00029 #include "OSDefaultSolver.h"
    00030 #include "OShL.h"
    00031 #include "OSErrorClass.h"
    00032 #include "OSmps2osil.h"
    00033 #include "OSBase64.h"
    00034 #include "OSErrorClass.h"
    00035 #include "OSMathUtil.h"
    00036 #include "CoinError.hpp"
    00037 #include "CoinHelperFunctions.hpp"
    00038 #include<iostream>
    00039
    00040
    00041 #ifdef COIN_HAS_ASL
    00042 #include "OSnl2osil.h"
    00043 #endif
    00044
    00045 #ifdef COIN_HAS_BONMIN
    00046 #include "OSBonminSolver.h"
    00047 #endif
    00048
    00049 #ifdef COIN_HAS_COUENNE
    00050 #include "OSCouenneSolver.h"
    00051 #endif
    00052
    00053 #ifdef COIN_HAS_IPOPT
    00054 #include "OSIpoptSolver.h"
    00055 #endif
    00056
    00057
    00058 using std::string;
    00059 using std::cout;
    00060 using std::endl;
    00061
    00062 void getOSResult(std::string osrl);
    00063
    00064 //int main(int argC, char* argV[]){
    00065 int main( ){
    00066         WindowsErrorPopupBlocker();
    00067         FileUtil *fileUtil = NULL;
    00068         fileUtil = new FileUtil();
    00069         cout << "Start Building the Model" << endl;
    00070         int i;
    00071         try{
    00072
    00073
    00074                 const char dirsep =  CoinFindDirSeparator();
    00075                 std::string osil;
    00076                 // Set directory containing mps data files.
    00077                 std::string dataDir;
    00078                 std::string osilFileName;
    00079                 //dataDir = dirsep == '/' ? "../../data/" : "..\\..\\data\\";
    00080                 dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
    00081                 // first declare a generic solver
    00082                 DefaultSolver *solver  = NULL;
    00083
    00084                 OSiLReader *osilreader = NULL;
    00085                 OSInstance *osinstance = NULL;
    00086                 OSoLWriter *osolwriter = NULL;
    00087                 OSOption* osoption = NULL;
    00088
    00089                 // set initial/starting values of 0 for the variables
    00090                 double* xinitial = NULL;
    00091                 int numVar;
    00092
    00093
    00094
    00095                 /******************** Start Clp Example *************************/
    00096                 std::cout << std::endl << std::endl;
    00097                 std::cout << "CLP EXAMPLE" << std::endl;
    00098
    00099                 /******************** STEP 1 ************************
    00100                 * Get an instance in mps format, and create an OSInstance object
    00101                 */
    00102                 std::string mpsFileName;
    00103                 mpsFileName =  dataDir  +  "parinc.mps";
    00104                 // convert to the OS native format
    00105                 OSmps2osil *mps2osil = NULL;
    00106                 mps2osil = new OSmps2osil( mpsFileName);
    00107                 // create the first in-memory OSInstance
    00108                 mps2osil->createOSInstance() ;
    00109                 osinstance =  mps2osil->osinstance;
    00110
    00111                 /******************** STEP 2 ************************
    00112                 * Create an OSOption object and give the solver options
    00113                 */
    00114                 osoption = new OSOption();
    00120                  // normally most output is turned off, here we turn it back on
    00121                 osoption->setAnotherSolverOption("OsiHintTry","","osi","","OsiHintStrength","");
    00122                 osoption->setAnotherSolverOption("OsiDoReducePrint","false","osi","","OsiHintParam","");
    00123                 osolwriter = new OSoLWriter();
    00124                 std::cout << osolwriter-> writeOSoL( osoption);
    00125
    00126
    00127                 /******************** STEP 3 ************************
    00128                 * Create the solver object -- for a CoinSolver we must specify
    00129                 * which solver to use
    00130                 */
    00131                 solver = new CoinSolver();
    00132                 solver->sSolverName ="clp";
    00133
    00134                 /******************** STEP 4 ************************
    00135                 * Give the solver the instance and options and solve
    00136                 */
    00137                 solver->osinstance = osinstance;
    00138                 solver->osoption = osoption;
    00139                 solver->solve();
    00140
    00141
    00142                 /******************** STEP 5 ************************
    00143                 * Create a result object and get the optimal objective
    00144                 * and primal variable values
    00145                 */
    00146                 //getOSResult( solver->osrl);
    00147
    00148                 //OSResult *osr = solver->osresult;
    00149                 //int numOfBasisVar = osr->getNumberOfBasisVar(0);
    00150                 //std::cout << "NUMBER OF BASIS VARS = " << numOfBasisVar << std::endl;
    00151
    00152
    00153
    00154
    00155                 //do garbage collection
    00156                 delete mps2osil;
    00157                 mps2osil = NULL;
    00158                 delete solver;
    00159                 solver = NULL;
    00160                 delete osoption;
    00161                 osoption = NULL;
    00162                 delete osolwriter;
    00163                 osolwriter = NULL;
    00164                 //finish garbage collection
    00165
    00166
    00167                 /******************** End Clp Example *************************/
    00168
    00169
    00170
    00171
    00172
    00173
    00174
    00175
    00176
    00177
    00178
    00179
    00180
    00181
    00182
    00183
    00184
    00185
    00186
    00187
    00188                 /******************** Start Cbc Example *************************/
    00189                 std::cout << std::endl << std::endl;
    00190                 std::cout << "CBC EXAMPLE" << std::endl;
    00191
    00192                 /******************** STEP 1 ************************
    00193                 * Get an instance in native OSiL format and create an OSInstance object
    00194                 */
    00195                 osilFileName =   dataDir +  "p0033.osil";
    00196                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
    00197                 osilreader = new OSiLReader();
    00198                 osinstance = osilreader->readOSiL( osil);
    00199
    00200                 /******************** STEP 2 ************************
    00201                 * Create an OSOption object and give the solver options
    00202                 */
    00203                 osoption = new OSOption();
    00209                 // tell Cbc to use the primal simplex algorithm
    00210                 osoption->setAnotherSolverOption("primalS","","cbc","","string","");
    00211                 //in primal simplex set the pivot choice -- use steepest edge
    00212                 osoption->setAnotherSolverOption("primalpivot","steepest","cbc","","string","");
    00213                 //set a high-level of log reporting
    00214                 osoption->setAnotherSolverOption("log","10","cbc","","integer","");
    00215                 osolwriter = new OSoLWriter();
    00216                 std::cout << osolwriter-> writeOSoL( osoption);
    00217
    00218                 /******************** STEP 3 ************************
    00219                 * Create the solver object -- for a CoinSolver we must specify
    00220                 * which solver to use
    00221                 */
    00222                 solver = new CoinSolver();
    00223                 solver->sSolverName ="cbc";
    00224
    00225                 /******************** STEP 4 ************************
    00226                 * Give the solver the instance and options and solve
    00227                 */
    00228                 solver->osinstance = osinstance;
    00229                 //solver->osoption = osoption;
    00230                 solver->solve();
    00231
    00232                 /******************** STEP 5 ************************
    00233                 * Create a result object and get the optimal objective
    00234                 * and primal variable values
    00235                 */
    00236                 getOSResult( solver->osrl);
    00237
    00238
    00239                 // start garbage collection
    00240                 delete osilreader;
    00241                 osilreader = NULL;
    00242                 delete solver;
    00243                 solver = NULL;
    00244                 delete osoption;
    00245                 osoption = NULL;
    00246                 delete osolwriter;
    00247                 osolwriter = NULL;
    00248                 // finish garbage collection
    00249
    00250                 /******************** End Cbc Example *************************/
    00251
    00252
    00253         #if 1
    00254 #ifdef COIN_HAS_COUENNE
    00255                 /******************** Start Couenne Example *************************/
    00256
    00257                 std::cout << std::endl << std::endl;
    00258                 std::cout << "COUENNE EXAMPLE" << std::endl;
    00259
    00260                 /******************** STEP 1 ************************
    00261                  * Get an instance in AMPL nl  format, and create an OSInstance object
    00262                  */
    00263                 std::string nlFileName;
    00264                 nlFileName =  dataDir  +   "bonminEx1.nl";
    00265                 // convert to the OS native format
    00266                 OSnl2osil *nl2osil = NULL;
    00267                 nl2osil = new OSnl2osil( nlFileName);
    00268                 // create the first in-memory OSInstance
    00269                 nl2osil->createOSInstance() ;
    00270                 osinstance =  nl2osil->osinstance;
    00271
    00272                 /******************** STEP 2 ************************
    00273                  * Create an OSOption object and give the solver options
    00274                  */
    00275         osoption = new OSOption();
    00282         // set Bonmin options through Couenne
    00283         // set a limit of 50000 nodes -- this is on Cbc
    00284         osoption->setAnotherSolverOption("node_limit","50000","couenne","bonmin","integer","");
    00285         // control some Bonmin output
    00286         osoption->setAnotherSolverOption("bb_log_level","3","couenne","bonmin","integer","");
    00287         osoption->setAnotherSolverOption("nlp_log_level","2","couenne","bonmin","integer","");
    00288         //solve 3 times at each node and get best solution
    00289         osoption->setAnotherSolverOption("num_resolve_at_node","3","couenne","bonmin","integer","");
    00290         //solve 5 times at root node and get best solution
    00291         //osoption->setAnotherSolverOption("num_resolve_at_root","5","couenne","bonmin","integer","");
    00292         // set Ipopt options through Couenne
    00293         osoption->setAnotherSolverOption("max_iter","100","couenne","ipopt","integer","");
    00294
    00295         // set a Couenne time limit option -- this seems to have no effect
    00296         osoption->setAnotherSolverOption("time_limit","100","couenne","","numeric","");
    00297
    00298
    00299         numVar =osinstance->getVariableNumber();
    00300         xinitial = new double[numVar];
    00301         for(i = 0; i < numVar; i++){
    00302             xinitial[ i] = 0.0;
    00303         }
    00304         osoption->setInitVarValuesDense(numVar, xinitial);
    00305         osolwriter = new OSoLWriter();
    00306         std::cout << osolwriter-> writeOSoL( osoption);
    00307
    00308                 /******************** STEP 3 ************************
    00309                  * Create the solver object
    00310                  */
    00311                 solver = new CouenneSolver();
    00312
    00313
    00314                 /******************** STEP 4 ************************
    00315                  * Give the solver the instance and options and solve
    00316                  */
    00317
    00318                 solver->osinstance = osinstance;
    00319                 //solver->osoption = osoption;
    00320                 solver->osol = "";
    00321                 solver->buildSolverInstance();
    00322                 solver->setSolverOptions();
    00323                 solver->solve() ;
    00324
    00325
    00326
    00327                 /******************** STEP 5 ************************
    00328                  * Create a result object and get the optimal objective
    00329                  * and primal variable values
    00330                  */
    00331                 std::cout  << "call get osresult" << std::endl;
    00332                 std::cout << solver->osrl << std::endl;
    00333                 getOSResult( solver->osrl);
    00334
    00335                 // start garbage collection
    00336                 delete[] xinitial;
    00337                 xinitial = NULL;
    00338                 delete osilreader;
    00339                 osilreader = NULL;
    00340                 delete solver;
    00341                 solver = NULL;
    00342                 delete osoption;
    00343                 osoption = NULL;
    00344                 delete osolwriter;
    00345                 osolwriter = NULL;
    00346                 delete nl2osil;
    00347                 nl2osil = NULL;
    00348                 // finish garbage collection
    00349
    00350                 /******************** End Couenne Example *************************/
    00351
    00352 #endif //end of  COIN_HAS_COUENNE
    00353 #endif //end #if 0/1
    00354
    00355
    00356
    00357
    00358                 /******************** Start SYMPHONY Example *************************/
    00359                 std::cout << std::endl << std::endl;
    00360                 std::cout << "SYMPHONY EXAMPLE" << std::endl;
    00361
    00362
    00363                 /******************** STEP 1 ************************
    00364                 * Get an instance in native OSiL format and create an OSInstance object
    00365                 */
    00366                 osilFileName =  dataDir +  "p0033.osil";
    00367                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
    00368                 osilreader = new OSiLReader();
    00369                 osinstance = osilreader->readOSiL( osil);
    00370
    00371                 /******************** STEP 2 ************************
    00372                 * Create an OSOption object and give the solver options
    00373                 */
    00374                 osoption = new OSOption();
    00381                 //turn on SYMPHONY output
    00382                 osoption->setAnotherSolverOption("verbosity","0","symphony","","","");
    00383                 osolwriter = new OSoLWriter();
    00384                 std::cout << osolwriter-> writeOSoL( osoption);
    00385
    00386                 /******************** STEP 3 ************************
    00387                 * Create the solver object -- for a CoinSolver we must specify
    00388                 * which solver to use
    00389                 */
    00390                 solver = new CoinSolver();
    00391                 solver->sSolverName ="symphony";
    00392
    00393                 /******************** STEP 4 ************************
    00394                 * Give the solver the instance and options and solve
    00395                 */
    00396                 solver->osinstance = osinstance;
    00397                 solver->osoption = osoption;
    00398                 solver->solve();
    00399
    00400                 /******************** STEP 5 ************************
    00401                 * Create a result object and get the optimal objective
    00402                 * and primal variable values
    00403                 */
    00404                 getOSResult( solver->osrl);
    00405                 // start garbage collection
    00406                 delete osilreader;
    00407                 osilreader = NULL;
    00408                 delete solver;
    00409                 solver = NULL;
    00410                 delete osoption;
    00411                 osoption = NULL;
    00412                 delete osolwriter;
    00413                 osolwriter = NULL;
    00414                 //finish garbage collection
    00415 //
    00416 /******************** End SYMPHONY Example *************************/
    00417
    00418
    00419
    00420
    00421
    00422 #ifdef COIN_HAS_IPOPT
    00423                 /******************** Start Ipopt Example *************************/
    00424
    00425                 std::cout << std::endl << std::endl;
    00426                 std::cout << "IPOPT EXAMPLE" << std::endl;
    00427
    00428                 /******************** STEP 1 ************************
    00429                 * Get an instance in OSiL  format, and create an OSiL string
    00430                 */
    00431                 osilFileName =  dataDir  +   "rosenbrockmod.osil";
    00432                 //get an osil string
    00433                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
    00434
    00435                 /******************** STEP 2 ************************
    00436                 * Create an OSOption object and give the solver options
    00437                 */
    00438                 osoption = new OSOption();
    00445                 // set iteration limit
    00446                 osoption->setAnotherSolverOption("max_iter","100","ipopt","","integer","");
    00447                 osoption->setAnotherSolverOption("output_file","ipopt_out.txt","ipopt","","string","");
    00448
    00449
    00450                 // set initial/starting values of 0 for the variables
    00451                 numVar = 2; //rosenbrock mod has two variables
    00452                 xinitial = new double[numVar];
    00453                 for(i = 0; i < numVar; i++){
    00454                         xinitial[ i] = 1.0;
    00455                 }
    00456                 osoption->setInitVarValuesDense(numVar, xinitial);
    00457                 osolwriter = new OSoLWriter();
    00458                 std::cout << osolwriter-> writeOSoL( osoption);
    00459
    00460                 /******************** STEP 3 ************************
    00461                 * Create the solver object
    00462                 */
    00463                 solver = new IpoptSolver();
    00464
    00465
    00466                 /******************** STEP 4 ************************
    00467                 * Give the solver the instance and options and solve
    00468                 */
    00469                 solver->osil = osil;
    00470                 solver->osoption = osoption;
    00471                 solver->solve();
    00472
    00473
    00474                 /******************** STEP 5 ************************
    00475                 * Create a result object and get the optimal objective
    00476                 * and primal variable values
    00477                 */
    00478
    00479
    00480                 getOSResult( solver->osrl);
    00481
    00482
    00483                 // start garbage collection
    00484                 delete[] xinitial;
    00485                 xinitial = NULL;
    00486                 delete solver;
    00487                 solver = NULL;
    00488                 delete osoption;
    00489                 osoption = NULL;
    00490                 delete osolwriter;
    00491                 osolwriter = NULL;
    00492                 // finish garbage collection
    00493
    00494                 /******************** End Ipopt Example *************************/
    00495
    00496
    00497
    00498 #endif //end of  COIN_HAS_IPOPT
    00499
    00500
    00501 #ifdef COIN_HAS_BONMIN
    00502                 /******************** Start Bonmin Example *************************/
    00503
    00504                 std::cout << std::endl << std::endl;
    00505                 std::cout << "BONMIN EXAMPLE" << std::endl;
    00506
    00507                 /******************** STEP 1 ************************
    00508                 * Get an instance in OSiL  format, and create an OSInstance object
    00509                 */
    00510                 osilFileName =  dataDir  +   "wayneQuadratic.osil";
    00511                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
    00512                 osilreader = new OSiLReader();
    00513                 osinstance = osilreader->readOSiL( osil);
    00514
    00515                 /******************** STEP 2 ************************
    00516                 * Create an OSOption object and give the solver options
    00517                 */
    00518                 osoption = new OSOption();
    00525                 // we are going to limit thenumber of nodes and terminate early
    00526                 // set a limit of 0 nodes
    00527                 osoption->setAnotherSolverOption("node_limit","0","bonmin","","integer","");
    00528
    00529
    00530                 osolwriter = new OSoLWriter();
    00531                 std::cout << osolwriter-> writeOSoL( osoption);
    00532
    00533                 /******************** STEP 3 ************************
    00534                 * Create the solver object
    00535                 */
    00536                 solver = new BonminSolver();
    00537
    00538
    00539                 /******************** STEP 4 ************************
    00540                 * Give the solver the instance and options and solve
    00541                 */
    00542                 solver->osinstance = osinstance;
    00543                 solver->osoption = osoption;
    00544                 solver->solve();
    00545
    00546
    00547                 /******************** STEP 5 ************************
    00548                 * Create a result object and get the optimal objective
    00549                 * and primal variable values
    00550                 */
    00551                 std::cout  << "call get osresult" << std::endl;
    00552                 std::cout << solver->osrl << std::endl;
    00553                 getOSResult( solver->osrl);
    00554                 std::cout  << "finish call get osresult" << std::endl;
    00555                 // start garbage collection
    00556                 delete osilreader;
    00557                 osilreader = NULL;
    00558                 delete solver;
    00559                 solver = NULL;
    00560                 delete osoption;
    00561                 osoption = NULL;
    00562                 delete osolwriter;
    00563                 osolwriter = NULL;
    00564                 // finish garbage collection
    00565
    00566                 /******************** End Bonmin Example *************************/
    00567
    00568 #endif //end of  COIN_HAS_BONMIN
    00569
    00570                 return 0;
    00571                 delete fileUtil;
    00572                 fileUtil = NULL;
    00573                 return 0;
    00574                 //
    00575         }
    00576         catch(const ErrorClass& eclass){
    00577                 delete fileUtil;
    00578                 std::cout << eclass.errormsg <<  std::endl;
    00579                 return 0;
    00580         }
    00581 }// end main
    00582
    00583
    00584 //create result object and get solution information
    00585 void getOSResult(std::string osrl){
    00586 //see the example OSResultDemo for a more detailed example
    00587         std::cout << std::endl << std::endl << std::endl;
    00588         OSrLReader *osrlreader = NULL;
    00589         OSResult *osresult = NULL;
    00590         osrlreader = new OSrLReader();
    00591         osresult  = osrlreader->readOSrL( osrl);
    00592
    00593         //now use the OSResult API -- first make sure we got an optimal solution
    00594         //get the status
    00595         std::string solStatus;
    00596         double optSolValue;
    00597         // the argument is the solution index
    00598         solStatus = osresult->getSolutionStatusType( 0 );
    00599         // if solStatus is optimal get the optimal solution value
    00600         if( solStatus.find("ptimal") != string::npos ){
    00601         //first index is objIdx, second is solution index
    00602                 optSolValue = osresult->getOptimalObjValue( -1, 0);
    00603                 std::cout << "OPTIMAL SOLUTION VALUE  " <<  optSolValue << std::endl;
    00604         }else{
    00605                 std::cout << "NO OPTIMAL SOLUTION FOUND " << std::endl;
    00606         }
    00607
    00608         int i;
    00609         int vecSize;
    00610         // now get the primal solution
    00611         std::vector<IndexValuePair*> primalValPair;
    00612         primalValPair = osresult->getOptimalPrimalVariableValues( 0);
    00613         vecSize = primalValPair.size();
    00614         for(i = 0; i < vecSize; i++){
    00615                 if(primalValPair[ i]->value > 0 || primalValPair[ i]->value < 0){
    00616                         std::cout << "index = " <<  primalValPair[ i]->idx ;
    00617                         std::cout << "    value = " <<  primalValPair[ i]->value << std::endl;
    00618                 }
    00619         }
    00620         // write a description of the solution status
    00621
    00622
    00623         std::cout << osresult->getSolutionStatusDescription( 0)<< std::endl;
    00624         delete osrlreader;
    00625 }// get OSResult
    00626