My Project
OSAmplClient.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#ifndef COIN_HAS_ASL
78# error do not have ASL
79#endif
80
81#ifdef COIN_HAS_LINDO
82# include "OSLindoSolver.h"
83#endif
84
85#ifdef COIN_HAS_IPOPT
86# include "OSIpoptSolver.h"
87#endif
88
89#ifdef COIN_HAS_BONMIN
90# include "OSBonminSolver.h"
91#endif
92
93#ifdef COIN_HAS_COUENNE
94# include "OSCouenneSolver.h"
95#endif
96
97#include "OSFileUtil.h"
98#include "OSSolverAgent.h"
99#include "OShL.h"
100#include "OSErrorClass.h"
101#include "CoinError.hpp"
102#include "OSCommandLine.h"
103#include "OSCommandLineReader.h"
104#include "OSRunSolver.h"
105#include <sstream>
106
107#ifdef HAVE_CSTRING
108# include <cstring>
109#else
110# ifdef HAVE_STRING_H
111# include <string.h>
112# else
113# error "don't have header file for string"
114# endif
115#endif
116
117#include "CoinError.hpp"
118#include "CoinHelperFunctions.hpp"
119
120
121#include <iostream>
122//AMPL includes must be last.
123#include <asl.h>
124
125
126
128YY_BUFFER_STATE osss_scan_string(const char* osss, void* scanner );
129//void osssset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner );
130void setyyextra(OSCommandLine *oscommandline, void* scanner);
131int ossslex(void* scanner );
132int ossslex_init(void** ptr);
133int ossslex_destroy (void* scanner );
134
135
136using std::cerr;
137using std::cout;
138using std::endl;
139using std::ostringstream;
140
141std::string get_help();
142void list_options(OSCommandLine *oscommandline);
143
144// the serviceMethods
145void solve(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
146void getJobID(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
147void send(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
148void kill(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
149void retrieve(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
150void knock(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
151
152// additional methods
153bool findInstance( OSCommandLine *oscommandline, OSnl2OS *osnl2os);
154void makeStrings( OSCommandLine *oscommandline);
155void doPrintRow(OSInstance *osinstance, std::string rownumberstring);
156void reportResults(OSCommandLine *oscommandline, std::string osrl, OSnl2OS *osnl2OS);
157void reportErrors(OSCommandLine *oscommandline, std::string errMsg, OSnl2OS *osnl2OS);
158
159extern const OSSmartPtr<OSOutput> osoutput;
160
161
162int main(int argc, char **argv)
163{
164 WindowsErrorPopupBlocker();
165 std::ostringstream outStr;
166 OSnl2OS *nl2OS = new OSnl2OS();
167 OSiLReader* osilreader = new OSiLReader();
168 FileUtil *fileUtil = NULL;
169 ostringstream echo_cl;
170
171 // initialize the command line structure
172
173 OSCommandLine *oscommandline = NULL; // = new OSCommandLine();
174 OSCommandLineReader *oscommandlinereader = new OSCommandLineReader();
175
176// DefaultSolver *solverType = NULL;
177 char* stub = NULL;
178 if (argc > 0) stub = argv[1];
179
184 ostringstream temp2;
185 std::string amplclient_options = "";
186 if (argc > 2)
187 {
188 if (strncmp(argv[2],"-AMPL",5) == 0)
189 {
190 char* temp = getenv("OSAmplClient_options");
191 if (temp != NULL)
192 {
193 temp2 << temp;
194 }
195 }
196 else
197 {
198 for (int i=2; i<argc; i++)
199 temp2 << argv[i] << " ";
200 }
201 amplclient_options = temp2.str();
202 }
203
204 // getting the OSAmplClient_options into a std::string is a bit of a pain...
205
206
207 // this output must be held in abeyance until the command line
208 // has been processed and printLevel has been set...
209#ifndef NDEBUG
210 echo_cl << "HERE ARE THE AMPLCLIENT OPTIONS ";
211 echo_cl << amplclient_options;
212 echo_cl << endl << endl;
213
214 echo_cl << "Try to open file ";
215 echo_cl << stub << ".nl";
216 echo_cl << endl;
217#endif
218
219 // Now read the command line
220 try
221 {
222 oscommandline = oscommandlinereader->readCommandLine(amplclient_options);
223 }
224
225 catch (const ErrorClass& eclass)
226 {
227 reportErrors(oscommandline, eclass.errormsg, nl2OS);
228 delete oscommandlinereader;
229 oscommandlinereader = NULL;
230 return 1;
231 }
232
233 if (stub) oscommandline->nlFile = stub;
234
235
238 try
239 {
240 outStr.str("");
241 outStr.clear();
242 outStr << std::endl << "using print level " << oscommandline->printLevel << " for stdout" << std::endl;
243
244 if (oscommandline->printLevel != DEFAULT_OUTPUT_LEVEL)
245 {
246 osoutput->SetPrintLevel("stdout", (ENUM_OUTPUT_LEVEL)oscommandline->printLevel);
247
248#ifndef NDEBUG
250#endif
252 }
253#ifndef NDEBUG
254 else
255 {
258 }
259#endif
260
261 if (oscommandline->logFile != "")
262 {
263 int status = osoutput->AddChannel(oscommandline->logFile);
264
265 switch(status)
266 {
267 case 0:
269 "Added channel " + oscommandline->logFile);
270 break;
271 case 1:
273 "Output channel " + oscommandline->logFile + " previously defined");
274 break;
275 default:
276 throw ErrorClass("Could not add output channel " + oscommandline->logFile);
277 }//end switch
278
279
280 outStr.str("");
281 outStr.clear();
282 outStr << std::endl << "using print level " << oscommandline->filePrintLevel;
283 outStr << " for " << oscommandline->logFile << std::endl;
284
285 if (oscommandline->filePrintLevel != DEFAULT_OUTPUT_LEVEL)
286 {
287 osoutput->SetPrintLevel(oscommandline->logFile, (ENUM_OUTPUT_LEVEL)oscommandline->filePrintLevel);
288 }
289 else
290 {
292 }
293 }
294
295 if (oscommandline->invokeHelp == true)
296 {
297 outStr.str("");
298 outStr.clear();
299 outStr << std::endl << std::endl << get_help() << std::endl;
301
302 delete oscommandlinereader;
303 oscommandlinereader = NULL;
304 return 0;
305 }
306
307 if (oscommandline->writeVersion == true)
308 {
309 outStr.str("");
310 outStr.clear();
311 outStr << std::endl << std::endl << OSgetVersionInfo() << std::endl;
313
314 delete oscommandlinereader;
315 oscommandlinereader = NULL;
316 return 0;
317 }
318 }
319 catch (const ErrorClass& eclass)
320 {
321 reportErrors(oscommandline, eclass.errormsg, nl2OS);
322 delete oscommandlinereader;
323 oscommandlinereader = NULL;
324 return 1;
325 }
326
327#ifndef NDEBUG
328 outStr.str("");
329 outStr.clear();
330
331 outStr << "HERE ARE THE OPTION VALUES:" << endl;
332 if(oscommandline->configFile != "") outStr << "Config file = " << oscommandline->configFile << endl;
333 if(oscommandline->osilFile != "") outStr << "OSiL file = " << oscommandline->osilFile << endl;
334 if(oscommandline->osolFile != "") outStr << "OSoL file = " << oscommandline->osolFile << endl;
335 if(oscommandline->osrlFile != "") outStr << "OSrL file = " << oscommandline->osrlFile << endl;
336 //if(oscommandline->insListFile != "") outStr << "Instruction List file = " << oscommandline->insListFile << endl;
337 if(oscommandline->osplInputFile != "") outStr << "OSpL Input file = " << oscommandline->osplInputFile << endl;
338 if(oscommandline->serviceMethod != "") outStr << "Service Method = " << oscommandline->serviceMethod << endl;
339 if(oscommandline->mpsFile != "") outStr << "MPS File Name = " << oscommandline->mpsFile << endl;
340 if(oscommandline->nlFile != "") outStr << "NL File Name = " << oscommandline->nlFile << endl;
341 if(oscommandline->gamsControlFile != "") outStr << "gams Control File Name = " << oscommandline->gamsControlFile << endl;
342 if(oscommandline->browser != "") outStr << "Browser Value = " << oscommandline->browser << endl;
343 if(oscommandline->solverName != "") outStr << "Selected Solver = " << oscommandline->solverName << endl;
344 if(oscommandline->serviceLocation != "") outStr << "Service Location = " << oscommandline->serviceLocation << endl;
345 if(oscommandline->jobID != "") outStr << "Job ID = " << oscommandline->jobID << endl;
346 if(oscommandline->printModel) outStr << "print model prior to solve/send" << endl;
347 if(oscommandline->printRowNumberAsString != "") outStr << "print model row " << oscommandline->printRowNumberAsString << " prior to solve/send" << endl;
348 outStr << "print level for stdout: " << oscommandline->printLevel << endl;
349 if(oscommandline->logFile != "")
350 {
351 outStr << "also send output to " << oscommandline->logFile << endl;
352 outStr << "print level for file output: " << oscommandline->filePrintLevel << endl;
353 }
354
356#endif
357
358
359 //convert solver name to lower case so there is no ambiguity
360 unsigned int k;
361 for (k = 0; k < oscommandline->solverName.length(); k++)
362 {
363 oscommandline->solverName[k] = (char)tolower(oscommandline->solverName[k]);
364 }
365
366 // now call the correct serviceMethod
367 // solve is the default
368 if (oscommandline->serviceMethod == "") oscommandline->serviceMethod = "solve";
369
370 try
371 {
372 // for local use only the solve method is available
373 if (oscommandline->serviceLocation == "")
374 if (oscommandline->serviceMethod != "solve")
375 throw ErrorClass("No serviceLocation given. Only \'solve\' is available locally.");
376
377 if (oscommandline->serviceMethod[0] == 's')
378 {
379 // for solve or send commands we must have an instance
380 if (oscommandline->osil == "")
381 {
382 if (!findInstance(oscommandline, nl2OS))
383 throw ErrorClass("No instance could be found");
384 }
385
386 // write out the model or portion thereof, if directed by the user
387 if (oscommandline->printModel == true)
388 {
389 if (oscommandline->osinstance == NULL)
390 {
391 oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
392 }
394 oscommandline->osinstance->printModel());
395 }
396 else if (oscommandline->printRowNumberAsString != "")
397 {
398 if (oscommandline->osinstance == NULL)
399 {
400 oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
401 }
402 doPrintRow(oscommandline->osinstance, oscommandline->printRowNumberAsString);
403 }
404
405 // write the instance to an OSiL file, if directed by the user
406 if (oscommandline->osilOutputFile != "")
407 {
408 fileUtil = new FileUtil();
409 if (oscommandline->osil == "")
410 {
411 OSiLWriter* osilwriter = new OSiLWriter();
412 oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
413 delete osilwriter;
414 osilwriter = NULL;
415 }
416 fileUtil->writeFileFromString(oscommandline->osilOutputFile, oscommandline->osil);
417 delete fileUtil;
418 fileUtil = NULL;
419 }
420
421 // write the solver options to an OSoL file, if directed by the user
422 if (oscommandline->osolOutputFile != "")
423 {
424 fileUtil = new FileUtil();
425 if (oscommandline->osol != "" && oscommandline->osoption == NULL)
426 fileUtil->writeFileFromString(oscommandline->osolOutputFile, oscommandline->osol);
427 else
428 {
429 if (oscommandline->osoption == NULL)
430 oscommandline->osoption = new OSOption();
431 OSoLWriter* osolwriter = new OSoLWriter();
432 fileUtil->writeFileFromString(oscommandline->osolOutputFile,
433 osolwriter->writeOSoL(oscommandline->osoption));
434 delete osolwriter;
435 osolwriter = NULL;
436 }
437 delete fileUtil;
438 fileUtil = NULL;
439 }
440
441 // if no serviceLocation was given, do a local solve
442 if (oscommandline->serviceLocation == "")
443 {
444 std::string osrl;
445 if (oscommandline->osinstance != NULL)
446 if (oscommandline->osoption != NULL)
447 osrl = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osinstance);
448 else
449 osrl = runSolver(oscommandline->solverName, oscommandline->osol, oscommandline->osinstance);
450 else
451 if (oscommandline->osoption != NULL)
452 osrl = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osil);
453 else
454 osrl = runSolver(oscommandline->solverName, oscommandline->osol, oscommandline->osil);
455 reportResults(oscommandline, osrl, nl2OS);
456 }
457
458 // remote solve or send
459 else
460 {
461 if (oscommandline->serviceMethod[1] == 'e')
462 send(oscommandline, nl2OS);
463 else
464 solve(oscommandline, nl2OS);
465 }
466 }
467
468 else //call one of the other methods
469 {
470 switch (oscommandline->serviceMethod[0])
471 {
472 case 'g':
473 getJobID(oscommandline, nl2OS);
474 break;
475 case 'r':
476 retrieve(oscommandline, nl2OS);
477 break;
478 case 'k':
479 if (oscommandline->serviceMethod[1] == 'i')
480 kill(oscommandline, nl2OS);
481 else
482 knock(oscommandline, nl2OS);
483 break;
484 default:
485 break;
486 }
487 }
488
489 // garbage collection
490 if (osilreader != NULL)
491 delete osilreader;
492 oscommandline->osinstance = NULL;
493 osilreader = NULL;
494 delete oscommandlinereader;
495 oscommandlinereader = NULL;
496 delete fileUtil;
497 fileUtil = NULL;
498
499 if (nl2OS != NULL)
500 delete nl2OS;
501 nl2OS = NULL;
502
503 return 0;
504 }
505
506 catch (const ErrorClass& eclass)
507 {
508 reportErrors(oscommandline, eclass.errormsg, nl2OS);
509 if (osilreader != NULL)
510 delete osilreader;
511 oscommandline->osinstance = NULL;
512 osilreader = NULL;
513 delete oscommandlinereader;
514 oscommandlinereader = NULL;
515 delete fileUtil;
516 fileUtil = NULL;
517
518 if (nl2OS != NULL)
519 delete nl2OS;
520 nl2OS = NULL;
521 if ( (argc > 2) && (strncmp(argv[2],"-AMPL",5) == 0) )
522 return 0;
523 else
524 return 1;
525 }
526}// end of main()
527
528
535bool findInstance(OSCommandLine *oscommandline, OSnl2OS *nl2os)
536{
537// FileUtil *fileUtil = NULL;
538// fileUtil = new FileUtil();
539
540 try
541 {
542// nl2os = new OSnl2OS();
543 if (!nl2os->readNl(oscommandline->nlFile))
544 throw ErrorClass("Error reading .nl file.");
545 nl2os->setOsol(oscommandline->osol);
546 nl2os->setJobID(oscommandline->jobID);
547 nl2os->createOSObjects();
548 oscommandline->osinstance = nl2os->osinstance;
549 return true;
550 }
551 catch (const ErrorClass& eclass)
552 {
554 //throw ErrorClass(eclass.errormsg);
555 return false;
556 }
557}//findInstance
558
559void makeStrings(OSCommandLine *oscommandline)
560{
561 // convert the osinstance and osoption objects to strings
562 if (oscommandline->osil == "")
563 {
564 OSiLWriter *osilwriter = new OSiLWriter();
565 oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
566#ifndef NDEBUG
568#endif
569 delete osilwriter;
570 osilwriter = NULL;
571 }
572
578 OSoLReader *osolreader = NULL;
579 if (oscommandline->osoption != NULL || oscommandline->jobID != "" || oscommandline->osol == "")
580 {
581 if (oscommandline->osoption == NULL)
582 {
583 oscommandline->osoption = new OSOption();
584 if (oscommandline->osol != "")
585 {
586 osolreader = new OSoLReader();
587 oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
588 }
589 if (oscommandline->jobID != "")
590 oscommandline->osoption->setJobID(oscommandline->jobID);
591 }
592
593 OSoLWriter *osolwriter = new OSoLWriter();
594 oscommandline->osol = osolwriter->writeOSoL(oscommandline->osoption);
595#ifndef NDEBUG
597#endif
598 delete osolwriter;
599 osolwriter = NULL;
600 }
601 if (osolreader != NULL)
602 {
603 delete osolreader;
604 oscommandline->osoption = NULL; // no longer needed, and memory may have just been freed
605 }
606 osolreader = NULL;
607}
608
614void solve(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
615{
616 std::string osrl = "";
617 OSSolverAgent* osagent = NULL;
618 FileUtil *fileUtil = NULL;
619 fileUtil = new FileUtil();
620
621 try
622 {
623 // place a remote call
624 osagent = new OSSolverAgent(oscommandline->serviceLocation);
625
626 //no need to worry about jobID, but make sure we have strings
627 oscommandline->jobID = "";
628 makeStrings(oscommandline);
629
630 osrl = osagent->solve(oscommandline->osil, oscommandline->osol);
631 delete osagent;
632 osagent = NULL;
633
634 reportResults(oscommandline, osrl, nl2OS);
635
636 //garbage collection
637 delete fileUtil;
638 fileUtil = NULL;
639
640 }//end try
641 catch (const ErrorClass& eclass)
642 {
643 reportErrors(oscommandline, eclass.errormsg, nl2OS);
644
645 //garbage collection
646 delete fileUtil;
647 fileUtil = NULL;
648 }//end local catch
649
650}//end solve
651
652
653void send(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
654{
655 bool bSend = false;
656 bool always_print = false;
657 OSSolverAgent* osagent = NULL;
658 ostringstream outStr;
659
660 try
661 {
662 osagent = new OSSolverAgent(oscommandline->serviceLocation);
663
664
665 // get a job ID if necessary
666 if (oscommandline->jobID == "NEW")
667 {
668 always_print = true;
669 oscommandline->jobID = osagent->getJobID("");
670 if (oscommandline->osoption != NULL)
671 oscommandline->osoption->setJobID(oscommandline->jobID);
672 }
673
674 makeStrings(oscommandline);
675
676 outStr.str("");
677 outStr.clear();
678 outStr << "Submitting Job " << oscommandline->jobID << std::endl;
680
681 bSend = osagent->send(oscommandline->osil, oscommandline->osol);
682
683 outStr.str("");
684 outStr.clear();
685 outStr << "Job " << oscommandline->jobID;
686
687 if (bSend == true)
688 {
689 outStr << " successfully submitted." << std::endl;
690 if (always_print)
692 else
694 }
695 else
696 {
697 outStr << ": send failed." << std::endl;
698 outStr << "Check to make sure you sent a jobID not on the system." << std::endl;
700 }
701
702 delete osagent;
703 osagent = NULL;
704 }
705
706 catch (const ErrorClass& eclass)
707 {
708 reportErrors(oscommandline, eclass.errormsg, nl2OS);
709 if (osagent != NULL)
710 delete osagent;
711 osagent = NULL;
712 }
713}//end send
714
715void getJobID(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
716{
717 OSSolverAgent* osagent = NULL;
718 try
719 {
720 if (oscommandline->serviceLocation != "")
721 {
722 osagent = new OSSolverAgent(oscommandline->serviceLocation);
723 oscommandline->jobID = osagent->getJobID(oscommandline->osol);
725 delete osagent;
726 osagent = NULL;
727 }
728 else
729 {
730 delete osagent;
731 osagent = NULL;
732 throw ErrorClass("please specify service location (url)");
733 }
734 }
735
736 catch (const ErrorClass& eclass)
737 {
738 reportErrors(oscommandline, eclass.errormsg, nl2OS);
739 if (osagent != NULL)
740 delete osagent;
741 osagent = NULL;
742 }
743}//end getJobID
744
745
746void knock(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
747{
748 std::string osplOutput = "";
749 OSSolverAgent* osagent = NULL;
750 FileUtil *fileUtil = NULL;
751 fileUtil = new FileUtil();
752 try
753 {
754 if (oscommandline->serviceLocation != "")
755 {
756 osagent = new OSSolverAgent(oscommandline->serviceLocation);
757
758 // if no OSpL file was given, make a default one
759 if (oscommandline->osplInput == "")
760 {
761 std::ostringstream temp;
762 temp << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" <<
763 "<ospl xmlns=\"os.optimizationservices.org\"\n" <<
764 " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" <<
765 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" <<
766 " xsi:schemaLocation=\"os.optimizationservices.org\n" <<
767 " http://www.optimizationservices.org/schemas/OSpL.xsd\">\n"<<
768 " <processHeader>\n" <<
769 " <request action=\"getAll\"/>\n" <<
770 " </processHeader>\n" <<
771 " <processData/>\n" <<
772 "</ospl>\n";
773 oscommandline->osplInput = temp.str();
774 }
775
776 // if a jobID was given on the command line, use it
777 if(oscommandline->jobID != "")
778 {
779 OSOption *osOption = NULL;
780 if (oscommandline->osol == "")
781 {
782 osOption = new OSOption();
783 }
784 else
785 {
786 OSoLReader *osolReader = new OSoLReader();
787 try
788 {
789 osOption = osolReader->readOSoL(oscommandline->osol);
790 delete osolReader;
791 osolReader = NULL;
792 }
793 catch (const ErrorClass& eclass)
794 {
795 if (osolReader != NULL) delete osolReader;
796 osolReader = NULL;
797 throw ErrorClass(eclass.errormsg);
798 }
799 }
800 osOption->setJobID( oscommandline->jobID);
801 OSoLWriter *osolWriter = new OSoLWriter();
802 oscommandline->osol = osolWriter->writeOSoL( osOption);
803 delete osOption;
804 osOption = NULL;
805 delete osolWriter;
806 osolWriter = NULL;
807 }
808
809 osplOutput = osagent->knock(oscommandline->osplInput, oscommandline->osol);
810 if (oscommandline->osplOutputFile != "")
811 fileUtil->writeFileFromString(oscommandline->osplOutputFile,
812 osplOutput);
813 else
815 delete osagent;
816 }
817 else
818 {
819 delete osagent;
820 throw ErrorClass("please specify service location (url)");
821 }
822 delete fileUtil;
823 fileUtil = NULL;
824 }
825 catch (const ErrorClass& eclass)
826 {
827 reportErrors(oscommandline, eclass.errormsg, nl2OS);
828 if (osagent != NULL)
829 delete osagent;
830 osagent = NULL;
831
832 delete fileUtil;
833 fileUtil = NULL;
834 }
835}//end knock
836
837void retrieve(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
838{
839 FileUtil *fileUtil = NULL;
840 fileUtil = new FileUtil();
841 std::string osrl = "";
842 OSSolverAgent* osagent = NULL;
843
844 try
845 {
846 if (oscommandline->serviceLocation != "")
847 {
848 if (!nl2OS->readNl(oscommandline->nlFile))
849 throw ErrorClass("Error reading .nl file.");
850
851 osagent = new OSSolverAgent(oscommandline->serviceLocation);
852
853 if (oscommandline->osol == "")
854 {
855 // we need to construct the OSoL
856 OSOption *osOption = NULL;
857 osOption = new OSOption();
858 // get a jobId if necessary
859 if (oscommandline->jobID == "") throw ErrorClass("there is no JobID");
860 //set the jobID
861 osOption->setJobID( oscommandline->jobID);
862 // now read the osOption object into a string
863 OSoLWriter *osolWriter = NULL;
864 osolWriter = new OSoLWriter();
865 oscommandline->osol = osolWriter->writeOSoL( osOption);
866 delete osOption;
867 osOption = NULL;
868 delete osolWriter;
869 osolWriter = NULL;
870 }
871
872 osrl = osagent->retrieve(oscommandline->osol);
873 reportResults(oscommandline, osrl, nl2OS);
874 delete osagent;
875 osagent = NULL;
876 }
877 else
878 {
879 delete osagent;
880 osagent = NULL;
881 throw ErrorClass("please specify service location (url)");
882 }
883 delete fileUtil;
884 fileUtil = NULL;
885 }
886 catch (const ErrorClass& eclass)
887 {
888 reportErrors(oscommandline, eclass.errormsg, nl2OS);
889 if (osagent != NULL)
890 delete osagent;
891 osagent = NULL;
892
893 delete fileUtil;
894 fileUtil = NULL;
895 }
896}//end retrieve
897
898void kill(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
899{
900 FileUtil *fileUtil = NULL;
901 fileUtil = new FileUtil();
902 std::string osplOutput = "";
903 OSSolverAgent* osagent = NULL;
904 try
905 {
906 if (oscommandline->serviceLocation != "")
907 {
908 osagent = new OSSolverAgent(oscommandline->serviceLocation);
909
910 if (oscommandline->osol == "")
911 {
912 // we need to construct the OSoL
913 OSOption *osOption = NULL;
914 osOption = new OSOption();
915 // get a jobId if necessary
916 if (oscommandline->jobID == "") throw ErrorClass("there is no JobID");
917 //set the jobID
918 osOption->setJobID( oscommandline->jobID);
919 // now read the osOption object into a string
920 OSoLWriter *osolWriter = NULL;
921 osolWriter = new OSoLWriter();
922 oscommandline->osol = osolWriter->writeOSoL( osOption);
923 delete osOption;
924 osOption = NULL;
925 delete osolWriter;
926 osolWriter = NULL;
927 }
928
929 osplOutput = osagent->kill(oscommandline->osol);
930
931 if (oscommandline->osplOutputFile != "")
932 {
933 fileUtil->writeFileFromString(oscommandline->osplOutputFile, osplOutput);
935 "Results written to file " + oscommandline->osplOutputFile);
936 }
937 else
938 {
939 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, "kill command executed\n");
941 }
942 delete osagent;
943 osagent = NULL;
944 }
945 else
946 {
947 delete osagent;
948 osagent = NULL;
949 throw ErrorClass("please specify service location (url)");
950 }
951 delete fileUtil;
952 fileUtil = NULL;
953 }
954 catch (const ErrorClass& eclass)
955 {
956 reportErrors(oscommandline, eclass.errormsg, nl2OS);
957 if (osagent != NULL)
958 delete osagent;
959 osagent = NULL;
960
961 delete fileUtil;
962 fileUtil = NULL;
963 }
964}//end kill
965
966
967
970void reportResults(OSCommandLine *oscommandline, std::string osrl, OSnl2OS* nl2OS)
971{
972 ostringstream outStr;
973
974 if (oscommandline->osrlFile == "")
975 oscommandline->osrlFile = oscommandline->nlFile + ".osrl";
976
977 FileUtil* fileUtil = new FileUtil();
978 fileUtil->writeFileFromString(oscommandline->osrlFile, osrl);
979
980 if (oscommandline->browser != "")
981 {
982 std::string str = oscommandline->browser + " "
983 + oscommandline->osrlFile;
984 const char *ch = &str[0];
985 std::system(ch);
986 }
987
988 // now put solution back to ampl
989
990 OSosrl2ampl *solWriter = new OSosrl2ampl();
991
992 try
993 {
994 std::string::size_type pos1 = osrl.find( "error");
995 if(pos1 == std::string::npos)
996 {
997// OSrLReader *osrlreader = new OSrLReader();
998// OSResult *osresult = osrlreader->readOSrL( osrl);
999 solWriter->writeSolFile(osrl, nl2OS->getASL("asl"), oscommandline->nlFile + ".sol");
1000
1001// delete osrlreader;
1002// osrlreader = NULL;
1003 }
1004 else // there was an error
1005 {
1007 solWriter->writeSolFile(osrl, nl2OS->getASL("asl"), oscommandline->nlFile + ".sol");
1008 }
1009 if (fileUtil != NULL)
1010 delete fileUtil;
1011 fileUtil = NULL;
1012 }
1013 catch(const ErrorClass& eclass)
1014 {
1015 outStr.str("");
1016 outStr.clear();
1017 outStr << "There was an error parsing the OSrL string" << endl << eclass.errormsg << endl << endl;
1019 if (fileUtil != NULL)
1020 delete fileUtil;
1021 fileUtil = NULL;
1022 }
1023
1024}// reportResults
1025
1028void reportErrors(OSCommandLine *oscommandline, std::string errormsg, OSnl2OS* nl2OS)
1029{
1030 std::string osrl = "";
1031 OSResult *osresult = NULL;
1032 OSrLWriter *osrlwriter = NULL;
1033 //first check to see if we already have OSrL,
1034 //if so don't create a new osresult object
1035 std::string::size_type pos1 = errormsg.find( "<osrl");
1036 if(pos1 == std::string::npos)
1037 {
1038 osrlwriter = new OSrLWriter();
1039 osresult = new OSResult();
1040 osresult->setGeneralMessage(errormsg);
1042 osrl = osrlwriter->writeOSrL(osresult);
1043 }
1044 else
1045 {
1046 osrl = errormsg;
1047 }
1048 reportResults(oscommandline, osrl, nl2OS);
1049}// reportErrors
1050
1051
1052void doPrintRow(OSInstance *osinstance, std::string rownumberstring)
1053{
1054 ostringstream outStr;
1055 int rownumber;
1056 if (rownumberstring == "")
1058 "no row number given; print command ignored");
1059 else
1060 {
1061 try
1062 {
1063 rownumber = atoi((rownumberstring).c_str());
1064 }
1065 catch (const ErrorClass& eclass)
1066 {
1068 "invalid row number; print command ignored");
1069 }
1070
1071 if (osinstance == NULL)
1072 {
1074 "no instance defined; print command ignored");
1075 }
1076 else
1077 {
1078 outStr << std::endl << "Row " << rownumber << ":" << std::endl << std::endl;
1079 outStr << osinstance->printModel(rownumber) << std::endl;
1081 }
1082 }
1083}// doPrintRow(OSInstance *osinstance, std::string rownumberstring)
1084
1085
1086std::string get_help()
1087{
1088 std::ostringstream helpMsg;
1089
1090 helpMsg << "************************* HELP *************************"
1091 << endl << endl;
1092 helpMsg
1093 << "In this HELP file we assume that the solve service method is used and "
1094 << endl;
1095 helpMsg
1096 << "that we are solving problems locally, that is the solver is on the "
1097 << endl;
1098 helpMsg
1099 << "machine running this OSSolverService. See Section 10.3 of the User\'s "
1100 << endl;
1101 helpMsg
1102 << "Manual for other service methods or calling a server remotely. "
1103 << endl;
1104 helpMsg << "The OSSolverService takes the parameters listed below. "
1105 << endl;
1106 helpMsg
1107 << "The order of the parameters is irrelevant. Not all the parameters "
1108 << endl;
1109 helpMsg << "are required. However, the location of an instance file is "
1110 << endl;
1111 helpMsg
1112 << "required when using the solve service method. The location of the "
1113 << endl;
1114 helpMsg << "instance file is specified using the osil option. " << endl;
1115
1116 helpMsg << endl;
1117
1118 helpMsg
1119 << "-osil xxx.osil this is the name of the file that contains the "
1120 << endl;
1121 helpMsg << "optimization instance in OSiL format. This option may be "
1122 << endl;
1123 helpMsg << "specified in the OSoL solver options file. " << endl;
1124
1125 helpMsg << endl;
1126
1127 helpMsg
1128 << "-osol xxx.osol this is the name of the file that contains the solver options. "
1129 << endl;
1130 helpMsg << "It is not necessary to specify this option. " << endl;
1131
1132 helpMsg << endl;
1133
1134 helpMsg
1135 << "-osrl xxx.osrl this is the name of the file to which the solver solution is written. "
1136 << endl;
1137 helpMsg
1138 << "It is not necessary to specify this option. If this option is not specified, "
1139 << endl;
1140 helpMsg << "the result will be printed to standard out. " << endl;
1141
1142 helpMsg << endl;
1143
1144 helpMsg
1145 << "-osplInput xxx.ospl this is the name of an input file in the OS Process"
1146 << endl;
1147 helpMsg << " Language (OSpL), this is used as input to the knock method."
1148 << endl;
1149
1150 helpMsg << endl;
1151
1152 helpMsg
1153 << "-osplOutput xxx.ospl this is the name of an output file in the OS Process"
1154 << endl;
1155 helpMsg
1156 << "Language (OSpL), this the output string from the knock and kill methods."
1157 << endl;
1158
1159 helpMsg << endl;
1160
1161 helpMsg << "-serviceLocation url is the URL of the solver service. "
1162 << endl;
1163 helpMsg
1164 << "This is not required, and if not specified it is assumed that "
1165 << endl;
1166 helpMsg << "the problem is solved locally. " << endl;
1167
1168 helpMsg << endl;
1169
1170 helpMsg
1171 << "-serviceMethod methodName this is the method on the solver service to be invoked. "
1172 << endl;
1173 helpMsg
1174 << "The options are solve, send, kill, knock, getJobID, and retrieve. "
1175 << endl;
1176 helpMsg
1177 << "This option is not required, and the default value is solve. "
1178 << endl;
1179
1180 helpMsg << endl;
1181
1182 helpMsg
1183 << "-mps xxx.mps this is the name of the mps file if the problem instance "
1184 << endl;
1185 helpMsg
1186 << "is in mps format. The default file format is OSiL so this option is not required. "
1187 << endl;
1188
1189 helpMsg << endl;
1190
1191 helpMsg
1192 << "-nl xxx.nl this is the name of the AMPL nl file if the problem "
1193 << endl;
1194 helpMsg
1195 << "instance is in AMPL nl format. The default file format is OSiL "
1196 << endl;
1197 helpMsg << "so this option is not required. " << endl;
1198
1199 helpMsg << endl;
1200
1201 helpMsg
1202 << "-solver solverName Possible values for default OS installation "
1203 << endl;
1204 helpMsg
1205 << "are bonmin(COIN-OR Bonmin), couenne (COIN-OR Couenne), clp (COIN-OR Clp),"
1206 << endl;
1207 helpMsg << "cbc (COIN-OR Cbc), dylp (COIN-OR DyLP), ipopt (COIN-OR Ipopt),"
1208 << endl;
1209 helpMsg << "and symphony (COIN-OR SYMPHONY). Other solvers supported"
1210 << endl;
1211 helpMsg
1212 << "(if the necessary libraries are present) are cplex (Cplex through COIN-OR Osi),"
1213 << endl;
1214 helpMsg
1215 << "glpk (glpk through COIN-OR Osi), knitro (Knitro), and lindo (LINDO)."
1216 << endl;
1217 helpMsg << "If no value is specified for this parameter," << endl;
1218 helpMsg << "then cbc is the default value of this parameter." << endl;
1219
1220 helpMsg << endl;
1221
1222 helpMsg
1223 << "-browser browserName this paramater is a path to the browser on the "
1224 << endl;
1225 helpMsg
1226 << "local machine. If this optional parameter is specified then the "
1227 << endl;
1228 helpMsg << "solver result in OSrL format is transformed using XSLT into "
1229 << endl;
1230 helpMsg << "HTML and displayed in the browser. " << endl;
1231
1232 helpMsg << endl;
1233
1234 helpMsg
1235 << "-config pathToConfigureFile this parameter specifies a path on "
1236
1237 << endl;
1238 helpMsg
1239 << "the local machine to a text file containing values for the input parameters. "
1240 << endl;
1241 helpMsg
1242 << "This is convenient for the user not wishing to constantly retype parameter values. "
1243 << endl;
1244 helpMsg
1245 << "This configure file can contain values for all of the other parameters. "
1246 << endl;
1247
1248 helpMsg << endl;
1249
1250 helpMsg << "--version or -v get the current version of this executable "
1251 << endl;
1252
1253 helpMsg << endl;
1254
1255 helpMsg << "--help or -h to get this help file " << endl;
1256
1257 helpMsg << endl;
1258
1259 helpMsg
1260 << "Note: If you specify a configure file by using the -config option, you can "
1261 << endl;
1262 helpMsg
1263 << "override the values of the options in the configure file by putting them in "
1264 << endl;
1265 helpMsg << "at the command line. " << endl << endl;
1266
1267 helpMsg
1268 << "See the OS User\' Manual: http://www.coin-or.org/OS/doc/osUsersManual.pdf"
1269 << endl;
1270 helpMsg << "for more detail on how to use the OS project. " << endl;
1271
1272 helpMsg << endl;
1273 helpMsg << "********************************************************"
1274 << endl << endl;
1275
1276 return helpMsg.str();
1277}// get help
1278
1279
void solve(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
Next we have implementations of the six remote service methods solve, send, retrieve,...
void retrieve(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
void send(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
int main(int argc, char **argv)
void kill(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
struct yy_buffer_state * YY_BUFFER_STATE
int ossslex_destroy(void *scanner)
int ossslex_init(void **ptr)
void reportErrors(OSCommandLine *oscommandline, std::string errMsg, OSnl2OS *osnl2OS)
Deal with any error messages generated by the call to one of the service methods.
void doPrintRow(OSInstance *osinstance, std::string rownumberstring)
YY_BUFFER_STATE osss_scan_string(const char *osss, void *scanner)
void getJobID(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
std::string get_help()
const OSSmartPtr< OSOutput > osoutput
Definition OSOutput.cpp:39
void list_options(OSCommandLine *oscommandline)
void setyyextra(OSCommandLine *oscommandline, void *scanner)
bool findInstance(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
This method tries to find the instance by reading the .nl file.
int ossslex(void *scanner)
void knock(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
void reportResults(OSCommandLine *oscommandline, std::string osrl, OSnl2OS *osnl2OS)
Deal with the OSrL output generated by the call to one of the service methods.
void makeStrings(OSCommandLine *oscommandline)
std::string OSgetVersionInfo()
#define scanner
std::string runSolver(std::string solverName, std::string osol, OSInstance *osinstance)
This class is used to invoke a solver locally.
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.
This class is used to store command line options for the OSSolverService executable and to provide me...
std::string jobID
the JobID
OSOption * osoption
osoption is a representation of the solver options in OSOption format
std::string serviceLocation
serviceLocation is the URL of the remote solver when a local solver is not used
std::string osplInputFile
name of an input file with xml in OS process language format, used for example to knock on a server,...
OSInstance * osinstance
osinstance is a representation of the instance in OSInstance format
bool writeVersion
if this parameter is true we print the current version of the OS project
int printLevel
this parameter controls the amount of output to print the higher the number, the more output is gener...
std::string printRowNumberAsString
this parameter contains a string representation (!) of the row number if only a single row (constrain...
std::string nlFile
the name of the file that holds an instance in AMPL nl format
std::string osilOutputFile
osilOutputFile is the name of the file to which the instance can be written in OSiL format.
std::string logFile
this optional parameter contains the path to a logfile that can be used as an alternate output stream...
std::string osplOutputFile
name of an output file where the solver should write the result of a knock or kill service request
std::string solverName
the name of the solver to be invoked locally, e.g -solver Ipopt
std::string mpsFile
the name of the file that holds an instance in MPS format
bool invokeHelp
if this parameter is true we print the contents of the file help.txt and return
std::string osolFile
osolFile is the name of the file that holds the solver options in OSoL format
std::string osplInput
osplInput is the content of the osplInputFile
std::string browser
this parameter is a path to the browser on the local machine.
int filePrintLevel
this parameter controls the amount of output to send to the log file (if used) the higher the number,...
std::string configFile
configFile is the name of the file that holds the configuration options if the OSSolverService reads ...
std::string gamsControlFile
the name of the file that holds the GAMS control parameters
std::string osil
osil is the content of the osilFile
bool printModel
if this parameter is true we print the current instance as read from an osil, nl or mps file
std::string osrlFile
osrlFile is the name of the file where the solver should write the result (in OSrL format)
std::string osolOutputFile
osolOutputFile is the name of the file to which the solver options can be written in OSoL format.
std::string serviceMethod
the service method the OSSolverService should execute, i.e.
std::string osilFile
osilFile is the name of the file that holds the model instance in OSiL format
std::string osol
osol is the content of the osolFile
The OSCommandLineReader Class.
OSCommandLine * readCommandLine(const std::string &osss)
Get an OSCommandLine object from a command line string.
The in-memory representation of an OSiL instance..
std::string printModel()
Print the infix representation of the problem.
The Option Class.
Definition OSOption.h:3565
bool setJobID(std::string jobID)
Set the job ID.
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.
Template class for Smart Pointers.
Used by a client to invoke a remote solver.
std::string retrieve(std::string osol)
implement the retrieve() method which is a virtual function in OShL
bool send(std::string osil, std::string osol)
implement the send() 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
std::string solve(std::string osil, std::string osol)
implement the solve() method which is a virtual function in OShL, this is synchronous
std::string knock(std::string ospl, std::string osol)
implement the knock() method which is a virtual function in OShL
std::string kill(std::string osol)
implement the kill() method which is a virtual function in OShL
Used to read an OSiL string.
Definition OSiLReader.h:38
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
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
The OSnl2OS Class.
Definition OSnl2OS.h:79
void setJobID(std::string jobID)
set the job ID
Definition OSnl2OS.cpp:104
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
void setOsol(std::string osol)
set the osol string
Definition OSnl2OS.cpp:99
Used to read an OSoL string.
Definition OSoLReader.h:38
OSOption * readOSoL(const std::string &osol)
parse the OSoL solver options.
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.
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
#define DEFAULT_OUTPUT_LEVEL
ENUM_OUTPUT_LEVEL
Enumeration for the different verbosity levels that can be used in producing output.
@ ENUM_OUTPUT_LEVEL_debug
@ ENUM_OUTPUT_LEVEL_trace
@ ENUM_OUTPUT_LEVEL_error
@ ENUM_OUTPUT_LEVEL_info
@ ENUM_OUTPUT_LEVEL_summary
@ ENUM_OUTPUT_LEVEL_always
@ ENUM_OUTPUT_AREA_main
OSResult * osresult