/home/coin/SVN-release/OS-2.3.2/ApplicationTemplates/osResultDemo/OSResultDemo.cpp

Go to the documentation of this file.
00001 /* $Id: OSResultDemo.cpp 2710 2009-06-10 21:13:43Z 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 
00037 
00038 #include "CoinError.hpp"
00039 #include "CoinHelperFunctions.hpp"
00040 #include<iostream> 
00041 
00042 
00043 using std::string;
00044 using std::cout;   
00045 using std::endl;
00046 
00047 //int main(int argC, char* argV[]){
00048 int main( ){
00049         WindowsErrorPopupBlocker();
00050         FileUtil *fileUtil = NULL; 
00051         fileUtil = new FileUtil();
00052         cout << "Start Building the Model" << endl;
00053         try{
00054 
00055 
00056                 const char dirsep =  CoinFindDirSeparator();
00057                 // Set directory containing mps data files.
00058                 std::string dataDir;
00059                 std::string osilFileName;
00060                 dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
00061                 
00062                 osilFileName =  dataDir  +  "parincLinear.osil";
00063                 std::cout << "Try to read a sample file" << std::endl;
00064                 std::cout << "The file is: " ;
00065                 std::cout <<  osilFileName << std::endl;
00066                 std::string osil = fileUtil->getFileAsString( osilFileName.c_str() );
00067                 //now create some options
00068                 OSiLReader *osilreader = NULL;
00069                 osilreader = new OSiLReader(); 
00070                 OSInstance *osinstance;
00071                 osinstance = osilreader->readOSiL( osil);
00072                 CoinSolver *solver = new CoinSolver();
00073                 solver->osinstance = osinstance;
00074                 solver->sSolverName ="clp"; 
00075                 solver->buildSolverInstance();          
00076                 solver->solve();
00077                 
00078                 // we demonstrate the OSResult API
00079                 // first read the result in OSResult object
00080                 // creat an OSResult object from the string                     
00081                 OSrLReader *osrlreader = NULL;
00082                 OSResult *osresult = NULL;
00083                 osrlreader = new OSrLReader();
00084                 osresult  = osrlreader->readOSrL( solver->osrl);
00085                 
00086                 //now use the OSResult API -- first make sure we got an optimal solution
00087                 //get the status
00088                 std::string solStatus;
00089                 double optSolValue;
00090                 // the argument is the solution index
00091                 solStatus = osresult->getSolutionStatusType( 0 );
00092                 // if solStatus is optimal get the optimal solution value
00093                 if( solStatus.find("ptimal") != string::npos ){
00094                 //first index is objIdx, second is solution index
00095                         optSolValue = osresult->getOptimalObjValue( -1, 0);
00096                         std::cout << "OPTIMAL SOLUTION VALUE  " <<  optSolValue << std::endl;
00097                 }else{
00098                         std::cout << "NO OPTIMAL SOLUTION FOUND " << std::endl;
00099                 }
00100                 
00101                 int i;
00102                 int j;
00103                 int k;
00104                 int vecSize;
00105                 // now get the primal solution
00106                 std::vector<IndexValuePair*> primalValPair;
00107                 primalValPair = osresult->getOptimalPrimalVariableValues( 0);
00108                 vecSize = primalValPair.size();
00109                 for(i = 0; i < vecSize; i++){
00110                         std::cout << "index = " <<  primalValPair[ i]->idx << std::endl;
00111                         std::cout << "value = " <<  primalValPair[ i]->value << std::endl;
00112                 }
00113                 // now get the dual solution
00114                 std::vector<IndexValuePair*> dualValPair;
00115                 dualValPair = osresult->getOptimalDualVariableValues( 0);
00116                 vecSize = dualValPair.size();
00117                 for(i = 0; i < vecSize; i++){
00118                         std::cout << "index = " <<  dualValPair[ i]->idx << std::endl;
00119                         std::cout << "value = " <<  dualValPair[ i]->value << std::endl;
00120                 }
00121                 //the OSResult API is currently somewhat limited, but you can get at the
00122                 //OSResult object directly -- let's get all the otherVar stuff
00123                 int numSolutions;
00124                 int numberOfOtherVariableResults;
00125                 int numberOfOtherVar;
00126                 numSolutions =osresult-> getSolutionNumber();
00127                 for(i = 0; i < numSolutions; i++){
00128                         numberOfOtherVariableResults = osresult->
00129                         getNumberOfOtherVariableResults( i);
00130                         for(j = 0; j < numberOfOtherVariableResults; j++){
00131                                 std::cout << "Other Name = " << osresult->optimization->solution[ i]->variables->other[ j]->name << std::endl;
00132                                 std::cout << "Other Description = " << osresult->optimization->solution[ i]->variables->other[ j]->description << std::endl;
00133                                 std::cout << "Other NumberOfVar = " << osresult->optimization->solution[ i]->variables->other[ j]->numberOfVar <<  std::endl;
00134                                 numberOfOtherVar = osresult->optimization->solution[ i]->variables->other[ j]->numberOfVar;
00135                                 for(k = 0; k < numberOfOtherVar; k++){
00136                                         std::cout << "Other Variable index = "   <<  osresult->optimization->solution[ i]->variables->other[ j]->var[ k]->idx << std::endl;
00137                                         std::cout << "Other Variable value = "   <<  osresult->optimization->solution[ i]->variables->other[ j]->var[ k]->value << std::endl;
00138                                 }
00139                         }
00140                 }               
00141                 //garbage collection
00142                 delete solver;
00143                 solver = NULL;
00144                 delete fileUtil;
00145                 fileUtil = NULL;
00146                 delete osrlreader;
00147                 osrlreader = NULL;
00148                 delete osilreader;
00149                 osilreader = NULL;
00150                 cout << "Done with garbage collection" << endl;
00151                 return 0;
00152                 //
00153         }
00154         catch(const ErrorClass& eclass){
00155                 delete fileUtil;
00156                 std::cout << eclass.errormsg <<  std::endl;
00157                 return 0;
00158         } 
00159 }// end main

Generated on Fri Jan 7 03:24:38 2011 by  doxygen 1.4.7