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
00046 #include "OSErrorClass.h"
00047 #include "OSMathUtil.h"
00048
00049 #include<iostream>
00050 using std::cout;
00051 using std::endl;
00052
00053
00054 int main( ){
00055 std::string osil;
00056
00057
00058 cout << "Start Building the Model" << endl;
00059 try{
00060 OSInstance *osinstance = new OSInstance();
00061
00062
00063 osinstance->setInstanceSource("From Anderson, Sweeney, Williams, and Martin");
00064 osinstance->setInstanceDescription("The Par Inc. Problem");
00065
00066
00067
00068
00069 osinstance->setVariableNumber( 2);
00070
00071
00072 osinstance->addVariable(0, "x0", 0, OSDBL_MAX, 'C');
00073 osinstance->addVariable(1, "x1", 0, OSDBL_MAX, 'C');
00074
00075
00076 osinstance->setObjectiveNumber( 1);
00077
00078 SparseVector *objcoeff = new SparseVector(2);
00079 objcoeff->indexes[ 0] = 0;
00080 objcoeff->values[ 0] = 10;
00081 objcoeff->indexes[ 1] = 1;
00082 objcoeff->values[ 1] = 9;
00083
00084 osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
00085 objcoeff->bDeleteArrays = true;
00086 delete objcoeff;
00087
00088
00089 osinstance->setConstraintNumber( 4);
00090
00091
00092 osinstance->addConstraint(0, "row0", -OSDBL_MAX, 630, 0);
00093 osinstance->addConstraint(1, "row1", -OSDBL_MAX, 600, 0);
00094 osinstance->addConstraint(2, "row2", -OSDBL_MAX, 708, 0);
00095 osinstance->addConstraint(3, "row3", -OSDBL_MAX, 135, 0);
00096
00097
00098
00099 double *values = new double[ 8];
00100 int *indexes = new int[ 8];
00101 int *starts = new int[ 5];
00102 values[ 0] = .7;
00103 values[ 1] = 1;
00104 values[ 2] = .5;
00105 values[ 3] = 5./6.;
00106 values[ 4] = 1.0;
00107 values[ 5] = 2./3.;
00108 values[ 6] = .1;
00109 values[ 7] = .25;
00110 indexes[ 0] = 0;
00111 indexes[ 1] = 1;
00112 indexes[ 2] = 0;
00113 indexes[ 3] = 1;
00114 indexes[ 4] = 0;
00115 indexes[ 5] = 1;
00116 indexes[ 6] = 0;
00117 indexes[ 7] = 1;
00118 starts[ 0] = 0;
00119 starts[ 1] = 2;
00120 starts[ 2] = 4;
00121 starts[ 3] = 6;
00122 starts[ 4] = 8;
00123 cout << "Call setLinearConstraintCoefficients" << endl;
00124 osinstance->setLinearConstraintCoefficients(8, false, values, 0, 7,
00125 indexes, 0, 7, starts, 0, 4);
00126 cout << "End Building the Model" << endl;
00127
00128
00129 OSiLWriter *osilwriter;
00130 osilwriter = new OSiLWriter();
00131 osil = osilwriter->writeOSiL( osinstance);
00132
00133
00134
00135
00136 OSSolverAgent* osagent = NULL;
00137 osagent = new OSSolverAgent("kipp.chicagobooth.edu/os/OSSolverService.jws");
00138
00139
00140 std::string osol = "<osol></osol>";
00141 cout << "Solve the model synchronously" << endl;
00142 cout << "osil input:" << endl << endl;
00143 cout << osil << endl << endl;
00144 std::string osrl ="" ;
00145 osrl = osagent->solve(osil, osol);
00146 cout << "osrl result from osagent:" << endl << endl;
00147 cout << osrl << endl << endl;
00148
00149
00150
00151 cout << "Now solve the problem in asynchronous mode" << endl;
00152 cout << "get the jobID" << endl;
00153 std::string jobID = osagent->getJobID("");
00154 cout << jobID << endl << endl;
00155
00156
00157
00158 osol = "<osol><general><jobID>" + jobID + "</jobID></general></osol>";
00159 cout << "submit the problem" << endl;
00160 bool sent = osagent->send(osil, osol);
00161 cout << "problem submitted; success?" << sent << endl << endl;
00162 std::string ospl = "<ospl><processHeader><request action=\"getAll\"/></processHeader><processData/></ospl>";
00163 cout << "Enquire about job status" << endl << endl;
00164 std::string osql = osagent->knock(ospl, osol);
00165 cout << "Result of knock {} method:" << endl << osql << endl << endl;
00166
00167
00168 cout << "Parse the result" << endl << endl;
00169 bool status_finished = false;
00170 bool status_found = false;
00171
00172 string::size_type pos1;
00173 string::size_type pos2;
00174 pos1 = osql.find( "<response" );
00175 if (pos1 != std::string::npos){
00176 pos2 = osql.find( ">", pos1 + 1);
00177 if (pos2 != std::string::npos){
00178 pos1 = osql.find( "status", pos1 + 1);
00179 if (pos1 != std::string::npos){
00180 pos1 = osql.find("=", pos1 + 1);
00181 if (pos1 != std::string::npos){
00182 status_found = true;
00183 pos1 = osql.find("finished", pos1 + 1);
00184 if (pos1 != std::string::npos){
00185 if (pos1 < pos2){
00186 status_finished = true;
00187 };
00188 }
00189 else{
00190 status_finished = false;
00191 };
00192 };
00193 };
00194 };
00195 };
00196
00197 if (status_found){
00198 if (status_finished){
00199 cout << "process finished; retrieve results" << endl;
00200 osrl = osagent->retrieve(osol);
00201 cout << osrl << endl << endl;
00202 }
00203 else {
00204 cout << "process still running; kill it!" << endl;
00205 osrl = osagent->kill(osol);
00206 cout << osrl << endl << endl;
00207 };
00208 }
00209 else {
00210 cout << "Improperly formed string" << endl;
00211 };
00212
00213
00214 delete osinstance;
00215 osinstance = NULL;
00216 delete osilwriter;
00217 osilwriter = NULL;
00218 delete osagent;
00219 osagent = NULL;
00220
00221 cout << "Done with garbage collection" << endl;
00222 return 0;
00223
00224 }
00225 catch(const ErrorClass& eclass){
00226
00227 std::cout << eclass.errormsg << std::endl;
00228 return 0;
00229 }
00230 }
00231