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

Generated on Fri Nov 19 13:31:03 2010 by  doxygen 1.4.7