My Project
OSAmplClient_old.cpp
Go to the documentation of this file.
1
60#include "OSConfig.h"
61#include "OSnl2OS.h"
62#include "OSosrl2ampl.h"
63#include "OSiLReader.h"
64#include "OSiLWriter.h"
65#include "OSoLReader.h"
66#include "OSoLWriter.h"
67#include "OSrLReader.h"
68#include "OSrLWriter.h"
69#include "OSInstance.h"
70#include "OSOption.h"
71#include "OSResult.h"
72#include "OSOutput.h"
73
74#include "OSDefaultSolver.h"
75#include "OSCoinSolver.h"
76
77#ifdef COIN_HAS_LINDO
78# include "OSLindoSolver.h"
79#endif
80
81#ifdef COIN_HAS_IPOPT
82# include "OSIpoptSolver.h"
83#endif
84
85#ifdef COIN_HAS_BONMIN
86# include "OSBonminSolver.h"
87#endif
88
89#ifdef COIN_HAS_COUENNE
90# include "OSCouenneSolver.h"
91#endif
92
93#include "OSFileUtil.h"
94#include "OSSolverAgent.h"
95#include "OShL.h"
96#include "OSErrorClass.h"
97#include "CoinError.hpp"
98#include "OSCommandLine.h"
99#include "OSRunSolver.h"
100#include <sstream>
101
102#ifdef HAVE_CSTRING
103# include <cstring>
104#else
105# ifdef HAVE_STRING_H
106# include <string.h>
107# else
108# error "don't have header file for string"
109# endif
110#endif
111
112#include "CoinError.hpp"
113#include "CoinHelperFunctions.hpp"
114
115
116#include <iostream>
117//AMPL includes must be last.
118#include <asl.h>
119
120
121
122
123#define MAXCHARS 5000
124
126YY_BUFFER_STATE osss_scan_string(const char* osss, void* scanner );
127//void osssset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner );
128void setyyextra(OSCommandLine *oscommandline, void* scanner);
129int ossslex(void* scanner );
130int ossslex_init(void** ptr);
131int ossslex_destroy (void* scanner );
132
133
134using std::cerr;
135using std::cout;
136using std::endl;
137using std::ostringstream;
138
139void getAmplClientOptions(char *options, std::string *solverName,
140 std::string *optionFile, std::string *serviceLocation);
141
142
143
144int main(int argc, char **argv)
145{
146 WindowsErrorPopupBlocker();
147 char *stub;
148 std::ostringstream outStr;
149
150 /* Parse the options (passed through ASL as the string OSAmplClient_options)
151 *
152 * There are three possible options:
153 * 1. solver:
154 * possible values - name of a supported solver (installation-dependent)
155 * 2. serviceLocation:
156 * possible values - NULL (empty) or URL of the solver service
157 * 3. optionFile:
158 * specify the location of the OSoL file (on the local system)
159 *
160 */
161 char *amplclient_options = NULL;
162 DefaultSolver *solverType = NULL;
163 std::string sSolverName = "";
164 std::string osolFileName = "";
165 std::string osol = "";
166 std::string serviceLocation = "";
167 std::string nlfile = "";
168
169#ifndef NDEBUG
170 outStr.str("");
171 outStr.clear();
172 outStr << "Here is the command line:";
173 for (int i=0; i<argc; i++) outStr << " " << argv[i];
174 outStr << endl;
176#endif
177
178 stub = argv[1];
179
180 amplclient_options = getenv("OSAmplClient_options");
181 if( amplclient_options != NULL)
182 {
183 outStr.str("");
184 outStr.clear();
185 outStr << "HERE ARE THE AMPLCLIENT OPTIONS " << amplclient_options << endl;
187 getAmplClientOptions(amplclient_options, &sSolverName, &osolFileName, &serviceLocation);
188 }
189
190 //convert solver name to lower case for testing purposes
191
192 unsigned int k;
193 for(k = 0; k < sSolverName.length(); k++)
194 {
195 sSolverName[ k] = tolower( sSolverName[ k]);
196 }
197 //std::cout << " solver Name = " << sSolverName << std::endl;
198
199 /* If an OSoL file was given, read it into a string (don't parse) */
200 if(osolFileName.size() > 0)
201 {
202 FileUtil *fileUtil;
203 fileUtil = new FileUtil();
204 osol = fileUtil->getFileAsString( osolFileName.c_str() );
205 delete fileUtil;
206 }
207 //std::cout << " solver Options = " << osol << std::endl;
208
209 // set AMPL structures: cw for column-wise representation, rw for row-wise; asl to switch between them
210 ASL *cw, *rw, *asl;
211 cw = ASL_alloc(ASL_read_fg);
212 rw = ASL_alloc(ASL_read_fg);
213 asl = cw;
214
215 jac0dim((char*)stub, (fint)strlen(stub));
216
217 OSnl2OS *nl2OS = new OSnl2OS(cw, rw, asl);
218 //std::cout << " call nl2OS" << std::endl;
219
220
225 try
226 {
227 nl2OS->readNl(stub);
228 nl2OS->setOsol(osol);
229 nl2OS->createOSObjects() ;
230 }
231 catch(const ErrorClass& eclass)
232 {
233 outStr.str("");
234 outStr.clear();
235 outStr << "Error detected: " << eclass.errormsg << std::endl;
237 return 0;
238 }
239 //std::cout << " return from nl2OS" << std::endl;
240
241 // create OS objects
242 //OSInstance *osinstance;
243 //OSOption *osoption;
244
245 //osinstance = nl2OS->osinstance;
246 //std::cout << " osinstance created" << std::endl;
247
248 //write out the instance
249#ifndef NDEBUG
250 OSiLWriter *osilwriter = NULL;
251 osilwriter = new OSiLWriter();
252 osilwriter->m_bWhiteSpace = true;
253 std::string sModelInstanceName = "modelInstance.osil";
254 FileUtil *fileUtil;
255 fileUtil = new FileUtil();
256 fileUtil->writeFileFromString(sModelInstanceName, osilwriter->writeOSiL(nl2OS->osinstance) );
257 delete fileUtil;
258 fileUtil = NULL;
259 delete osilwriter;
260 osilwriter = NULL;
261#endif
262
263
264 if (nl2OS->osoption != NULL)
265 {
266 //osoption = nl2OS->osoption;
267
268 //write out the options
269#ifndef NDEBUG
270 OSoLWriter *osolwriter = NULL;
271 osolwriter = new OSoLWriter();
272 //osolwriter->m_bWhiteSpace = true;
273 std::string sModelOptionName = "modelOptions.osol";
274 fileUtil = new FileUtil();
275 fileUtil->writeFileFromString(sModelOptionName, osolwriter->writeOSoL(nl2OS->osoption) );
276 delete fileUtil;
277 fileUtil = NULL;
278 delete osolwriter;
279 osolwriter = NULL;
280#endif
281 }
282
283 OSrLReader *osrlreader = NULL;
284 OSrLWriter *osrlwriter;
285 osrlwriter = new OSrLWriter();
286 OSResult *osresult = NULL;
287 std::string osrl = "";
288
289 bool writeOK;
290 OSosrl2ampl *solWriter = new OSosrl2ampl();
291
292 try
293 {
294 if(serviceLocation.size() == 0 )
295 {
296 //determine the solver and do a local solve
297 if (nl2OS->osoption == NULL)
298 osrl = runSolver(sSolverName, osol, nl2OS->osinstance);
299 else
300 osrl = runSolver(sSolverName, nl2OS->osoption, nl2OS->osinstance);
301 }// end if serviceLocation.size() == 0
302
303 /* ------------------------------------------------------- */
304 else // do a remote solve
305 {
306 OSSolverAgent* osagent = NULL;
307 OSiLWriter *osilwriter = NULL;
308 osilwriter = new OSiLWriter();
309 std::string osil = osilwriter->writeOSiL(nl2OS->osinstance);
311
312 //agent_address = strstr(solver_option, "service");
313 //agent_address += 7;
314 //URL = strtok( agent_address, delims );
315 //std::string sURL = URL;
317 // we should be pointing to the start of the address
318 osagent = new OSSolverAgent( serviceLocation);
319 outStr.str("");
320 outStr.clear();
321 outStr << "Place remote synchronous call: " << serviceLocation << endl << endl << endl;
323
324 osrl = osagent->solve(osil, osol);
325 if (osrl.size() == 0) throw ErrorClass("Nothing was returned from the server, please check service address");
326 delete osilwriter;
327 delete osagent;
328 }
329 }//end try
330 catch(const ErrorClass& eclass)
331 {
332 osresult = new OSResult();
335 osrl = osrlwriter->writeOSrL( osresult);
337// osrl = " ";
338// write_sol(const_cast<char*>(osrl.c_str()), NULL, NULL, NULL);
339
340 writeOK = solWriter->writeSolFile(osrl, nl2OS->getASL("asl"));
341
342 delete solWriter;
343 solWriter = NULL;
344
345 delete osresult;
346 osresult = NULL;
347 return 0;
348 }
349
350 try // now put solution back to ampl
351 {
352 //need_nl = 0;
353 std::string sResultFileName = "solutionResult.osrl";
354 FileUtil *fileUtil;
355 fileUtil = new FileUtil();
356 fileUtil->writeFileFromString(sResultFileName, osrl);
357 delete fileUtil;
358 std::string::size_type pos1 = osrl.find( "error");
359 if(pos1 == std::string::npos)
360 {
361 std::string sReport = "model was solved";
362 osrlreader = new OSrLReader();
363 osresult = osrlreader->readOSrL( osrl);
364
365 writeOK = solWriter->writeSolFile(osrl, nl2OS->getASL("asl"));
366
367 delete osrlreader;
368 osrlreader = NULL;
369 }
370 else // there was an error
371 {
373 writeOK = solWriter->writeSolFile(osrl, nl2OS->getASL("asl"));
374 }
375 }
376 catch(const ErrorClass& eclass)
377 {
378 outStr.str("");
379 outStr.clear();
380 outStr << "There was an error parsing the OSrL string" << endl << eclass.errormsg << endl << endl;
382 }
383
384 if (solverType != NULL)
385 {
386 //cout << "TRY TO DELETE solverType" <<endl;
387 delete solverType;
388 //cout << "solverType JUST DELETED" <<endl;
389 solverType = NULL;
390 }
391 delete osrlwriter;
392 //cout << "osrlwriter JUST DELETED" <<endl;
393 osrlwriter = NULL;
394 delete nl2OS;
395 //cout << "nl2OS JUST DELETED" <<endl;
396 nl2OS = NULL;
397// ASL_free(&asl);
398 if (solWriter != NULL) delete solWriter;
399 solWriter = NULL;
400 return 0;
401} // end main
402
403
404
405
406void getAmplClientOptions(char *amplclient_options, std::string *solverName,
407 std::string *solverOptions, std::string *serviceLocation)
408{
409 std::ostringstream outStr;
410
411 // initialize the OS command line structure
412 OSCommandLine *oscommandline = new OSCommandLine();
413
414 void* scanner;
415
416 try
417 {
418 //cout << "Input String = " << amplclient_options << endl;
420 //std::cout << "Call Text Extra" << std::endl;
421 setyyextra( oscommandline, scanner);
422 //std::cout << "Call scan string " << std::endl;
423 osss_scan_string( amplclient_options, scanner);
424 //std::cout << "call ossslex" << std::endl;
427 //std::cout << "done with call to ossslex" << std::endl;
428
429 *solverName = oscommandline->solverName;
430 *solverOptions = oscommandline->osolFile;
431 *serviceLocation = oscommandline->serviceLocation;
432
433 }//end try
434 catch(const ErrorClass& eclass)
435 {
436 outStr.str("");
437 outStr.clear();
438 outStr << "There was an error processing OSAmplClient options: " << endl << eclass.errormsg << endl << endl;
440 throw ErrorClass( eclass.errormsg) ;
441 }
442}//getAmplClientOptions
const OSSmartPtr< OSOutput > osoutput
Definition OSOutput.cpp:39
int main(int argc, char **argv)
struct yy_buffer_state * YY_BUFFER_STATE
int ossslex_destroy(void *scanner)
int ossslex_init(void **ptr)
YY_BUFFER_STATE osss_scan_string(const char *osss, void *scanner)
void getAmplClientOptions(char *options, std::string *solverName, std::string *optionFile, std::string *serviceLocation)
void setyyextra(OSCommandLine *oscommandline, void *scanner)
int ossslex(void *scanner)
#define scanner
std::string runSolver(std::string solverName, std::string osol, OSInstance *osinstance)
This class is used to invoke a solver locally.
The Default Solver Class.
used for throwing exceptions.
std::string errormsg
errormsg is the error that is causing the exception to be thrown
class used to make it easy to read and write files.
Definition OSFileUtil.h:38
bool writeFileFromString(char *fname, std::string thestring)
write a file from an input string.
std::string getFileAsString(const char *fname)
read a file and return contents as a string.
This class is used to store command line options for the OSSolverService executable and to provide me...
std::string serviceLocation
serviceLocation is the URL of the remote solver when a local solver is not used
std::string solverName
the name of the solver to be invoked locally, e.g -solver Ipopt
std::string osolFile
osolFile is the name of the file that holds the solver options in OSoL format
The Result Class.
Definition OSResult.h:2549
bool setGeneralMessage(std::string message)
Set the general message.
bool setGeneralStatusType(std::string type)
Set the general status type, which can be: success, error, warning.
Used by a client to invoke a remote solver.
std::string solve(std::string osil, std::string osol)
implement the solve() method which is a virtual function in OShL, this is synchronous
Take an OSInstance object and write a string that validates against the OSiL schema.
Definition OSiLWriter.h:30
std::string writeOSiL(const OSInstance *theosinstance)
create an osil string from an OSInstance object
bool m_bWhiteSpace
m_bWhiteSpace is set to true if we write white space in the file
Definition OSiLWriter.h:68
The OSnl2OS Class.
Definition OSnl2OS.h:79
ASL * getASL(std::string name)
return a pointer to an ASL object
Definition OSnl2OS.cpp:87
OSInstance * osinstance
osinstance is a pointer to the OSInstance object that gets created from the information in the nl fil...
Definition OSnl2OS.h:172
bool readNl(std::string stub)
read the nl file
Definition OSnl2OS.cpp:109
bool createOSObjects()
create an OSInstance and OSOption representation from the AMPL nl content (Some of the information in...
Definition OSnl2OS.cpp:504
OSOption * osoption
osoption is a pointer to the OSOption object that gets created from the information in the nl file (a...
Definition OSnl2OS.h:177
void setOsol(std::string osol)
set the osol string
Definition OSnl2OS.cpp:99
Take an OSOption object and write a string that validates against the OSoL schema.
Definition OSoLWriter.h:30
std::string writeOSoL(OSOption *theosoption)
create an osol string from an OSOption object
The OSosrl2ampl Class.
Definition OSosrl2ampl.h:45
bool writeSolFile(std::string osrl, ASL *asl, std::string filename)
Convert the solution to AMPL .sol format.
The OSrLReader Class.
Definition OSrLReader.h:43
OSResult * readOSrL(const std::string &posrl)
Get an OSResult object from an OSrL string.
Take an OSResult object and write a string that validates against OSrL.
Definition OSrLWriter.h:31
std::string writeOSrL(OSResult *theosresult)
create an osrl string from an OSResult object
@ ENUM_OUTPUT_LEVEL_debug
@ ENUM_OUTPUT_LEVEL_error
@ ENUM_OUTPUT_LEVEL_info
@ ENUM_OUTPUT_AREA_main
OSResult * osresult