/home/coin/SVN-release/OS-1.1.1/OS/examples/osRemoteTest/OSRemoteTest.cpp

Go to the documentation of this file.
00001 
00018 #include <cppad/cppad.hpp> 
00019 #include "OSConfig.h"
00020 #include "OSCoinSolver.h"
00021 
00022 #ifdef COIN_HAS_IPOPT  
00023         #ifndef COIN_HAS_ASL
00024                 #include "OSIpoptSolver.h"
00025                 #undef COIN_HAS_ASL
00026         #else
00027                 #include "OSIpoptSolver.h"
00028         #endif
00029 #endif
00030 
00031 #include "OSResult.h" 
00032 #include "OSiLReader.h"        
00033 #include "OSiLWriter.h"   
00034 #include "OSrLReader.h"          
00035 #include "OSrLWriter.h"      
00036 #include "OSInstance.h"  
00037 #include "OSFileUtil.h"   
00038 #include "OSDefaultSolver.h"  
00039 #include "OSWSUtil.h" 
00040 #include "OSSolverAgent.h"   
00041 #include "OShL.h"     
00042 #include "OSErrorClass.h"
00043 #include "OSmps2osil.h"   
00044 #include "OSBase64.h"
00045 #include "OSCommonUtil.h"
00046 #include "OSErrorClass.h"
00047 #include "OSMathUtil.h"
00048 
00049 #include<iostream> 
00050 using std::cout;   
00051 using std::endl;
00052 
00053 //int main(int argC, char* argV[]){
00054 int main( ){
00055                 std::string osil;
00056 
00057 // test OS code samples here
00058         FileUtil *fileUtil = NULL; 
00059         fileUtil = new FileUtil();
00060         cout << "Start Building the Model" << endl;
00061         try{
00062                 OSInstance *osinstance = new OSInstance();
00063 
00064                 // put in some of the OSInstance <instanceHeader> information
00065                 osinstance->setInstanceSource("From Anderson, Sweeney, Williams, and Martin");
00066                 osinstance->setInstanceDescription("The Par Inc. Problem");
00067                 //
00068                 // now put in the OSInstance <instanceData> information
00069                 // 
00070                 // first the variables
00071                 osinstance->setVariableNumber( 2);   
00072                 //addVariable(int index, string name, double lowerBound, double upperBound, char type, double init, string initString);
00073                 // we could use setVariables() and add all the variable with one method call -- below is easier
00074                 osinstance->addVariable(0, "x0", 0, OSDBL_MAX, 'C', OSNAN, "");
00075                 osinstance->addVariable(1, "x1", 0, OSDBL_MAX, 'C', OSNAN, "");
00076                 //
00077                 // now add the objective function
00078                 osinstance->setObjectiveNumber( 1);
00079                 // now the coefficient
00080                 SparseVector *objcoeff = new SparseVector(2);   
00081                 objcoeff->indexes[ 0] = 0;
00082                 objcoeff->values[ 0] = 10;
00083                 objcoeff->indexes[ 1] = 1;
00084                 objcoeff->values[ 1] = 9;
00085                 //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
00086                 osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
00087                 objcoeff->bDeleteArrays = true;
00088                 delete objcoeff;
00089                 //
00090                 // now the constraints
00091                 osinstance->setConstraintNumber( 4); 
00092                 //bool addConstraint(int index, string name, double lowerBound, double upperBound, double constant);
00093                 // note: we could use setConstraints() and add all the constraints with one method call -- below is easier
00094                 osinstance->addConstraint(0, "row0", -OSDBL_MAX, 630, 0); 
00095                 osinstance->addConstraint(1, "row1", -OSDBL_MAX, 600, 0);
00096                 osinstance->addConstraint(2, "row2", -OSDBL_MAX, 708, 0);
00097                 osinstance->addConstraint(3, "row3", -OSDBL_MAX, 135, 0); 
00098                 /*
00099                 now add the <linearConstraintCoefficients> --- row major
00100                 */
00101                 double *values = new double[ 8];
00102                 int *indexes = new int[ 8];
00103                 int *starts = new int[ 5];  
00104                 values[ 0] = .7;
00105                 values[ 1] = 1;
00106                 values[ 2] = .5;
00107                 values[ 3] = 5./6.;
00108                 values[ 4] = 1.0;
00109                 values[ 5] = 2./3.;
00110                 values[ 6] = .1;
00111                 values[ 7] = .25;
00112                 indexes[ 0] = 0;
00113                 indexes[ 1] = 1;
00114                 indexes[ 2] = 0;
00115                 indexes[ 3] = 1;
00116                 indexes[ 4] = 0;
00117                 indexes[ 5] = 1;
00118                 indexes[ 6] = 0;  
00119                 indexes[ 7] = 1;
00120                 starts[ 0] = 0;
00121                 starts[ 1] = 2;
00122                 starts[ 2] = 4;
00123                 starts[ 3] = 6;
00124                 starts[ 4] = 8;
00125                 cout << "Call setLinearConstraintCoefficients" << endl;
00126                 osinstance->setLinearConstraintCoefficients(8, false, values, 0, 7, 
00127                         indexes, 0, 7, starts, 0, 4);
00128                 cout << "End Building the Model" << endl; 
00129 
00130                 // Write out the model
00131                 OSiLWriter *osilwriter; 
00132                 osilwriter = new OSiLWriter();
00133                 osil = osilwriter->writeOSiL( osinstance);
00134 
00135                 // now solve the model synchronously
00136                 OSSolverAgent* osagent = NULL;
00137                 osagent = new OSSolverAgent("gsbkip.chicagogsb.edu/os/OSSolverService.jws");
00138                 
00139                 std::string osol = "<osol></osol>";
00140                 cout << "Solve the model synchronously" << endl;
00141                 cout << "osil input:" << endl << endl;
00142                 cout << osil  << endl << endl;
00143                 std::string osrl = osagent->solve(osil, osol);
00144                 cout << "osrl result from osagent:" << endl << endl;
00145                 cout << osrl  << endl << endl;
00146 
00147                 // now do an asynchronous call
00148                 // get the jobID first
00149                 cout << "Now solve the problem in asynchronous mode" << endl;
00150                 cout << "get the jobID" << endl;
00151                 std::string jobID = osagent->getJobID("");
00152                 cout << jobID << endl << endl;
00153 
00154                 // build the osol 
00155                 osol = "<osol><general><jobID>" + jobID + "</jobID></general></osol>";
00156                 cout << "submit the problem" << endl;
00157                 bool sent = osagent->send(osil, osol);
00158                 cout << "problem submitted; success?" << sent << endl << endl;
00159                 std::string ospl = "<ospl><processHeader><request action=\"getAll\"/></processHeader><processData/></ospl>";
00160                 cout << "Enquire about job status" << endl << endl;
00161                 std::string osql = osagent->knock(ospl, osol);
00162                 cout << "Result of knock {} method:" << endl << osql  << endl << endl;
00163 
00164                 // parse the result --- only look for 'status = "finished  "'
00165                 cout << "Parse the result" << endl << endl;
00166                 bool status_finished = false;
00167                 bool status_found    = false;
00168 
00169                 string::size_type pos1;
00170                 string::size_type pos2;
00171                 pos1 = osql.find( "<response" );
00172                 if (pos1 != std::string::npos){
00173                         pos2 = osql.find( ">", pos1 + 1);
00174                         if (pos2 != std::string::npos){
00175                                 pos1 = osql.find( "status", pos1 + 1);
00176                                 if (pos1 != std::string::npos){
00177                                         pos1 = osql.find("=", pos1 + 1);
00178                                         if (pos1 != std::string::npos){
00179                                                 status_found = true;
00180                                                 pos1 = osql.find("finished", pos1 + 1);
00181                                                 if (pos1 != std::string::npos){
00182                                                         if (pos1 < pos2){
00183                                                                 status_finished = true;
00184                                                         };
00185                                                 }
00186                                                 else{
00187                                                         status_finished = false;
00188                                                 };
00189                                         };
00190                                 };
00191                         };
00192                 };
00193 
00194                 if (status_found){
00195                         if (status_finished){
00196                                 cout << "process finished; retrieve results" << endl;
00197                                 osrl = osagent->retrieve(osol);
00198                                 cout << osrl  << endl << endl;
00199                         }
00200                                 else {
00201                                 cout << "process still running; kill it!" << endl;
00202                                 osrl = osagent->kill(osol);
00203                                 cout << osrl  << endl << endl;
00204                         };
00205                 }
00206                         else {
00207                         cout << "Improperly formed string" << endl;
00208                 };
00209 
00210                 // do garbage collection
00211                 delete osinstance;
00212                 osinstance = NULL;
00213                 delete osilwriter;
00214                 osilwriter = NULL;
00215                 delete fileUtil;
00216                 fileUtil = NULL;
00217 
00218                 cout << "Done with garbage collection" << endl;
00219                 return 0;
00220                 //
00221         }
00222         catch(const ErrorClass& eclass){
00223                 delete fileUtil;
00224                 std::cout << eclass.errormsg <<  std::endl;
00225                 return 0;
00226         } 
00227 }// end main
00228 

Generated on Tue Sep 30 03:01:24 2008 by  doxygen 1.4.7