/home/coin/SVN-release/OS-2.1.1/OS/examples/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                 // 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  + "mpsFiles" + dirsep +  "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                 
00149                 //do garbage collection
00150                 delete mps2osil;
00151                 mps2osil = NULL;
00152                 delete solver;
00153                 solver = NULL;
00154                 delete osoption;
00155                 osoption = NULL;
00156                 delete osolwriter;
00157                 osolwriter = NULL;
00158                 //finish garbage collection
00159 
00160                 
00161                 /******************** End Clp Example *************************/
00162                 
00163                 
00164                 
00165                 
00166                 
00167         
00168                 
00169                 
00170                 
00171                 
00172                 
00173                 
00174                 
00175                 
00176                 
00177                 
00178                 
00179                 
00180                 
00181                 
00182                 /******************** Start Cbc Example *************************/
00183                 std::cout << std::endl << std::endl;
00184                 std::cout << "CBC EXAMPLE" << std::endl;
00185 
00186                 /******************** STEP 1 ************************
00187                 * Get an instance in native OSiL format and create an OSInstance object
00188                 */
00189                 osilFileName =  dataDir  + "osilFiles" + dirsep +  "p0033.osil";
00190                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
00191                 osilreader = new OSiLReader(); 
00192                 osinstance = osilreader->readOSiL( osil);
00193                 
00194                 /******************** STEP 2 ************************
00195                 * Create an OSOption object and give the solver options
00196                 */                      
00197                 osoption = new OSOption();
00203                 // tell Cbc to use the primal simplex algorithm
00204                 osoption->setAnotherSolverOption("primalS","","cbc","","string","");
00205                 //in primal simplex set the pivot choice -- use steepest edge
00206                 osoption->setAnotherSolverOption("primalpivot","steepest","cbc","","string","");
00207                 //set a high-level of log reporting
00208                 osoption->setAnotherSolverOption("log","10","cbc","","integer","");
00209                 osolwriter = new OSoLWriter();
00210                 std::cout << osolwriter-> writeOSoL( osoption);
00211                 
00212                 /******************** STEP 3 ************************
00213                 * Create the solver object -- for a CoinSolver we must specify
00214                 * which solver to use
00215                 */
00216                 solver = new CoinSolver();
00217                 solver->sSolverName ="cbc";
00218                 
00219                 /******************** STEP 4 ************************
00220                 * Give the solver the instance and options and solve
00221                 */                      
00222                 solver->osinstance = osinstance;
00223                 //solver->osoption = osoption;  
00224                 solver->solve();
00225                 
00226                 /******************** STEP 5 ************************
00227                 * Create a result object and get the optimal objective
00228                 * and primal variable values
00229                 */      
00230                 getOSResult( solver->osrl);
00231                 
00232                 
00233                 // start garbage collection
00234                 delete osilreader;
00235                 osilreader = NULL;
00236                 delete solver;
00237                 solver = NULL;
00238                 delete osoption;
00239                 osoption = NULL;
00240                 delete osolwriter;
00241                 osolwriter = NULL;
00242                 // finish garbage collection
00243                 
00244                 /******************** End Cbc Example *************************/
00245                 
00246                 
00247         #if 1   
00248 #ifdef COIN_HAS_COUENNE         
00249                 /******************** Start Couenne Example *************************/
00250                 
00251                 std::cout << std::endl << std::endl;
00252                 std::cout << "COUENNE EXAMPLE" << std::endl;
00253                 
00254                 /******************** STEP 1 ************************
00255                  * Get an instance in AMPL nl  format, and create an OSInstance object
00256                  */
00257                 std::string nlFileName;
00258                 nlFileName =  dataDir  + "amplFiles" + dirsep +  "bonminEx1.nl";
00259                 // convert to the OS native format
00260                 OSnl2osil *nl2osil = NULL;
00261                 nl2osil = new OSnl2osil( nlFileName);
00262                 // create the first in-memory OSInstance
00263                 nl2osil->createOSInstance() ;
00264                 osinstance =  nl2osil->osinstance;
00265                 
00266                 /******************** STEP 2 ************************
00267                  * Create an OSOption object and give the solver options
00268                  */                     
00269         osoption = new OSOption();
00276         // set Bonmin options through Couenne 
00277         // set a limit of 50000 nodes -- this is on Cbc
00278         osoption->setAnotherSolverOption("node_limit","50000","couenne","bonmin","integer","");
00279         // control some Bonmin output
00280         osoption->setAnotherSolverOption("bb_log_level","3","couenne","bonmin","integer","");
00281         osoption->setAnotherSolverOption("nlp_log_level","2","couenne","bonmin","integer","");
00282         //solve 3 times at each node and get best solution
00283         osoption->setAnotherSolverOption("num_resolve_at_node","3","couenne","bonmin","integer","");
00284         //solve 5 times at root node and get best solution
00285         //osoption->setAnotherSolverOption("num_resolve_at_root","5","couenne","bonmin","integer","");
00286         // set Ipopt options through Couenne
00287         osoption->setAnotherSolverOption("max_iter","100","couenne","ipopt","integer","");
00288 
00289         // set a Couenne time limit option -- this seems to have no effect
00290         osoption->setAnotherSolverOption("time_limit","100","couenne","","numeric","");
00291 
00292 
00293         numVar =osinstance->getVariableNumber();
00294         xinitial = new double[numVar];
00295         for(i = 0; i < numVar; i++){
00296             xinitial[ i] = 0.0;
00297         }
00298         osoption->setInitVarValuesDense(numVar, xinitial);
00299         osolwriter = new OSoLWriter();
00300         std::cout << osolwriter-> writeOSoL( osoption);
00301                 
00302                 /******************** STEP 3 ************************
00303                  * Create the solver object
00304                  */                     
00305                 solver = new CouenneSolver();
00306                 
00307                 
00308                 /******************** STEP 4 ************************
00309                  * Give the solver the instance and options and solve
00310                  */     
00311                 
00312                 solver->osinstance = osinstance;
00313                 //solver->osoption = osoption;  
00314                 solver->osol = "";
00315                 solver->buildSolverInstance();
00316                 solver->setSolverOptions();
00317                 solver->solve() ;
00318                 
00319                 
00320                 
00321                 /******************** STEP 5 ************************
00322                  * Create a result object and get the optimal objective
00323                  * and primal variable values
00324                  */     
00325                 std::cout  << "call get osresult" << std::endl;
00326                 std::cout << solver->osrl << std::endl;
00327                 getOSResult( solver->osrl);
00328                 
00329                 // start garbage collection
00330                 delete[] xinitial;
00331                 xinitial = NULL;
00332                 delete osilreader;
00333                 osilreader = NULL;
00334                 delete solver;
00335                 solver = NULL;
00336                 delete osoption;
00337                 osoption = NULL;
00338                 delete osolwriter;
00339                 osolwriter = NULL;
00340                 delete nl2osil;
00341                 nl2osil = NULL;
00342                 // finish garbage collection
00343                 
00344                 /******************** End Couenne Example *************************/
00345                 
00346 #endif //end of  COIN_HAS_COUENNE                       
00347 #endif //end #if 0/1            
00348                 
00349                 
00350                 
00351                 
00352                 /******************** Start SYMPHONY Example *************************/
00353                 std::cout << std::endl << std::endl;
00354                 std::cout << "SYMPHONY EXAMPLE" << std::endl;
00355                 
00356                 
00357                 /******************** STEP 1 ************************
00358                 * Get an instance in native OSiL format and create an OSInstance object
00359                 */
00360                 osilFileName =  dataDir  + "osilFiles" + dirsep +  "p0033.osil";
00361                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
00362                 osilreader = new OSiLReader(); 
00363                 osinstance = osilreader->readOSiL( osil);
00364 
00365                 /******************** STEP 2 ************************
00366                 * Create an OSOption object and give the solver options
00367                 */                      
00368                 osoption = new OSOption();
00375                 //turn on SYMPHONY output
00376                 osoption->setAnotherSolverOption("verbosity","0","symphony","","","");
00377                 osolwriter = new OSoLWriter();
00378                 std::cout << osolwriter-> writeOSoL( osoption);
00379                 
00380                 /******************** STEP 3 ************************
00381                 * Create the solver object -- for a CoinSolver we must specify
00382                 * which solver to use
00383                 */      
00384                 solver = new CoinSolver();
00385                 solver->sSolverName ="symphony"; 
00386 
00387                 /******************** STEP 4 ************************
00388                 * Give the solver the instance and options and solve
00389                 */                      
00390                 solver->osinstance = osinstance;
00391                 solver->osoption = osoption;    
00392                 solver->solve();
00393                 
00394                 /******************** STEP 5 ************************
00395                 * Create a result object and get the optimal objective
00396                 * and primal variable values
00397                 */      
00398                 getOSResult( solver->osrl);
00399                 // start garbage collection
00400                 delete osilreader;
00401                 osilreader = NULL;
00402                 delete solver;
00403                 solver = NULL;
00404                 delete osoption;
00405                 osoption = NULL;
00406                 delete osolwriter;
00407                 osolwriter = NULL;
00408                 //finish garbage collection
00409 //              
00410 /******************** End SYMPHONY Example *************************/   
00411                 
00412                 
00413                 
00414                         
00415 
00416 #ifdef COIN_HAS_IPOPT           
00417                 /******************** Start Ipopt Example *************************/
00418                 
00419                 std::cout << std::endl << std::endl;
00420                 std::cout << "IPOPT EXAMPLE" << std::endl;
00421                 
00422                 /******************** STEP 1 ************************
00423                 * Get an instance in OSiL  format, and create an OSiL string
00424                 */
00425                 osilFileName =  dataDir  + "osilFiles" + dirsep +  "rosenbrockmod.osil";
00426                 //get an osil string
00427                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
00428 
00429                 /******************** STEP 2 ************************
00430                 * Create an OSOption object and give the solver options
00431                 */                      
00432                 osoption = new OSOption();
00439                 // set iteration limit
00440                 osoption->setAnotherSolverOption("max_iter","100","ipopt","","integer","");
00441                 osoption->setAnotherSolverOption("output_file","ipopt_out.txt","ipopt","","string","");
00442                 
00443                 
00444                 // set initial/starting values of 0 for the variables
00445                 numVar = 2; //rosenbrock mod has two variables 
00446                 xinitial = new double[numVar];
00447                 for(i = 0; i < numVar; i++){
00448                         xinitial[ i] = 1.0;
00449                 }
00450                 osoption->setInitVarValuesDense(numVar, xinitial);
00451                 osolwriter = new OSoLWriter();
00452                 std::cout << osolwriter-> writeOSoL( osoption);
00453                 
00454                 /******************** STEP 3 ************************
00455                 * Create the solver object
00456                 */                      
00457                 solver = new IpoptSolver();
00458                 
00459                 
00460                 /******************** STEP 4 ************************
00461                 * Give the solver the instance and options and solve
00462                 */      
00463                 solver->osil = osil;
00464                 solver->osoption = osoption;    
00465                 solver->solve();
00466                 
00467                 
00468                 /******************** STEP 5 ************************
00469                 * Create a result object and get the optimal objective
00470                 * and primal variable values
00471                 */      
00472                 getOSResult( solver->osrl);
00473                 
00474                 // start garbage collection
00475                 delete[] xinitial;
00476                 xinitial = NULL;
00477                 delete solver;
00478                 solver = NULL;
00479                 delete osoption;
00480                 osoption = NULL;
00481                 delete osolwriter;
00482                 osolwriter = NULL;
00483                 // finish garbage collection
00484                 
00485                 /******************** End Ipopt Example *************************/
00486         
00487                 
00488                 
00489 #endif //end of  COIN_HAS_IPOPT 
00490 
00491 
00492 #ifdef COIN_HAS_BONMIN  
00493                 /******************** Start Bonmin Example *************************/
00494                 
00495                 std::cout << std::endl << std::endl;
00496                 std::cout << "BONMIN EXAMPLE" << std::endl;
00497                 
00498                 /******************** STEP 1 ************************
00499                 * Get an instance in OSiL  format, and create an OSInstance object
00500                 */
00501                 osilFileName =  dataDir  + "osilFiles" + dirsep +  "wayneQuadratic.osil";
00502                 osil = fileUtil->getFileAsString( osilFileName.c_str() );
00503                 osilreader = new OSiLReader(); 
00504                 osinstance = osilreader->readOSiL( osil);
00505 
00506                 /******************** STEP 2 ************************
00507                 * Create an OSOption object and give the solver options
00508                 */                      
00509                 osoption = new OSOption();
00516                 // we are going to limit thenumber of nodes and terminate early
00517                 // set a limit of 0 nodes 
00518                 osoption->setAnotherSolverOption("node_limit","0","bonmin","","integer","");
00519 
00520                 
00521                 osolwriter = new OSoLWriter();
00522                 std::cout << osolwriter-> writeOSoL( osoption);
00523                 
00524                 /******************** STEP 3 ************************
00525                 * Create the solver object
00526                 */                      
00527                 solver = new BonminSolver();
00528                 
00529                 
00530                 /******************** STEP 4 ************************
00531                 * Give the solver the instance and options and solve
00532                 */      
00533                 solver->osinstance = osinstance;
00534                 solver->osoption = osoption;    
00535                 solver->solve();
00536                 
00537                 
00538                 /******************** STEP 5 ************************
00539                 * Create a result object and get the optimal objective
00540                 * and primal variable values
00541                 */      
00542                 std::cout  << "call get osresult" << std::endl;
00543                 std::cout << solver->osrl << std::endl;
00544                 getOSResult( solver->osrl);
00545                 std::cout  << "finish call get osresult" << std::endl;
00546                 // start garbage collection
00547                 delete osilreader;
00548                 osilreader = NULL;
00549                 delete solver;
00550                 solver = NULL;
00551                 delete osoption;
00552                 osoption = NULL;
00553                 delete osolwriter;
00554                 osolwriter = NULL;
00555                 // finish garbage collection
00556                 
00557                 /******************** End Bonmin Example *************************/
00558                 
00559 #endif //end of  COIN_HAS_BONMIN
00560         
00561                 return 0;               
00562                 delete fileUtil;
00563                 fileUtil = NULL;        
00564                 return 0;
00565                 //
00566         }
00567         catch(const ErrorClass& eclass){
00568                 delete fileUtil;
00569                 std::cout << eclass.errormsg <<  std::endl;
00570                 return 0;
00571         } 
00572 }// end main
00573 
00574 
00575 //create result object and get solution information
00576 void getOSResult(std::string osrl){
00577 //see the example OSResultDemo for a more detailed example
00578         std::cout << std::endl << std::endl << std::endl;
00579         OSrLReader *osrlreader = NULL;
00580         OSResult *osresult = NULL;
00581         osrlreader = new OSrLReader();
00582         osresult  = osrlreader->readOSrL( osrl);
00583         
00584         //now use the OSResult API -- first make sure we got an optimal solution
00585         //get the status
00586         std::string solStatus;
00587         double optSolValue;
00588         // the argument is the solution index
00589         solStatus = osresult->getSolutionStatusType( 0 );
00590         // if solStatus is optimal get the optimal solution value
00591         if( solStatus.find("ptimal") != string::npos ){
00592         //first index is objIdx, second is solution index
00593                 optSolValue = osresult->getOptimalObjValue( -1, 0);
00594                 std::cout << "OPTIMAL SOLUTION VALUE  " <<  optSolValue << std::endl;
00595         }else{
00596                 std::cout << "NO OPTIMAL SOLUTION FOUND " << 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 Mon May 3 03:05:23 2010 by  doxygen 1.4.7