/home/coin/SVN-release/OS-2.0.0/OS/examples/osSolverDemo/OSSolverDemo.cpp

Go to the documentation of this file.
00001 /* $Id: OSSolverDemo.cpp 2769 2009-07-02 03:56:50Z Gassmann $ */
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 
00042 #ifdef COIN_HAS_ASL
00043 #include "OSnl2osil.h"
00044 #endif
00045 
00046 #ifdef COIN_HAS_Bonmin   
00047 #include "OSBonminSolver.h"
00048 #endif
00049 
00050 #ifdef COIN_HAS_COUENNE    
00051 #include "OSCouenneSolver.h"
00052 #endif
00053 
00054 #ifdef COIN_HAS_IPOPT    
00055 #include "OSIpoptSolver.h"
00056 #endif
00057 
00058 
00059 using std::string;
00060 using std::cout;   
00061 using std::endl;
00062 
00063 void getOSResult(std::string osrl);
00064 
00065 //int main(int argC, char* argV[]){
00066 int main( ){
00067         WindowsErrorPopupBlocker();
00068         FileUtil *fileUtil = NULL; 
00069         fileUtil = new FileUtil();
00070         cout << "Start Building the Model" << endl;
00071         int i;
00072         try{
00073 
00074 
00075                 const char dirsep =  CoinFindDirSeparator();
00076                 std::string osil;
00077                 // Set directory containing mps data files.
00078                 std::string dataDir;
00079                 std::string osilFileName;
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                 /******************** Start Clp Example *************************/
00094                 std::cout << std::endl << std::endl;
00095                 std::cout << "CLP EXAMPLE" << std::endl;
00096                 
00097                 /******************** STEP 1 ************************
00098                 * Get an instance in mps format, and create an OSInstance object
00099                 */
00100                 std::string mpsFileName;
00101                 mpsFileName =  dataDir  + "mpsFiles" + dirsep +  "parinc.mps";
00102                 // convert to the OS native format
00103                 OSmps2osil *mps2osil = NULL;
00104                 mps2osil = new OSmps2osil( mpsFileName);
00105                 // create the first in-memory OSInstance
00106                 mps2osil->createOSInstance() ;
00107                 osinstance =  mps2osil->osinstance;
00108                 
00109                 /******************** STEP 2 ************************
00110                 * Create an OSOption object and give the solver options
00111                 */              
00112                 osoption = new OSOption();
00118                  // normally most output is turned off, here we turn it back on
00119                 osoption->setAnotherSolverOption("OsiHintTry","","osi","","OsiHintStrength","");
00120                 osoption->setAnotherSolverOption("OsiDoReducePrint","false","osi","","OsiHintParam","");
00121                 osolwriter = new OSoLWriter();
00122                 std::cout << osolwriter-> writeOSoL( osoption);
00123                 
00124                 
00125                 /******************** STEP 3 ************************
00126                 * Create the solver object -- for a CoinSolver we must specify
00127                 * which solver to use
00128                 */
00129                 solver = new CoinSolver();
00130                 solver->sSolverName ="clp"; 
00131                 
00132                 /******************** STEP 4 ************************
00133                 * Give the solver the instance and options and solve
00134                 */      
00135                 solver->osinstance = osinstance;
00136                 solver->osoption = osoption;    
00137                 solver->solve();
00138                 
00139                 
00140                 /******************** STEP 5 ************************
00141                 * Create a result object and get the optimal objective
00142                 * and primal variable values
00143                 */      
00144                 getOSResult( solver->osrl);
00145                 
00146                 
00147                 //do garbage collection
00148                 delete mps2osil;
00149                 mps2osil = NULL;
00150                 delete solver;
00151                 solver = NULL;
00152                 delete osoption;
00153                 osoption = NULL;
00154                 delete osolwriter;
00155                 osolwriter = NULL;
00156                 //finish garbage collection
00157 
00158                 
00159                 /******************** End Clp Example *************************/
00160                 
00161                 
00162                 /******************** Start Cbc Example *************************/
00163                 std::cout << std::endl << std::endl;
00164                 std::cout << "CBC EXAMPLE" << std::endl;
00165 
00166                 /******************** STEP 1 ************************
00167                 * Get an instance in native OSiL format and create an OSInstance object
00168                 */
00169                 osilFileName =  dataDir  + "osilFiles" + dirsep +  "p0033.osil";
00170                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
00171                 osilreader = new OSiLReader(); 
00172                 osinstance = osilreader->readOSiL( osil);
00173                 
00174                 /******************** STEP 2 ************************
00175                 * Create an OSOption object and give the solver options
00176                 */                      
00177                 osoption = new OSOption();
00183                 // tell Cbc to use the primal simplex algorithm
00184                 osoption->setAnotherSolverOption("primalS","","cbc","","string","");
00185                 //in primal simplex set the pivot choice -- use steepest edge
00186                 osoption->setAnotherSolverOption("primalpivot","steepest","cbc","","string","");
00187                 //set a high-level of log reporting
00188                 osoption->setAnotherSolverOption("log","10","cbc","","integer","");
00189                 osolwriter = new OSoLWriter();
00190                 std::cout << osolwriter-> writeOSoL( osoption);
00191                 
00192                 /******************** STEP 3 ************************
00193                 * Create the solver object -- for a CoinSolver we must specify
00194                 * which solver to use
00195                 */
00196                 solver = new CoinSolver();
00197                 solver->sSolverName ="cbc";
00198                 
00199                 /******************** STEP 4 ************************
00200                 * Give the solver the instance and options and solve
00201                 */                      
00202                 solver->osinstance = osinstance;
00203                 solver->osoption = osoption;    
00204                 solver->solve();
00205                 
00206                 /******************** STEP 5 ************************
00207                 * Create a result object and get the optimal objective
00208                 * and primal variable values
00209                 */      
00210                 getOSResult( solver->osrl);
00211                 
00212                 
00213                 // start garbage collection
00214                 delete osilreader;
00215                 osilreader = NULL;
00216                 delete solver;
00217                 solver = NULL;
00218                 delete osoption;
00219                 osoption = NULL;
00220                 delete osolwriter;
00221                 osolwriter = NULL;
00222                 // finish garbage collection
00223                 
00224                 /******************** End Cbc Example *************************/
00225                 
00226                 
00227                 /******************** Start SYMPHONY Example *************************/
00228                 std::cout << std::endl << std::endl;
00229                 std::cout << "SYMPHONY EXAMPLE" << std::endl;
00230                 
00231                 
00232                 /******************** STEP 1 ************************
00233                 * Get an instance in native OSiL format and create an OSInstance object
00234                 */
00235                 osilFileName =  dataDir  + "osilFiles" + dirsep +  "p0033.osil";
00236                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
00237                 osilreader = new OSiLReader(); 
00238                 osinstance = osilreader->readOSiL( osil);
00239 
00240                 /******************** STEP 2 ************************
00241                 * Create an OSOption object and give the solver options
00242                 */                      
00243                 osoption = new OSOption();
00250                 //turn on SYMPHONY output
00251                 osoption->setAnotherSolverOption("verbosity","0","symphony","","","");
00252                 osolwriter = new OSoLWriter();
00253                 std::cout << osolwriter-> writeOSoL( osoption);
00254                 
00255                 /******************** STEP 3 ************************
00256                 * Create the solver object -- for a CoinSolver we must specify
00257                 * which solver to use
00258                 */      
00259                 solver = new CoinSolver();
00260                 solver->sSolverName ="symphony"; 
00261 
00262                 /******************** STEP 4 ************************
00263                 * Give the solver the instance and options and solve
00264                 */                      
00265                 solver->osinstance = osinstance;
00266                 solver->osoption = osoption;    
00267                 solver->solve();
00268                 
00269                 /******************** STEP 5 ************************
00270                 * Create a result object and get the optimal objective
00271                 * and primal variable values
00272                 */      
00273                 getOSResult( solver->osrl);
00274                 // start garbage collection
00275                 delete osilreader;
00276                 osilreader = NULL;
00277                 delete solver;
00278                 solver = NULL;
00279                 delete osoption;
00280                 osoption = NULL;
00281                 delete osolwriter;
00282                 osolwriter = NULL;
00283                 // finish garbage collection
00284                 
00285 /******************** End SYMPHONY Example *************************/           
00286                 
00287                 
00288 #ifdef COIN_HAS_IPOPT           
00289                 /******************** Start Ipopt Example *************************/
00290                 
00291                 std::cout << std::endl << std::endl;
00292                 std::cout << "IPOPT EXAMPLE" << std::endl;
00293                 
00294                 /******************** STEP 1 ************************
00295                 * Get an instance in OSiL  format, and create an OSiL string
00296                 */
00297                 osilFileName =  dataDir  + "osilFiles" + dirsep +  "rosenbrockmod.osil";
00298                 //get an osil string
00299                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
00300 
00301                 /******************** STEP 2 ************************
00302                 * Create an OSOption object and give the solver options
00303                 */                      
00304                 osoption = new OSOption();
00311                 // set iteration limit
00312                 osoption->setAnotherSolverOption("max_iter","100","ipopt","","integer","");
00313                 osoption->setAnotherSolverOption("output_file","ipopt_out.txt","ipopt","","string","");
00314                 
00315                 
00316                 // set initial/starting values of 0 for the variables
00317                 numVar = 2; //rosenbrock mod has two variables 
00318                 xinitial = new double[numVar];
00319                 for(i = 0; i < numVar; i++){
00320                         xinitial[ i] = 1.0;
00321                 }
00322                 osoption->setInitVarValuesDense(numVar, xinitial);
00323                 osolwriter = new OSoLWriter();
00324                 std::cout << osolwriter-> writeOSoL( osoption);
00325                 
00326                 /******************** STEP 3 ************************
00327                 * Create the solver object
00328                 */                      
00329                 solver = new IpoptSolver();
00330                 
00331                 
00332                 /******************** STEP 4 ************************
00333                 * Give the solver the instance and options and solve
00334                 */      
00335                 solver->osil = osil;
00336                 solver->osoption = osoption;    
00337                 solver->solve();
00338                 
00339                 
00340                 /******************** STEP 5 ************************
00341                 * Create a result object and get the optimal objective
00342                 * and primal variable values
00343                 */      
00344                 getOSResult( solver->osrl);
00345                 
00346                 // start garbage collection
00347                 delete[] xinitial;
00348                 xinitial = NULL;
00349                 delete solver;
00350                 solver = NULL;
00351                 delete osoption;
00352                 osoption = NULL;
00353                 delete osolwriter;
00354                 osolwriter = NULL;
00355                 // finish garbage collection
00356                 
00357                 /******************** End Ipopt Example *************************/
00358                 
00359 #endif //end of  COIN_HAS_IPOPT 
00360 
00361 
00362 #ifdef COIN_HAS_BONMIN  
00363                 /******************** Start Bonmin Example *************************/
00364                 
00365                 std::cout << std::endl << std::endl;
00366                 std::cout << "BONMIN EXAMPLE" << std::endl;
00367                 
00368                 /******************** STEP 1 ************************
00369                 * Get an instance in OSiL  format, and create an OSInstance object
00370                 */
00371                 osilFileName =  dataDir  + "osilFiles" + dirsep +  "wayneQuadratic.osil";
00372                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
00373                 osilreader = new OSiLReader(); 
00374                 osinstance = osilreader->readOSiL( osil);
00375 
00376                 /******************** STEP 2 ************************
00377                 * Create an OSOption object and give the solver options
00378                 */                      
00379                 osoption = new OSOption();
00386                 // we are going to limit thenumber of nodes and terminate early
00387                 // set a limit of 0 nodes 
00388                 osoption->setAnotherSolverOption("node_limit","0","bonmin","","integer","");
00389 
00390                 
00391                 osolwriter = new OSoLWriter();
00392                 std::cout << osolwriter-> writeOSoL( osoption);
00393                 
00394                 /******************** STEP 3 ************************
00395                 * Create the solver object
00396                 */                      
00397                 solver = new BonminSolver();
00398                 
00399                 
00400                 /******************** STEP 4 ************************
00401                 * Give the solver the instance and options and solve
00402                 */      
00403                 solver->osinstance = osinstance;
00404                 solver->osoption = osoption;    
00405                 solver->solve();
00406                 
00407                 
00408                 /******************** STEP 5 ************************
00409                 * Create a result object and get the optimal objective
00410                 * and primal variable values
00411                 */      
00412                 getOSResult( solver->osrl);
00413 
00414                 // start garbage collection
00415                 delete osilreader;
00416                 osilreader = NULL;
00417                 delete solver;
00418                 solver = NULL;
00419                 delete osoption;
00420                 osoption = NULL;
00421                 delete osolwriter;
00422                 osolwriter = NULL;
00423                 // finish garbage collection
00424                 
00425                 /******************** End Bonmin Example *************************/
00426                 
00427 #endif //end of  COIN_HAS_BONMIN
00428 
00429                 
00430                 
00431         
00432 #if 1   
00433 #ifdef COIN_HAS_COUENNE         
00434                 /******************** Start Couenne Example *************************/
00435                 
00436                 std::cout << std::endl << std::endl;
00437                 std::cout << "COUENNE EXAMPLE" << std::endl;
00438                 
00439                 /******************** STEP 1 ************************
00440                 * Get an instance in AMPL nl  format, and create an OSInstance object
00441                 */
00442                 std::string nlFileName;
00443                 nlFileName =  dataDir  + "amplFiles" + dirsep +  "bonminEx1.nl";
00444                 // convert to the OS native format
00445                 OSnl2osil *nl2osil = NULL;
00446                 nl2osil = new OSnl2osil( nlFileName);
00447                 // create the first in-memory OSInstance
00448                 nl2osil->createOSInstance() ;
00449                 osinstance =  nl2osil->osinstance;
00450 
00451                 /******************** STEP 2 ************************
00452                 * Create an OSOption object and give the solver options
00453                 */                      
00454                 osoption = new OSOption();
00461                 // set Bonmin options through Couenne 
00462                 // set a limit of 50000 nodes -- this is on Cbc
00463                 osoption->setAnotherSolverOption("node_limit","50000","couenne","bonmin","integer","");
00464                 // control some Bonmin output
00465                 osoption->setAnotherSolverOption("bb_log_level","3","couenne","bonmin","integer","");
00466                 osoption->setAnotherSolverOption("nlp_log_level","2","couenne","bonmin","integer","");
00467                 //solve 3 times at each node and get best solution
00468                 osoption->setAnotherSolverOption("num_resolve_at_node","3","couenne","bonmin","integer","");
00469                 //solve 5 times at root node and get best solution
00470                 //osoption->setAnotherSolverOption("num_resolve_at_root","5","couenne","bonmin","integer","");
00471                 // set Ipopt options through Couenne
00472                 osoption->setAnotherSolverOption("max_iter","100","couenne","ipopt","integer","");
00473                 
00474                 // set a Couenne time limit option -- this seems to have no effect
00475                 osoption->setAnotherSolverOption("time_limit","100","couenne","","numeric","");
00476                 
00477 
00478                 numVar =osinstance->getVariableNumber();
00479                 xinitial = new double[numVar];
00480                 for(i = 0; i < numVar; i++){
00481                         xinitial[ i] = 0.0;
00482                 }
00483                 osoption->setInitVarValuesDense(numVar, xinitial);
00484                 osolwriter = new OSoLWriter();
00485                 std::cout << osolwriter-> writeOSoL( osoption);
00486                 
00487                 /******************** STEP 3 ************************
00488                 * Create the solver object
00489                 */                      
00490                 solver = new CouenneSolver();
00491                 
00492                 
00493                 /******************** STEP 4 ************************
00494                 * Give the solver the instance and options and solve
00495                 */      
00496                 solver->osinstance = osinstance;
00497                 solver->osoption = osoption;    
00498                 solver->solve();
00499                 
00500                 
00501                 /******************** STEP 5 ************************
00502                 * Create a result object and get the optimal objective
00503                 * and primal variable values
00504                 */      
00505                 getOSResult( solver->osrl);
00506                 
00507                 // start garbage collection
00508                 delete[] xinitial;
00509                 xinitial = NULL;
00510                 delete osilreader;
00511                 osilreader = NULL;
00512                 delete solver;
00513                 solver = NULL;
00514                 delete osoption;
00515                 osoption = NULL;
00516                 delete osolwriter;
00517                 osolwriter = NULL;
00518                 delete nl2osil;
00519                 nl2osil = NULL;
00520                 // finish garbage collection
00521                 
00522                 /******************** End Couenne Example *************************/
00523                 
00524 #endif //end of  COIN_HAS_COUENNE                       
00525 #endif //end #if 0/1    
00526 
00527                 
00528                 delete fileUtil;
00529                 fileUtil = NULL;        
00530                 return 0;
00531                 //
00532         }
00533         catch(const ErrorClass& eclass){
00534                 delete fileUtil;
00535                 std::cout << eclass.errormsg <<  std::endl;
00536                 return 0;
00537         } 
00538 }// end main
00539 
00540 
00541 //create result object and get solution information
00542 void getOSResult(std::string osrl){
00543 //see the example OSResultDemo for a more detailed example
00544         std::cout << std::endl << std::endl << std::endl;
00545         OSrLReader *osrlreader = NULL;
00546         OSResult *osresult = NULL;
00547         osrlreader = new OSrLReader();
00548         osresult  = osrlreader->readOSrL( osrl);
00549         
00550         //now use the OSResult API -- first make sure we got an optimal solution
00551         //get the status
00552         std::string solStatus;
00553         double optSolValue;
00554         // the argument is the solution index
00555         solStatus = osresult->getSolutionStatusType( 0 );
00556         // if solStatus is optimal get the optimal solution value
00557         if( solStatus.find("ptimal") != string::npos ){
00558         //first index is objIdx, second is solution index
00559                 optSolValue = osresult->getOptimalObjValue( -1, 0);
00560                 std::cout << "OPTIMAL SOLUTION VALUE  " <<  optSolValue << std::endl;
00561         }else{
00562                 std::cout << "NO OPTIMAL SOLUTION FOUND " << std::endl;
00563         }
00564         
00565         int i;
00566         int vecSize;
00567         // now get the primal solution
00568         std::vector<IndexValuePair*> primalValPair;
00569         primalValPair = osresult->getOptimalPrimalVariableValues( 0);
00570         vecSize = primalValPair.size();
00571         for(i = 0; i < vecSize; i++){
00572                 if(primalValPair[ i]->value > 0 || primalValPair[ i]->value < 0){
00573                         std::cout << "index = " <<  primalValPair[ i]->idx ;
00574                         std::cout << "    value = " <<  primalValPair[ i]->value << std::endl;
00575                 }
00576         }
00577         // write a description of the solution status
00578         
00579         
00580         std::cout << osresult->getSolutionStatusDescription( 0)<< std::endl;
00581         delete osrlreader;
00582 }// get OSResult
00583 

Generated on Mon Aug 3 03:02:22 2009 by  doxygen 1.4.7