/home/coin/SVN-release/OS-1.0.0/OS/test/unitTest.cpp

Go to the documentation of this file.
00001 
00099 #include <cppad/cppad.hpp> 
00100 #include "OSResult.h" 
00101 #include "OSiLReader.h"        
00102 #include "OSiLWriter.h" 
00103 #include "OSrLReader.h"          
00104 #include "OSrLWriter.h"      
00105 #include "OSInstance.h"  
00106 #include "FileUtil.h"  
00107 #include "OSConfig.h" 
00108 #include "CoinSolver.h"
00109 #include "DefaultSolver.h"  
00110 #include "WSUtil.h" 
00111 #include "OSSolverAgent.h"   
00112 #include "OShL.h"     
00113 #include "ErrorClass.h"
00114 #include "OSmps2osil.h"   
00115 #include "Base64.h"
00116 #include "CommonUtil.h"
00117     
00118  
00119 #ifdef COIN_HAS_KNITRO    
00120 #include "KnitroSolver.h"
00121 #endif 
00122 
00123 #ifdef COIN_HAS_GLPK
00124 #include <OsiGlpkSolverInterface.hpp>
00125 #endif
00126 
00127 
00128 #ifdef COIN_HAS_ASL
00129 #include "OSnl2osil.h"
00130 #endif
00131 #ifdef COIN_HAS_LINDO    
00132 #include "LindoSolver.h"
00133 #endif  
00134 #ifdef COIN_HAS_IPOPT    
00135 #include "IpoptSolver.h"
00136 #endif 
00137  
00138 
00139 
00140 #ifdef HAVE_CTIME
00141 # include <ctime>
00142 #else
00143 # ifdef HAVE_TIME_H
00144 #  include <time.h>
00145 # else
00146 #  error "don't have header file for time"
00147 # endif
00148 #endif
00149 
00150 
00151 using std::cout;   
00152 using std::endl;
00153 using std::ostringstream; 
00154 
00155 int main(int argC, char* argV[])
00156 {
00157         double getObjVal(std::string osrl);
00158         using CppAD::NearEqual;
00159         bool ok;
00160         double check;
00161         cout << "START UNIT TEST" << endl;
00162         // define the classes
00163         FileUtil *fileUtil = NULL;  
00164         #ifdef COIN_HAS_ASL
00165         OSnl2osil *nl2osil = NULL;
00166         #endif 
00167         OSmps2osil *mps2osil = NULL;
00168         DefaultSolver *solver  = NULL;
00169         // end classes    
00170         std::string osilFileName;
00171         std::string osrlFileName;
00172         std::string nlFileName; 
00173         std::string mpsFileName;     
00174         std::string osil;
00175         ostringstream unitTestResult;
00176         ostringstream unitTestResultFailure;
00177         // get the input files
00178          const char dirsep =  CoinFindDirSeparator();
00179         // Set directory containing mps data files.
00180         std::string dataDir;
00181     dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
00182         std::string osol = "<osol></osol>";
00183         nlFileName =  dataDir  + "amplFiles" + dirsep + "parinc.nl";
00184         mpsFileName =  dataDir + "mpsFiles" + dirsep + "parinc.mps";
00185         fileUtil = new FileUtil();
00186         //
00187         unitTestResult << "HERE ARE THE UNIT TEST RESULTS:" << std::endl << std::endl;
00188         
00189         // solve using using the osil file
00190         #ifdef COIN_HAS_IPOPT
00191         cout << "create a new IPOPT Solver for OSiL string solution" << endl;
00192         SmartPtr<IpoptSolver> ipoptSolver  = new IpoptSolver(); 
00193         try{
00194                 ok = true;
00195                 OSiLReader *osilreader = NULL;
00196                 osilFileName =  dataDir  + "osilFiles" + dirsep +  "avion2.osil";
00197                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00198                 cout << "IPOPT Solver created for OSiL string solution" << endl;
00199                 ipoptSolver->osol = osol;
00200                 osilreader = new OSiLReader(); 
00201                 ipoptSolver->osinstance = osilreader->readOSiL( &osil);
00202                 cout << "call the IPOPT Solver" << endl;        
00203                 ipoptSolver->solve();
00204                 cout << "Here is the IPOPT solver solution for avion2" << endl;
00205                 check = 9.46801e+07;
00206                 ok &= NearEqual(getObjVal( ipoptSolver->osrl) , check,  1e-10 , 1e-10);
00207                 if(ok == false) throw ErrorClass(" Fail unit test with Ipopt on avion2");
00208                 delete osilreader;
00209                 osilreader = NULL;
00210                 unitTestResult << "Solved problem avion2.osil with Ipopt" << std::endl;
00211                 // solve another problem
00212                 // a problem with all nonlinear terms no linear terms
00213                 osilFileName =  dataDir  + "osilFiles" + dirsep + "HS071_NLPMod.osil";
00214                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00215                 cout << "IPOPT Solver created for OSiL string solution" << endl;
00216                 ipoptSolver->osol = osol;
00217                 osilreader = new OSiLReader(); 
00218                 ipoptSolver->osinstance = osilreader->readOSiL( &osil);
00219                 ipoptSolver->solve();
00220                 cout << "Here is the IPOPT solver solution for HS071_NLP" << endl;
00221                 check = 17.014;
00222                 ok &= NearEqual(getObjVal( ipoptSolver->osrl) , check,  1e-10 , 1e-10);
00223                 if(ok == false) throw ErrorClass(" Fail unit test with Ipopt on HS071_NLP");
00224                 delete osilreader;
00225                 osilreader = NULL;
00226                 unitTestResult << "Solved problem HS071.osil with Ipopt" << std::endl;
00227                 // solve another problem
00228                 // a problem with both quadratic terms and general nonlinear terms
00229                 osilFileName =  dataDir  + "osilFiles" + dirsep + "rosenbrockmod.osil";
00230                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00231                 cout << "IPOPT Solver created for OSiL string solution" << endl;
00232                 ipoptSolver->osol = osol;
00233                 osilreader = new OSiLReader(); 
00234                 ipoptSolver->osinstance = osilreader->readOSiL( &osil);
00235                 cout << "call the IPOPT Solver" << endl;
00236                 ipoptSolver->solve();
00237                 check = 6.7279;
00238                 ok &= NearEqual(getObjVal( ipoptSolver->osrl) , check,  1e-10 , 1e-10);
00239                 if(ok == false) throw ErrorClass(" Fail unit test with Ipopt on rosenbrock");
00240                 delete osilreader;
00241                 osilreader = NULL;
00242                 unitTestResult << "Solved problem rosenbrockmod.osil with Ipopt" << std::endl;
00243                 // solve another problem
00244                 // a problem that is a pure quadratic
00245                 osilFileName =  dataDir  + "osilFiles" + dirsep + "parincQuadratic.osil";
00246                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00247                 cout << "IPOPT Solver created for OSiL string solution" << endl;
00248                 ipoptSolver->osol = osol;
00249                 osilreader = new OSiLReader(); 
00250                 ipoptSolver->osinstance = osilreader->readOSiL( &osil);
00251                 cout << "call the IPOPT Solver" << endl;
00252                 ipoptSolver->solve();
00253                 cout << "Here is the IPOPT solver solution for parincQuadratic" << endl;
00254                 check = 49920.5;
00255                 ok &= NearEqual(getObjVal( ipoptSolver->osrl) , check,  1e-10 , 1e-10);
00256                 if(ok == false) throw ErrorClass(" Fail unit test with Ipopt on parincQuadradic");
00257                 delete osilreader;
00258                 osilreader = NULL;      
00259                 unitTestResult << "Solved problem parincQuadratic.osil with Ipopt" << std::endl;
00260                 // solve another problem
00261                 // try a pure linear program
00262                 osilFileName =  dataDir  + "osilFiles" + dirsep + "parincLinear.osil";
00263                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00264                 cout << "IPOPT Solver created for OSiL string solution" << endl;
00265                 ipoptSolver->osol = osol; 
00266                 osilreader = new OSiLReader(); 
00267                 ipoptSolver->osinstance = osilreader->readOSiL( &osil);
00268                 cout << "call the IPOPT Solver" << endl;
00269                 ipoptSolver->solve();
00270                 cout << "Here is the IPOPT solver solution for parincLinear" << endl;
00271                 check = 7668;
00272                 ok &= NearEqual(getObjVal( ipoptSolver->osrl) , check,  1e-1 , 1e-1);
00273                 if(ok == false) throw ErrorClass(" Fail unit test with Ipopt on parincLinear");
00274                 delete osilreader;
00275                 osilreader = NULL;      
00276                 unitTestResult << "Solved problem parincLinear.osil with Ipopt" << std::endl;   
00277                 // solve another problem
00278                 // callBackTest.osil
00279                 osilFileName =  dataDir  + "osilFiles" + dirsep + "callBackTest.osil";
00280                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00281                 cout << "IPOPT Solver created for OSiL string solution" << endl;
00282                 ipoptSolver->osol = osol;
00283                 osilreader = new OSiLReader(); 
00284                 ipoptSolver->osinstance = osilreader->readOSiL( &osil);
00285                 //OSiLWriter osilwriter;
00286                 //cout << osilwriter.writeOSiL( ipoptSolver->osinstance) << endl;
00287                 //return 0;
00288                 cout << "call the IPOPT Solver" << endl;
00289                 ipoptSolver->solve();
00290                 cout << "Here is the IPOPT solver solution for callBackTest" << endl;
00291                 check = 1.00045e+06;
00292                 ok &= NearEqual(getObjVal( ipoptSolver->osrl) , check,  1e-10 , 1e-10);
00293                 if(ok == false) throw ErrorClass(" Fail unit test with Ipopt on parincLinear");
00294                 delete osilreader;
00295                 osilreader = NULL;      
00296                 unitTestResult << "Solved problem callBack.osil with Ipopt" << std::endl;       
00297                 
00298                 // solve another problem
00299                 // callBackTest.osil
00300                 osilFileName =  dataDir  + "osilFiles" + dirsep + "callBackTestRowMajor.osil";
00301                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00302                 cout << "IPOPT Solver created for OSiL string solution" << endl;
00303                 ipoptSolver->osol = osol;
00304                 osilreader = new OSiLReader(); 
00305                 ipoptSolver->osinstance = osilreader->readOSiL( &osil);
00306                 //OSiLWriter osilwriter;
00307                 //cout << osilwriter.writeOSiL( ipoptSolver->osinstance) << endl;
00308                 //return 0;
00309                 cout << "call the IPOPT Solver" << endl;
00310                 ipoptSolver->solve();
00311                 cout << "Here is the IPOPT solver solution for callBackTestRowMajor" << endl;
00312                 check = 1.00045e+06;
00313                 ok &= NearEqual(getObjVal( ipoptSolver->osrl) , check,  1e-10 , 1e-10);
00314                 if(ok == false) throw ErrorClass(" Fail unit test with Ipopt on parincLinear");
00315                 delete osilreader;
00316                 osilreader = NULL;      
00317                 unitTestResult << "Solved problem callBackRowMajor.osil with Ipopt" << std::endl;       
00318                 // not we do not delete ipoptSolver -- this is a smart pointer
00319                 //delete solver;
00320                 //solver = NULL;                        
00321                 // not we do not delete ipoptSolver -- this is a smart pointer
00322                 //delete solver;
00323                 //solver = NULL;        
00324         }
00325         catch(const ErrorClass& eclass){
00326                 unitTestResultFailure << "Sorry Unit Test Failed Testing the Ipopt Solver:"  + eclass.errormsg<< endl;  
00327         }
00328         #endif
00329         try{
00330                 ok = true; 
00331                 osilFileName = dataDir  + "osilFiles" + dirsep + "parincLinear.osil";
00332                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00333                 solver = new CoinSolver();
00334                 solver->sSolverName = "clp";
00335                 solver->osil = osil;
00336                 solver->osol = osol;  
00337                 solver->osinstance = NULL; 
00338                 cout << "call the COIN - clp Solver for parincLinear" << endl;
00339                 solver->solve();
00340                 cout << "Here is the COIN clp solver solution for parincLinear" << endl;
00341                 cout << solver->osrl << endl;
00342                 check = 7668;
00343                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-1 , 1e-1);
00344                 if(ok == false) throw ErrorClass(" Fail unit test with clp on parincLinear");
00345                 delete solver;
00346                 solver = NULL;
00347                 unitTestResult << "Solved problem parincLinear.osil with Clp" << std::endl;
00348                 // now solve another problem -- try an integer program
00349                 // this problem is also stored in base64 binary
00350                 osilFileName = dataDir  + "osilFiles" + dirsep + "p0033.osil";
00351                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00352                 solver = new CoinSolver();
00353                 solver->sSolverName ="cbc";
00354                 solver->osil = osil;
00355                 solver->osol = osol;  
00356                 solver->osinstance = NULL; 
00357                 cout << "call the COIN - Cbc Solver for p0033" << endl;
00358                 solver->solve();
00359                 cout << "Here is the COIN Cbc solver solution for p0033" << endl;
00360                 //cout << solver->osrl << endl;
00361                 check = 3089;
00362                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00363                 if(ok == false) throw ErrorClass(" Fail unit test with Cbc on p0033");
00364                 delete solver;
00365                 solver = NULL;
00366                 unitTestResult << "Solved problem p0033.osil with Cbc" << std::endl;
00367         }
00368         catch(const ErrorClass& eclass){
00369                 cout << "OSrL =  " <<  solver->osrl <<  endl;
00370                 cout << endl << endl << endl;
00371                 cout <<"Sorry Unit Test Failed Testing the Ipopt Solver:"  + eclass.errormsg << endl;
00372         }
00373         
00374         //
00375         //
00376         #ifdef COIN_HAS_KNITRO
00377         try{
00378                 ok = true; 
00379                 osilFileName = dataDir  + "osilFiles" + dirsep + "rosenbrockmod.osil";
00380                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00381                 solver = new KnitroSolver();
00382                 solver->osrl = "";      
00383                 solver->osil = osil;
00384                 solver->osol = osol;
00385                 solver->osinstance = NULL;
00386                 cout << "call the KNITRO Solver" << endl;
00387                 solver->solve();
00388                 cout << "Here is the KNITRO solver solution" << endl;   
00389                 check = 6.7279;
00390                 cout << solver->osrl << endl;
00391                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00392                 if(ok == false) throw ErrorClass(" Fail unit test with Knitro on r              return 0;osenbrokmod");
00393                 solver->osinstance = NULL;
00394                 delete solver;
00395                 solver = NULL;
00396                 unitTestResult << "Solved problem rosenbrockmod.osil with Knitro" << std::endl;
00397                 //
00398                 // now solve callBackTest.osil
00399                 osilFileName = dataDir  + "osilFiles" + dirsep + "callBackTest.osil";
00400                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00401                 solver = new KnitroSolver();
00402                 solver->osrl = "";              
00403                 solver->osil = osil;            
00404                 solver->osol = osol;
00405                 solver->osinstance = NULL;
00406                 cout << "call the KNITRO Solver" << endl;
00407                 solver->solve();
00408                 cout << "Here is the KNITRO solver solution" << endl;
00409                 check = 1.00045e+06; 
00410                 cout << solver->osrl << endl;
00411                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00412                 if(ok == false) throw ErrorClass(" Fail unit test with Knitr callBackTest.osil");
00413                 solver->osinstance = NULL;
00414                 delete solver;
00415                 solver = NULL;  
00416                 unitTestResult << "Solved problem callBackTest.osil with Knitro" << std::endl;          
00417                 //
00418                 // now solve a pure quadratic
00419                 osilFileName = dataDir  + "osilFiles" + dirsep + "parincQuadratic.osil";
00420                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00421                 solver = new KnitroSolver();    
00422                 solver->osil = osil;
00423                 solver->osol = osol;
00424                 solver->osinstance = NULL;
00425                 cout << "call the Knitro Solver" << endl;
00426                 solver->solve();
00427                 cout << "Here is the Knitro solver solution" << endl;
00428                 cout << solver->osrl << endl;
00429                 check = 49920.5;
00430                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00431                 if(ok == false) throw ErrorClass(" Fail unit test with Knitro on parincQuadratic");
00432                 delete solver;
00433                 solver = NULL;
00434                 unitTestResult << "Solved problem parincQuadratic.osil with Knitro" << std::endl;
00435                 //
00436                 // now solve a HS071_NLP.osil
00437                 osilFileName = dataDir  + "osilFiles" + dirsep + "HS071_NLP.osil";
00438                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00439                 solver = new KnitroSolver();    
00440                 solver->osil = osil;
00441                 solver->osol = osol;
00442                 solver->osinstance = NULL;
00443                 cout << "call the Knitro Solver" << endl;
00444                 solver->solve();                
00445                 cout << "Here is the Knitro solver solution" << endl;
00446                 cout << solver->osrl << endl;
00447                 check = 17.014;
00448                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00449                 if(ok == false) throw ErrorClass(" Fail unit test with Knitro on HS071_NLP");
00450                 delete solver;
00451                 solver = NULL;
00452                 unitTestResult << "Solved problem HS071_NLP.osil with Knitro" << std::endl;
00453         }
00454         catch(const ErrorClass& eclass){
00455                 cout << "OSrL =  " <<  solver->osrl <<  endl;           
00456                 cout << endl << endl << endl;
00457                 unitTestResultFailure  <<"Sorry Unit Test Failed Testing the Knitro Solver:"  + eclass.errormsg << endl;
00458         }       
00459         #endif
00460         //
00461         //
00462         
00463         //
00464         //
00465         #ifdef COIN_HAS_SYMPHONY
00466         try{
00467                 ok = true; 
00468                 osilFileName = dataDir  + "osilFiles" + dirsep + "p0033.osil";
00469                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00470                 solver = new CoinSolver();
00471                 solver->sSolverName = "symphony";
00472                 solver->osil = osil;
00473                 solver->osol = osol;  
00474                 solver->osinstance = NULL; 
00475                 cout << "call the COIN - SYMPHONY Solver for p0033" << endl;
00476                 solver->solve();
00477                 cout << "Here is the COIN SYMPHONY solver solution for p0033" << endl;
00478                 cout << solver->osrl << endl;
00479                 check = 3089;
00480                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00481                 if(ok == false) throw ErrorClass(" Fail unit test with SYMPHONY on p0033.osil");
00482                 delete solver;
00483                 solver = NULL;
00484                 unitTestResult << "Solved problem p0033.osil with SYMPHONY" << std::endl;
00485         }
00486         catch(const ErrorClass& eclass){
00487                 cout << "OSrL =  " <<  solver->osrl <<  endl;
00488                 cout << endl << endl << endl;
00489                 unitTestResultFailure  << "Sorry Unit Test Failed Testing the SYMPHONY Solver:"  + eclass.errormsg << endl;
00490         }       
00491         #endif
00492         //
00493         //
00494         //
00495         //
00496         #ifdef COIN_HAS_DYLP
00497         try{
00498                 ok = true; 
00499                 osilFileName = dataDir  + "osilFiles" + dirsep + "parincLinear.osil";
00500                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00501                 solver = new CoinSolver();
00502                 solver->sSolverName = "dylp";
00503                 solver->osil = osil;
00504                 solver->osol = osol;   
00505                 solver->osinstance = NULL; 
00506                 cout << "call the COIN - DyLP solver for parincLinear" << endl;
00507                 solver->solve();
00508                 cout << "Here is the COIN - DyLP solver solution for parincLinear" << endl;
00509                 cout << solver->osrl << endl;
00510                 check = 7668;
00511                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-1 , 1e-1);
00512                 if(ok == false) throw ErrorClass(" Fail unit test with DyLP on parincLinear.osil");
00513                 delete solver;
00514                 solver = NULL;
00515                 unitTestResult << "Solved problem parincLinear.osil with DyLP" << std::endl;
00516         }
00517         catch(const ErrorClass& eclass){
00518                 cout << "OSrL =  " <<  solver->osrl <<  endl;
00519                 cout << endl << endl << endl;
00520                 unitTestResultFailure  <<"Sorry Unit Test Failed Testing the DyLP Solver:"  + eclass.errormsg << endl;
00521         }       
00522         #endif
00523         //
00524         //
00525         //
00526         
00527         #ifdef COIN_HAS_VOL
00528         try{
00529                 ok = true; 
00530                 osilFileName = dataDir  + "osilFiles" + dirsep + "volumeTest.osil";
00531                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00532                 solver = new CoinSolver();
00533                 solver->sSolverName = "vol";
00534                 solver->osil = osil;
00535                 solver->osol = osol;  
00536                 solver->osinstance = NULL; 
00537                 cout << "call the COIN - Vol solver for parincLinear" << endl;
00538                 solver->solve();
00539                 cout << "Here is the COIN - Vol solver solution for parincLinear" << endl;
00540                 cout << solver->osrl << endl;
00541                 check = 7;
00542                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-1 , 1e-1);
00543                 if(ok == false) throw ErrorClass(" Fail unit test with Vol on volumeTest.osil");
00544                 delete solver;
00545                 solver = NULL;
00546                 unitTestResult << "Solved problem volumeTest.osil with Vol" << std::endl;
00547         }
00548         catch(const ErrorClass& eclass){
00549                 cout << "OSrL =  " <<  solver->osrl <<  endl;
00550                 cout << endl << endl << endl;
00551                 unitTestResultFailure  <<"Sorry Unit Test Failed Testing the Vol Solver:"  + eclass.errormsg << endl;
00552         }       
00553         #endif
00554         //
00555         //
00556         //
00557         //
00558         #ifdef COIN_HAS_GLPK
00559         try{
00560                 ok = true; 
00561                 osilFileName = dataDir  + "osilFiles" + dirsep + "p0033.osil";
00562                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00563                 solver = new CoinSolver();
00564                 solver->sSolverName = "glpk";
00565                 solver->osil = osil;
00566                 solver->osol = osol;  
00567                 solver->osinstance = NULL; 
00568                 cout << "call the GLPK Solver for p0033" << endl;
00569                 solver->solve();
00570                 cout << "Here is the GLPK solver solution for p0033" << endl;
00571                 cout << solver->osrl << endl;
00572                 check = 3089;
00573                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00574                 if(ok == false) throw ErrorClass(" Fail unit test with GLPK on p0033.osil");
00575                 delete solver;
00576                 solver = NULL;
00577                 unitTestResult << "Solved problem p0033.osil with GLPK" << std::endl;
00578         }
00579         catch(const ErrorClass& eclass){
00580                 cout << "OSrL =  " <<  solver->osrl <<  endl;
00581                 cout << endl << endl << endl;
00582                 unitTestResultFailure  <<"Sorry Unit Test Failed Testing the Glpk Solver:"  + eclass.errormsg << endl;
00583         }       
00584         #endif
00585         //
00586         //
00587         //
00588         #ifdef COIN_HAS_CPX
00589         try{
00590                 ok = true; 
00591                 osilFileName = dataDir  + "osilFiles" + dirsep + "p0033.osil";
00592                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00593                 solver = new CoinSolver();
00594                 solver->sSolverName = "cplex";
00595                 solver->osil = osil;
00596                 solver->osol = osol;  
00597                 solver->osinstance = NULL; 
00598                 cout << "call the CPLEX Solver for p0033" << endl;
00599                 solver->solve();
00600                 cout << "Here is the CPLEX solver solution for p0033" << endl;
00601                 cout << solver->osrl << endl;
00602                 check = 3089;
00603                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00604                 if(ok == false) throw ErrorClass(" Fail unit test with CPLEX on p0033.osil");
00605                 delete solver;
00606                 solver = NULL;
00607                 unitTestResult << "Solved problem p0033.osil with CPLEX" << std::endl;
00608         }
00609         catch(const ErrorClass& eclass){
00610                 cout << "OSrL =  " <<  solver->osrl <<  endl;
00611                 cout << endl << endl << endl;
00612                 unitTestResultFailure  <<"Sorry Unit Test Failed Testing the Cplex Solver:"  + eclass.errormsg << endl;
00613         }       
00614         #endif
00615         
00616         //
00617         #ifdef COIN_HAS_LINDO
00618         try{
00619                 ok = true;
00620                 osilFileName = dataDir  + "osilFiles" + dirsep + "lindoapiaddins.osil";
00621                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00622                 cout << "create a new LINDO Solver for OSiL string solution" << endl;
00623                 solver = new LindoSolver();     
00624                 solver->osil = osil;
00625                 solver->osol = osol;
00626                 solver->osinstance = NULL;
00627                 cout << "call the LINDO Solver" << endl;
00628                 solver->solve();
00629                 cout << "Here is the LINDO solver solution" << endl;
00630                 cout << solver->osrl << endl;
00631                 check = 99;
00632                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00633                 if(ok == false) throw ErrorClass(" Fail unit test with LINDO on lindoapiaddins");
00634                 solver->osinstance = NULL;
00635                 delete solver;
00636                 solver = NULL;
00637                 unitTestResult << "Solved problem lindoapiaddins.osil with Lindo" << std::endl;
00638                 // now solve the rosenbrock problem from the OSiL paper
00639                 osilFileName = dataDir  + "osilFiles" + dirsep + "rosenbrockmod.osil";
00640                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00641                 solver = new LindoSolver();     
00642                 solver->osil = osil;
00643                 solver->osol = osol;
00644                 solver->osinstance = NULL;
00645                 cout << "call the LINDO Solver" << endl;
00646                 solver->solve();
00647                 cout << "Here is the LINDO solver solution" << endl;
00648                 cout << solver->osrl << endl;
00649                 check = 6.7279;
00650                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00651                 if(ok == false) throw ErrorClass(" Fail unit test with LINDO on rosenbrockmod");
00652                 solver->osinstance = NULL;
00653                 delete solver;
00654                 solver = NULL;
00655                 unitTestResult << "Solved problem rosenbrockmod.osil with Lindo" << std::endl;
00656                 // now solve a pure quadratic
00657                 osilFileName = dataDir  + "osilFiles" + dirsep + "parincQuadratic.osil";
00658                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00659                 solver = new LindoSolver();     
00660                 solver->osil = osil;
00661                 solver->osol = osol;
00662                 solver->osinstance = NULL;
00663                 cout << "call the LINDO Solver" << endl;
00664                 solver->solve();
00665                 cout << "Here is the LINDO solver solution" << endl;
00666                 cout << solver->osrl << endl;
00667                 check = 49920.5;
00668                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00669                 if(ok == false) throw ErrorClass(" Fail unit test with LINDO on parincQuadratic");
00670                 delete solver;
00671                 solver = NULL;
00672                 unitTestResult << "Solved problem parincQuadratic.osil with Lindo" << std::endl;
00673                 // now solve a quadratic binary problem
00674                 // wayneQuadratic.osil
00675                 osilFileName = dataDir  + "osilFiles" + dirsep + "wayneQuadratic.osil";
00676                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00677                 solver = new LindoSolver();     
00678                 solver->osil = osil;
00679                 solver->osol = osol;
00680                 solver->osinstance = NULL;
00681                 cout << "call the LINDO Solver" << endl;
00682                 solver->solve();
00683                 cout << "Here is the LINDO solver solution" << endl;
00684                 cout << solver->osrl << endl;
00685                 check = 2.925;
00686                 std::cout << "CALL NEAR_EQUSL" << std::endl;
00687                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-10 , 1e-10);
00688                 std::cout << "CALL NEAR_EQUSL" << std::endl;
00689                 if(ok == false) throw ErrorClass(" Fail unit test with LINDO on wayneQuadratic");
00690                 delete solver;
00691                 solver = NULL;
00692                 unitTestResult << "Solved problem wayneQuadratic.osil with Lindo" << std::endl;
00693                 
00694         }
00695         catch(const ErrorClass& eclass){
00696                 //cout << "OSrL =  " <<  solver->osrl <<  endl;
00697                 cout << endl << endl << endl;
00698                 unitTestResultFailure  <<"Sorry Unit Test Failed Testing the LINDO Solver:"  + eclass.errormsg << endl << endl;
00699         }
00700         #endif
00701         // end solving using the osil file
00702         // now solve with an OSInstance created from an MPS file
00703         try{
00704                 ok = true;
00705                 cout << endl;
00706                 cout << "START MPS TESTING" << endl << endl;
00707                 cout << "create a COIN Solver for MPS - OSInstance solution" << endl;
00708                 solver = new CoinSolver();
00709                 solver->sSolverName = "cbc";
00710                 mps2osil = new OSmps2osil( mpsFileName);
00711                 mps2osil->createOSInstance() ;
00712                 solver->osinstance = mps2osil->osinstance;
00713                 solver->osol = osol;
00714                 cout << "call COIN Solve" << endl;
00715                 solver->solve();
00716                 cout << "Here is the COIN solver solution" << endl;
00717                 cout << solver->osrl << endl;
00718                 check = -7668;
00719                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-1 , 1e-1);
00720                 if(ok == false) throw ErrorClass(" Fail unit test with COIN Solver on MPS test problem parincLinear.mps");
00721                 delete solver;
00722                 solver = NULL;
00723                 delete mps2osil; 
00724                 mps2osil = NULL;
00725                 cout << endl;
00726                 cout << "DONE WITH MPS TESTING" << endl;
00727                 unitTestResult << "Test the MPS -> OSiL converter on parinc.mps usig Cbc" << std::endl;
00728         }
00729         catch(const ErrorClass& eclass){
00730                 cout << "OSrL =  " <<  solver->osrl <<  endl;
00731                 cout << endl << endl << endl;
00732                 unitTestResultFailure  <<"Sorry Unit Test Failed Testing the MPS convertor:"  + eclass.errormsg << endl;
00733         }
00734         // now solve with an OSInstance created from an AMPL nl file
00735         try{
00736                 ok = true;
00737                 cout << endl;
00738                 cout << "START AMPL TESTING" << endl << endl;
00739 #ifdef COIN_HAS_ASL
00740                 cout << "create a cbc Solver for AMPL nl - OSInstance solution" << endl;
00741                 solver = new CoinSolver();
00742                 solver->sSolverName = "cbc";
00743                 nl2osil = new OSnl2osil( nlFileName); 
00744                 nl2osil->createOSInstance() ;
00745                 solver->osinstance = nl2osil->osinstance;       
00746                 solver->osol = osol;
00747                 cout << "call Cbc Solve" << endl;
00748                 solver->solve();
00749                 cout << "Here is the Cbc solver solution" << endl;
00750                 cout << solver->osrl << endl;
00751                 check = 7668;
00752                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-1 , 1e-1);
00753                 if(ok == false) throw ErrorClass(" Fail unit test with OSnl2osil on problem parinc.nl");
00754                 solver->osinstance = NULL;
00755                 delete solver;
00756                 solver = NULL;
00757                 cout << "call delete nl2osil" << endl;
00758                 delete nl2osil;
00759                 nl2osil = NULL; 
00760                 unitTestResult << "Test the AMPL nl -> OSiL converter on parinc.nl using Cbc" << std::endl; 
00761 #endif
00762                 cout << "END AMPL TESTING" << endl << endl;
00763         }       
00764                 catch(const ErrorClass& eclass){
00765                 cout << "OSrL =  " <<  solver->osrl <<  endl;
00766                 cout << endl << endl << endl;
00767                 unitTestResultFailure  <<"Sorry Unit Test Failed Testing AMPL:"  + eclass.errormsg << endl;
00768         }
00769         //
00770         // Now test the b64 feature
00771         //
00772         try{
00773                 ok = true;
00774                 cout << endl;
00775                 cout << endl;
00776                 cout << "TESTING BASE 64 WITH A COIN SOLVER"<< endl;
00777                 cout << endl;
00778                 OSiLWriter osilwriter;
00779                 osilwriter.m_bWriteBase64 = true;
00780                 solver = new CoinSolver();
00781                 solver->sSolverName = "cbc";
00782                 mps2osil = new OSmps2osil( mpsFileName);
00783                 solver->osinstance = NULL;
00784                 solver->osol = osol;
00785                 mps2osil->createOSInstance() ;
00786                 solver->osil = osilwriter.writeOSiL( mps2osil->osinstance) ;
00787                 std::cout << solver->osil << std::endl;
00788                 solver->solve();
00789                 cout << endl << endl;
00790                 cout << "COIN solution of a OSiL string in b64 format" << endl;
00791                 cout << solver->osrl;
00792                 check = -7668;
00793                 ok &= NearEqual(getObjVal( solver->osrl) , check,  1e-1 , 1e-1);
00794                 if(ok == false) throw ErrorClass(" Fail unit test with COIN Cbc cSolver on b64 test problem parincLinear.mps");
00795                 solver->osinstance = NULL;
00796                 delete solver;
00797                 solver = NULL;
00798                 delete mps2osil;
00799                 mps2osil = NULL;
00800                 unitTestResult << "Test a problem written in b64 and then converted to OSInstance" << std::endl;
00801         }       
00802         catch(const ErrorClass& eclass){
00803                 cout << endl << endl;
00804                 cout << eclass.errormsg <<  endl << endl;
00805                 cout << "OSrL =  " <<  solver->osrl <<  endl;
00806                 cout << endl << endl << endl;
00807                 unitTestResultFailure   << "Sorry Unit Test Failed Testing Use of Base 64" << endl;
00808 
00809         }  
00810         //
00811         // Now just test the OSiL parser
00812         try{ 
00813                 cout << endl;
00814                 clock_t start, finish;
00815                 double duration;
00816                 OSiLWriter *osilwriter = NULL;
00817                 osilwriter = new OSiLWriter();
00818                 //delete fileUtil;
00819                 //fileUtil = NULL;
00820                 //fileUtil = new FileUtil();
00821                 cout << "TEST PARSING A MODEL" << endl;
00822                 cout << "FIRST READ THE FILE INTO A STRING" << endl;
00823                 start = clock();
00824                 osilFileName = dataDir  + "osilFiles" + dirsep + "parincLinear.osil";
00825                 osil = fileUtil->getFileAsString( &osilFileName[0]);
00826                 finish = clock();
00827                 duration = (double) (finish - start) / CLOCKS_PER_SEC;
00828                 cout << "Reading the file into a string took (seconds): "<< duration << endl;
00829                 OSiLReader *osilreader = NULL;
00830                 osilreader = new OSiLReader(); 
00831                 start = clock();
00832                 cout << "PARSE THE OSIL STRING INTO AN OSINSTNACE OBJECT" << endl;
00833                 osilreader->readOSiL( &osil);
00834                 //cout << osilwriter->writeOSiL( osilreader->readOSiL( &osil)) << endl;
00835                 delete osilreader;
00836                 osilreader = 0;
00837                 finish = clock();
00838                 duration = (double) (finish - start) / CLOCKS_PER_SEC;
00839                 cout << "Parsing took (seconds): "<< duration << endl;
00840                 unitTestResult << "Successful test of OSiL parser on problem parincLinear.osil" << std::endl;
00841 
00842         }       
00843                 catch(const ErrorClass& eclass){
00844                 cout << endl << endl << endl;
00845                 cout << eclass.errormsg << endl;
00846                 unitTestResultFailure << "Sorry Unit Test Failed Testing An OSiL Parser" << endl;
00847                 
00848         }       
00849         
00850         //
00851         // Now just test the OSrL parser
00852         try{ 
00853                 cout << endl;
00854                 clock_t start, finish;
00855                 double duration;
00856                 OSrLWriter *osrlwriter = NULL;
00857                 osrlwriter = new OSrLWriter();
00858                 OSrLReader *osrlreader = NULL;
00859                 osrlreader = new OSrLReader();
00860                 OSResult *osresult = NULL;
00861                 osresult = new OSResult(); 
00862                 cout << "TEST PARSING AN OSrL FILE" << endl;
00863                 cout << "FIRST READ THE OSrL FILE INTO A STRING" << endl;
00864                 osrlFileName = dataDir  + "osrlFiles" + dirsep + "parincLinear.osrl"; 
00865                 start = clock();
00866                 std::string osrl = fileUtil->getFileAsString( &osrlFileName[0]);
00867                 finish = clock();
00868                 duration = (double) (finish - start) / CLOCKS_PER_SEC;
00869                 cout << "Reading the file into a string took (seconds): "<< duration << endl;
00870                 start = clock();
00871                 cout << "PARSE THE OSRL STRING INTO AN OSRESULT OBJECT" << endl;
00872                 osresult = osrlreader->readOSrL( osrl);
00873                 cout << osrlwriter->writeOSrL( osresult) << endl;
00874                 delete osrlwriter;
00875                 osrlwriter = NULL;
00876                 delete osrlreader;
00877                 osrlreader = NULL;
00878                 delete osresult;
00879                 osresult = NULL;
00880                 finish = clock();
00881                 duration = (double) (finish - start) / CLOCKS_PER_SEC;
00882                 cout << "Parsing took (seconds): "<< duration << endl;
00883                 unitTestResult << "Successful test of OSrL parser on problem parincLinear.osrl" << std::endl;
00884 
00885         }       
00886                 catch(const ErrorClass& eclass){
00887                 cout << endl << endl << endl;
00888                 unitTestResultFailure << eclass.errormsg << endl;
00889         }
00890         // now test postfix and prefix routines
00891         try{
00892                 std::string expTreeTest =  dataDir  + "osilFiles" + dirsep + "rosenbrockmod.osil";
00893                 osil = fileUtil->getFileAsString( &expTreeTest[0]);
00894                 OSInstance *osinstance = NULL;
00895                 osinstance = new OSInstance();
00896                 OSiLReader *osilreader = NULL;
00897                 osilreader = new OSiLReader();
00898                 //create an osinstance
00899                 osinstance = osilreader->readOSiL( &osil);
00900                 OSExpressionTree* expTree = osinstance->getNonlinearExpressionTree( -1);
00901                 std::vector<OSnLNode*> postfixVec;
00902                 //postfixVec = expTree->m_treeRoot->getPostfixFromExpressionTree();
00903                 postfixVec = osinstance->getNonlinearExpressionTreeInPostfix( -1);
00904                 unsigned int n = postfixVec.size();
00905                 unsigned int i;
00906                 std::string *nodeNames1 = new std::string[ n];
00907                 std::string *nodeNames2 = new std::string[ n];
00908                 for (i = 0 ; i < n; i++){
00909                         std::cout << postfixVec[i]->snodeName << std::endl;
00910                         nodeNames1[i] = postfixVec[i]->snodeName;
00911                 }
00912                 // now create back the expression tree 
00913                 expTree->m_treeRoot = postfixVec[ n - 1]->createExpressionTreeFromPostfix( postfixVec);
00914                 // now get in prefix
00915                 std::vector<OSnLNode*> prefixVec;
00916                 //prefixVec = expTree->m_treeRoot->getPrefixFromExpressionTree();
00917                 prefixVec = osinstance->getNonlinearExpressionTreeInPrefix( -1);
00918                 // now create back the expression tree
00919                 expTree->m_treeRoot = prefixVec[ 0]->createExpressionTreeFromPrefix( prefixVec);
00920                 // now get postfix vector again and compare with original
00921                 postfixVec = expTree->m_treeRoot->getPostfixFromExpressionTree();
00922                 //postfixVec = osinstance->getNonlinearExpressionTreeInPostfix( -1);
00923                 if(postfixVec.size() != n) throw ErrorClass(" Problem with creating expression trees");
00924                 std::cout << std::endl << std::endl;
00925                 for (i = 0 ; i < n; i++){
00926                         //std::cout << postfixVec[i]->snodeName << std::endl;
00927                         nodeNames2[i] = postfixVec[i]->snodeName;
00928                         if( nodeNames1[i] != nodeNames2[ i]) throw ErrorClass(" Problem with creating expression trees");
00929                 }       
00930                 //delete osinstance;
00931                 osinstance = NULL;
00932                 delete osilreader;
00933                 osilreader = NULL;
00934                 unitTestResult << "Successful test of prefix and postfix conversion routines on problem rosenbrockmod.osil" << std::endl;
00935                 
00936         }
00937         catch(const ErrorClass& eclass){
00938                 cout << endl << endl << endl;
00939                 unitTestResultFailure << eclass.errormsg << endl;
00940         }
00941         // now test the nonlinear operators     
00942         try{
00943                 ok = true;
00944                 std::cout << "Test nonlinear operators" << std::endl;
00945                 std::string operatorTest =  dataDir  + "osilFiles" + dirsep + "testOperators.osil";
00946                 osil = fileUtil->getFileAsString( &operatorTest[0]);
00947                 OSInstance *osinstance = NULL;
00948                 osinstance = new OSInstance();
00949                 OSiLReader *osilreader = NULL;
00950                 osilreader = new OSiLReader();
00951                 OSiLWriter *osilwriter = NULL;
00952                 osilwriter = new OSiLWriter();
00953                 osinstance = osilreader->readOSiL( &osil);
00954                 OSExpressionTree* expTree = osinstance->getNonlinearExpressionTree( -1);
00955                 std::vector<OSnLNode*> postfixVec;
00956                 postfixVec = expTree->m_treeRoot->getPostfixFromExpressionTree();
00957                 int n = postfixVec.size();
00958                 std::string *nodeNames1 = new std::string[ n];
00959                 for (int i = 0 ; i < n; i++){
00960                         std::cout << postfixVec[i]->snodeName << std::endl;
00961                         nodeNames1[i] = postfixVec[i]->snodeName;
00962                 }
00963                 std::cout << std::endl << std::endl;
00964                 std::cout << osilwriter->writeOSiL( osinstance) << std::endl;
00965                 // now test value
00966                 double *x = NULL;
00967                 x = new double[2];
00968                 x[0] = 1;
00969                 x[1] = 2;
00970                 double parserTestVal = expTree->m_treeRoot->calculateFunction( x);
00971                 std::cout << "ParserTest Val = " << parserTestVal << std::endl;
00972                 check = 11;
00973                 ok &= NearEqual(expTree->m_treeRoot->calculateFunction( x) , check,  1e-10 , 1e-10);
00974                 if(ok == false) throw ErrorClass(" Problem evaluating expression tree");
00975                 osinstance = NULL;
00976                 delete osilreader;
00977                 osilreader = NULL;
00978                 delete osilwriter;
00979                 osilwriter = NULL;
00980                 //create an osinstance
00981                 unitTestResult << "Successful test of all of the nonlinear operators on file testOperators.osil" << std::endl;
00982         }
00983         catch(const ErrorClass& eclass){
00984                 cout << endl << endl << endl;
00985                 unitTestResultFailure << eclass.errormsg << endl;
00986         }
00987         // 
00988         // now solve on a remote server
00989         /*
00990         try{
00991                 //return 0;
00992                 OSSolverAgent* osagent = NULL;
00993                 //osagent = new OSSolverAgent("128.135.130.17/axis/OSSolverService");
00994                 //osagent = new OSSolverAgent("gsbkip.chicagogsb.edu:80/os/ossolver/COINSolverService.jws");
00995                 //osagent = new OSSolverAgent("localhost/os/ossolver/COINSolverService.jws");
00996                 //osagent = new OSSolverAgent("localhost/axis/OSSolverService");
00997                 osagent = new OSSolverAgent("http://127.0.0.1:8080/os/ossolver/CoinSolverService.jws");
00998                 //osagent = new OSSolverAgent("128.135.130.17:8080/os/ossolver/CoinSolverService.jws");
00999                 //osagent = new OSSolverAgent("127.0.0.1:8080/web-services-examples/ted/VrptsSolverService.jws");
01000                 cout << "Place remote synchronous call" << endl;
01001                 nl2osil = new OSnl2osil( nlFileName);
01002                 nl2osil->createOSInstance() ;
01003                 OSiLWriter *osilwriter = NULL;
01004                 osilwriter = new OSiLWriter();
01005                 osil = osilwriter->writeOSiL( nl2osil->osinstance) ;
01006                 string osrl = osagent->solve(osil, osol);
01007                 cout << "READ THE OSrL and WRITE IT AGAIN" << endl;
01008                 cout << osrl  << endl;
01009                 OSrLReader *osrlreader = NULL;
01010                 OSResult *osresult = NULL;
01011                 osrlreader = new OSrLReader();
01012                 osresult = osrlreader->readOSrL( osrl);
01013                 OSrLWriter *osrlwriter;
01014                 if(osresult != NULL){
01015                         osrlwriter = new OSrLWriter();
01016                         osrl = osrlwriter->writeOSrL( osresult);
01017                 }
01018                 cout << osrl << endl;
01019                 //
01020                 delete nl2osil;
01021                 nl2osil = NULL;
01022                 delete osilwriter;
01023                 osilwriter = NULL;
01024         }       
01025                 catch(const ErrorClass& eclass){
01026                 cout <<  eclass.errormsg <<  endl;
01027         }       
01028         */
01029         try{
01030                 std::cout << std::endl << std::endl;
01031                 std::cout << "Testing AD Features " << std::endl;
01032                 std::string expTreeTest =  dataDir  + "osilFiles" + dirsep + "CppADTestLag.osil";
01033                 /*
01034                 min x0^2 + 9*x1   -- w[0]
01035                 s.t. 
01036                 33 - 105 + 1.37*x1 + 2*x3 + 5*x1 <= 10  -- y[0]
01037                 ln(x0*x3)  + 7*x2 >= 10  -- y[1]
01038                 Note: in the first constraint 33 is a constant term and 105 
01039                 is part of the nl node
01040                 */
01041                 osil = fileUtil->getFileAsString( &expTreeTest[0]);
01042                 OSInstance *osinstance = NULL;
01043                 osinstance = new OSInstance();
01044                 OSiLReader *osilreader = NULL;
01045                 osilreader = new OSiLReader();
01046                 //create an osinstance
01047                 osinstance = osilreader->readOSiL( &osil);
01048                 double *x;
01049                 x = new double[ 4];
01050                 x[0] = 1;
01051                 x[1] = 5;
01052                 x[2] = 10;
01053                 x[3] = 5;
01054                 SparseVector *sp;
01055                 // get the gradient for constraint 1
01056                 sp = osinstance->calculateConstraintFunctionGradient(x, 1, true);
01057                 int i;
01058                 for(i = 0; i < sp->number; i++){
01059                         std::cout << "gradient value " << sp->values[i] << std::endl;
01060                 }
01061                 ok = true;
01062                 //check gradient for constraint with index 1
01063                 double checkPartial2Con1 = 7.0 ;
01064                 ok &= NearEqual( sp->values[ 0], checkPartial2Con1, 1e-10, 1e-10); 
01065                 if(ok == false) throw ErrorClass(" Fail testing gradient calculation");
01066                 double checkPartial0Con1 = (1./x[0])  ;
01067                 ok &= NearEqual(sp->values[ 1], checkPartial0Con1, 1e-10, 1e-10); 
01068                 if(ok == false) throw ErrorClass(" Fail testing gradient calculation");
01069                 double checkPartial3Con1 = (1./x[3]) ;
01070                 ok &= NearEqual( sp->values[ 2], checkPartial3Con1, 1e-10, 1e-10); 
01071                 if(ok == false) throw ErrorClass(" Fail testing gradient calculation");
01072                 delete sp;
01073                 SparseHessianMatrix *sh;
01074                 // calcuate Hessian of objective function (index = -1)
01075                 sh = osinstance->calculateHessian(x, -1, true);
01076                 for(i = 0; i < sh->hessDimension; i++){
01077                         std::cout << "Hessian value " << sh->hessValues[i] << std::endl;
01078                 }
01079                 ok &= NearEqual( sh->hessValues[ 0], 2., 1e-10, 1e-10);
01080                 if(ok == false) throw ErrorClass(" Fail testing Hessian calculation"); 
01081                 ok &= NearEqual( sh->hessValues[ 1], 0., 1e-10, 1e-10);
01082                 if(ok == false) throw ErrorClass(" Fail testing Hessian calculation");
01083                 ok &= NearEqual( sh->hessValues[ 2], 0., 1e-10, 1e-10);
01084                 if(ok == false) throw ErrorClass(" Fail testing Hessian calculation");
01085                 ok &= NearEqual( sh->hessValues[ 3], 0., 1e-10, 1e-10);
01086                 if(ok == false) throw ErrorClass(" Fail testing Hessian calculation");
01087                 ok &= NearEqual( sh->hessValues[ 4], 0., 1e-10, 1e-10);
01088                 if(ok == false) throw ErrorClass(" Fail testing Hessian calculation");
01089                 ok &= NearEqual( sh->hessValues[ 5], 0., 1e-10, 1e-1);
01090                 if(ok == false) throw ErrorClass(" Fail testing Hessian calculation");
01091                 unitTestResult << "Successful test of AD gradient and Hessian calculations on problem CppADTestLag.osil" << std::endl;
01092         }       
01093         catch(const ErrorClass& eclass){
01094                 cout << endl << endl << endl;
01095                 unitTestResultFailure << eclass.errormsg << endl;
01096         }
01097 
01098             
01099         if(unitTestResultFailure.str().length() > 0){
01100                 cout << "The unitTest passed the following" << endl << endl;
01101                 cout << unitTestResult.str() << endl << endl;
01102                 cout << "Unfortunately, you failed on the following:" << endl << endl;
01103                 cout << unitTestResultFailure.str() << endl << endl;
01104                 cout << "Conclusion: FAILURE" << endl;
01105                 return 1;
01106         }
01107         else{
01108                 cout << "The unitTest passed the following" << endl << endl;
01109                 cout << unitTestResult.str() << endl << endl;
01110                 cout << "All tests completed successfully" <<  endl <<  endl;
01111                 return 0;
01112         }
01113 }
01114 
01115 double getObjVal( std::string osrl){
01116         std::string sObjVal;
01117         double dObjVal;
01118         string::size_type pos2;
01119         string::size_type pos1 = osrl.find( "<obj ");
01120         if(pos1 != std::string::npos){
01121                 // get the end of the obj start tag
01122                 pos1 = osrl.find(">", pos1 + 1);
01123                 if(pos1 != std::string::npos){
01124                         // get the start of obj end tag
01125                         pos2 = osrl.find( "</obj", pos1 + 1);
01126                         if( pos2 != std::string::npos){
01127                                 // get the substring
01128                                 sObjVal = osrl.substr( pos1 + 1, pos2 - pos1 - 1);
01129                                 //std::cout << "HERE IS THE OBJECTIVE FUNCTION VALUE SUBSTRING  " << sObjVal<< std::endl; 
01130                                 return dObjVal = atof( sObjVal.c_str() ); 
01131                         }
01132                         else return OSNAN;
01133                 }
01134                 else return OSNAN;
01135         }
01136         else return OSNAN;
01137 }

Generated on Thu May 15 22:15:05 2008 by  doxygen 1.4.7