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

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

Generated on Thu Nov 10 03:05:46 2011 by  doxygen 1.4.7