OSRemoteTest.cpp
Go to the documentation of this file.
1 /* $Id: OSRemoteTest.cpp 3355 2010-03-28 08:06:32Z kmartin $ */
16 #include <cppad/cppad.hpp>
17 #include "OSConfig.h"
18 #include "OSCoinSolver.h"
19 
20 #ifdef COIN_HAS_IPOPT
21  #ifndef COIN_HAS_ASL
22  #include "OSIpoptSolver.h"
23  #undef COIN_HAS_ASL
24  #else
25  #include "OSIpoptSolver.h"
26  #endif
27 #endif
28 
29 #include "OSResult.h"
30 #include "OSiLReader.h"
31 #include "OSiLWriter.h"
32 #include "OSrLReader.h"
33 #include "OSrLWriter.h"
34 #include "OSInstance.h"
35 #include "OSFileUtil.h"
36 #include "OSDefaultSolver.h"
37 #include "OSWSUtil.h"
38 #include "OSSolverAgent.h"
39 #include "OShL.h"
40 #include "OSErrorClass.h"
41 #include "OSmps2OS.h"
42 #include "OSBase64.h"
43 //#include "OSCommonUtil.h"
44 #include "OSErrorClass.h"
45 #include "OSMathUtil.h"
46 
47 #include<iostream>
48 using std::cout;
49 using std::endl;
50 
51 //int main(int argC, char* argV[]){
52 int main( ){
53  std::string osil;
54 
55 // test OS code samples here
56  cout << "Start Building the Model" << endl;
57  try{
59 
60  // put in some of the OSInstance <instanceHeader> information
61  osinstance->setInstanceSource("From Anderson, Sweeney, Williams, and Martin");
62  osinstance->setInstanceDescription("The Par Inc. Problem");
63  //
64  // now put in the OSInstance <instanceData> information
65  //
66  // first the variables
67  osinstance->setVariableNumber( 2);
68  //addVariable(int index, string name, double lowerBound, double upperBound, char type, double init, string initString);
69  // we could use setVariables() and add all the variable with one method call -- below is easier
70  osinstance->addVariable(0, "x0", 0, OSDBL_MAX, 'C');
71  osinstance->addVariable(1, "x1", 0, OSDBL_MAX, 'C');
72  //
73  // now add the objective function
74  osinstance->setObjectiveNumber( 1);
75  // now the coefficient
76  SparseVector *objcoeff = new SparseVector(2);
77  objcoeff->indexes[ 0] = 0;
78  objcoeff->values[ 0] = 10;
79  objcoeff->indexes[ 1] = 1;
80  objcoeff->values[ 1] = 9;
81  //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
82  osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
83  objcoeff->bDeleteArrays = true;
84  delete objcoeff;
85  //
86  // now the constraints
87  osinstance->setConstraintNumber( 4);
88  //bool addConstraint(int index, string name, double lowerBound, double upperBound, double constant);
89  // note: we could use setConstraints() and add all the constraints with one method call -- below is easier
90  osinstance->addConstraint(0, "row0", -OSDBL_MAX, 630, 0);
91  osinstance->addConstraint(1, "row1", -OSDBL_MAX, 600, 0);
92  osinstance->addConstraint(2, "row2", -OSDBL_MAX, 708, 0);
93  osinstance->addConstraint(3, "row3", -OSDBL_MAX, 135, 0);
94  /*
95  now add the <linearConstraintCoefficients> --- row major
96  */
97  double *values = new double[ 8];
98  int *indexes = new int[ 8];
99  int *starts = new int[ 5];
100  values[ 0] = .7;
101  values[ 1] = 1;
102  values[ 2] = .5;
103  values[ 3] = 5./6.;
104  values[ 4] = 1.0;
105  values[ 5] = 2./3.;
106  values[ 6] = .1;
107  values[ 7] = .25;
108  indexes[ 0] = 0;
109  indexes[ 1] = 1;
110  indexes[ 2] = 0;
111  indexes[ 3] = 1;
112  indexes[ 4] = 0;
113  indexes[ 5] = 1;
114  indexes[ 6] = 0;
115  indexes[ 7] = 1;
116  starts[ 0] = 0;
117  starts[ 1] = 2;
118  starts[ 2] = 4;
119  starts[ 3] = 6;
120  starts[ 4] = 8;
121  cout << "Call setLinearConstraintCoefficients" << endl;
122  osinstance->setLinearConstraintCoefficients(8, false, values, 0, 7,
123  indexes, 0, 7, starts, 0, 4);
124  cout << "End Building the Model" << endl;
125 
126  // Write out the model
127  OSiLWriter *osilwriter;
128  osilwriter = new OSiLWriter();
129  osil = osilwriter->writeOSiL( osinstance);
130 
131 
132 
133  // now solve the model synchronously
134  OSSolverAgent* osagent = NULL;
135  osagent = new OSSolverAgent("http://74.94.100.129:8080/OSServer/services/OSSolverService");
136 
137 
138  std::string osol = "<osol></osol>";
139  cout << "Solve the model synchronously" << endl;
140  cout << "osil input:" << endl << endl;
141  cout << osil << endl << endl;
142  std::string osrl ="" ;
143  osrl = osagent->solve(osil, osol);
144  cout << "osrl result from osagent:" << endl << endl;
145  cout << osrl << endl << endl;
146 
147  // now do an asynchronous call
148  // get the jobID first
149  cout << "Now solve the problem in asynchronous mode" << endl;
150  cout << "get the jobID" << endl;
151  std::string jobID = osagent->getJobID("");
152  cout << jobID << endl << endl;
153 
154 
155  // build the osol
156  osol = "<osol><general><jobID>" + jobID + "</jobID></general></osol>";
157  cout << "submit the problem" << endl;
158  bool sent = osagent->send(osil, osol);
159  cout << "problem submitted; success?" << sent << endl << endl;
160  std::string ospl = "<ospl><processHeader><request action=\"getAll\"/></processHeader><processData/></ospl>";
161  cout << "Enquire about job status" << endl << endl;
162  std::string osql = osagent->knock(ospl, osol);
163  cout << "Result of knock {} method:" << endl << osql << endl << endl;
164 
165  // parse the result --- only look for 'status = "finished "'
166  cout << "Parse the result" << endl << endl;
167  bool status_finished = false;
168  bool status_found = false;
169 
170  std::string::size_type pos1;
171  std::string::size_type pos2;
172  pos1 = osql.find( "<response" );
173  if (pos1 != std::string::npos){
174  pos2 = osql.find( ">", pos1 + 1);
175  if (pos2 != std::string::npos){
176  pos1 = osql.find( "status", pos1 + 1);
177  if (pos1 != std::string::npos){
178  pos1 = osql.find("=", pos1 + 1);
179  if (pos1 != std::string::npos){
180  status_found = true;
181  pos1 = osql.find("finished", pos1 + 1);
182  if (pos1 != std::string::npos){
183  if (pos1 < pos2){
184  status_finished = true;
185  };
186  }
187  else{
188  status_finished = false;
189  };
190  };
191  };
192  };
193  };
194 
195  if (status_found){
196  if (status_finished){
197  cout << "process finished; retrieve results" << endl;
198  osrl = osagent->retrieve(osol);
199  cout << osrl << endl << endl;
200  }
201  else {
202  cout << "process still running; kill it!" << endl;
203  osrl = osagent->kill(osol);
204  cout << osrl << endl << endl;
205  };
206  }
207  else {
208  cout << "Improperly formed string" << endl;
209  };
210 
211  // do garbage collection
212  delete osinstance;
213  osinstance = NULL;
214  delete osilwriter;
215  osilwriter = NULL;
216  delete osagent;
217  osagent = NULL;
218 
219  cout << "Done with garbage collection" << endl;
220  cout << "Program terminates normally" << endl;
221  return 0;
222  //
223  }
224  catch(const ErrorClass& eclass){
225 
226  std::cout << eclass.errormsg << std::endl;
227  return 0;
228  }
229 }// end main
230 
double * values
Used by a client to invoke a remote solver.
Definition: OSSolverAgent.h:41
bool addVariable(int index, std::string name, double lowerBound, double upperBound, char type)
add a variable.
std::string retrieve(std::string osol)
implement the retrieve() method which is a virtual function in OShL
bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, double *values, int valuesBegin, int valuesEnd, int *indexes, int indexesBegin, int indexesEnd, int *starts, int startsBegin, int startsEnd)
set linear constraint coefficients
bool send(std::string osil, std::string osol)
implement the send() method which is a virtual function in OShL
std::string errormsg
errormsg is the error that is causing the exception to be thrown
Definition: OSErrorClass.h:42
int main(int argc, char *argv[])
Definition: BB_tm.cpp:32
std::string solve(std::string osil, std::string osol)
implement the solve() method which is a virtual function in OShL, this is synchronous ...
bool setConstraintNumber(int number)
set the number of constraints.
bool bDeleteArrays
bDeleteArrays is true if we delete the arrays in garbage collection set to true by default ...
Definition: OSGeneral.h:149
bool setObjectiveNumber(int number)
set the number of objectives.
bool addConstraint(int index, std::string name, double lowerBound, double upperBound, double constant)
add a constraint.
bool addObjective(int index, std::string name, std::string maxOrMin, double constant, double weight, SparseVector *objectiveCoefficients)
add an objective.
const double OSDBL_MAX
Definition: OSParameters.h:93
a sparse vector data structure
Definition: OSGeneral.h:122
std::string knock(std::string ospl, std::string osol)
implement the knock() method which is a virtual function in OShL
bool setInstanceSource(std::string source)
set the instance source.
bool setInstanceDescription(std::string description)
set the instance description.
std::string writeOSiL(const OSInstance *theosinstance)
create an osil string from an OSInstance object
Definition: OSiLWriter.cpp:40
double * values
values holds a double array of nonzero values.
Definition: OSGeneral.h:164
std::string kill(std::string osol)
implement the kill() method which is a virtual function in OShL
std::string getJobID(std::string osol)
implement the getJobID() method which is a virtual function in OShL
int * indexes
indexes holds an integer array of indexes whose corresponding values are nonzero. ...
Definition: OSGeneral.h:159
bool setVariableNumber(int number)
set the number of variables.
The in-memory representation of an OSiL instance..
Definition: OSInstance.h:2262
OSInstance * osinstance
used for throwing exceptions.
Definition: OSErrorClass.h:31
Take an OSInstance object and write a string that validates against the OSiL schema.
Definition: OSiLWriter.h:29