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