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

Generated on Thu Sep 22 03:05:50 2011 by  doxygen 1.4.7