00001
00060 #include "OSConfig.h"
00061 #include "OSnl2OS.h"
00062 #include "OSosrl2ampl.h"
00063 #include "OSiLReader.h"
00064 #include "OSiLWriter.h"
00065 #include "OSoLReader.h"
00066 #include "OSoLWriter.h"
00067 #include "OSrLReader.h"
00068 #include "OSrLWriter.h"
00069 #include "OSInstance.h"
00070 #include "OSOption.h"
00071 #include "OSResult.h"
00072 #include "OSOutput.h"
00073
00074 #include "OSDefaultSolver.h"
00075 #include "OSCoinSolver.h"
00076
00077 #ifndef COIN_HAS_ASL
00078 # error do not have ASL
00079 #endif
00080
00081 #ifdef COIN_HAS_LINDO
00082 # include "OSLindoSolver.h"
00083 #endif
00084
00085 #ifdef COIN_HAS_IPOPT
00086 # include "OSIpoptSolver.h"
00087 #endif
00088
00089 #ifdef COIN_HAS_BONMIN
00090 # include "OSBonminSolver.h"
00091 #endif
00092
00093 #ifdef COIN_HAS_COUENNE
00094 # include "OSCouenneSolver.h"
00095 #endif
00096
00097 #include "OSFileUtil.h"
00098 #include "OSSolverAgent.h"
00099 #include "OShL.h"
00100 #include "OSErrorClass.h"
00101 #include "CoinError.hpp"
00102 #include "OSCommandLine.h"
00103 #include "OSCommandLineReader.h"
00104 #include "OSRunSolver.h"
00105 #include <sstream>
00106
00107 #ifdef HAVE_CSTRING
00108 # include <cstring>
00109 #else
00110 # ifdef HAVE_STRING_H
00111 # include <string.h>
00112 # else
00113 # error "don't have header file for string"
00114 # endif
00115 #endif
00116
00117 #include "CoinError.hpp"
00118 #include "CoinHelperFunctions.hpp"
00119
00120
00121 #include <iostream>
00122
00123 #include <asl.h>
00124
00125
00126
00127 typedef struct yy_buffer_state *YY_BUFFER_STATE;
00128 YY_BUFFER_STATE osss_scan_string(const char* osss, void* scanner );
00129
00130 void setyyextra(OSCommandLine *oscommandline, void* scanner);
00131 int ossslex(void* scanner );
00132 int ossslex_init(void** ptr);
00133 int ossslex_destroy (void* scanner );
00134
00135
00136 using std::cerr;
00137 using std::cout;
00138 using std::endl;
00139 using std::ostringstream;
00140
00141 std::string get_help();
00142 void list_options(OSCommandLine *oscommandline);
00143
00144
00145 void solve(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00146 void getJobID(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00147 void send(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00148 void kill(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00149 void retrieve(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00150 void knock(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00151
00152
00153 bool findInstance( OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00154 void makeStrings( OSCommandLine *oscommandline);
00155 void doPrintRow(OSInstance *osinstance, std::string rownumberstring);
00156 void reportResults(OSCommandLine *oscommandline, std::string osrl, OSnl2OS *osnl2OS);
00157 void reportErrors(OSCommandLine *oscommandline, std::string errMsg, OSnl2OS *osnl2OS);
00158
00159 extern const SmartPtr< OSOutput> osoutput;
00160
00161
00162 int main(int argc, char **argv)
00163 {
00164 WindowsErrorPopupBlocker();
00165 std::ostringstream outStr;
00166 OSnl2OS *nl2OS = new OSnl2OS();
00167 OSiLReader* osilreader = new OSiLReader();
00168 FileUtil *fileUtil = NULL;
00169 ostringstream echo_cl;
00170
00171
00172
00173 OSCommandLine *oscommandline;
00174 OSCommandLineReader *oscommandlinereader = new OSCommandLineReader();
00175
00176 DefaultSolver *solverType = NULL;
00177 char* stub = NULL;
00178 if (argc > 0) stub = argv[1];
00179
00180
00181 char* temp = getenv("OSAmplClient_options");
00182 ostringstream temp2;
00183 std::string amplclient_options;
00184 if (temp != NULL)
00185 {
00186 temp2 << temp;
00187 amplclient_options = temp2.str();
00188 std::cout << amplclient_options << std::endl;
00189 }
00190 else
00191
00192 amplclient_options = "serviceMethod retrieve serviceLocation http://74.94.100.129:8080/OSServer/services/OSSolverService printLevel 4 jobID gus-10-apr-2013-0001";
00193
00194
00195
00196 #ifndef NDEBUG
00197 echo_cl << "HERE ARE THE AMPLCLIENT OPTIONS ";
00198 echo_cl << amplclient_options;
00199 echo_cl << endl << endl;
00200
00201 echo_cl << "Try to open file ";
00202 echo_cl << stub << ".nl";
00203 echo_cl << endl;
00204 #endif
00205
00206
00207 try
00208 {
00209 oscommandline = oscommandlinereader->readCommandLine(amplclient_options);
00210 }
00211
00212 catch (const ErrorClass& eclass)
00213 {
00214 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00215 delete oscommandlinereader;
00216 oscommandlinereader = NULL;
00217 return 1;
00218 }
00219
00220 if (stub) oscommandline->nlFile = stub;
00221
00222
00225 try
00226 {
00227 outStr.str("");
00228 outStr.clear();
00229 outStr << std::endl << "using print level " << oscommandline->printLevel << " for stdout" << std::endl;
00230
00231 if (oscommandline->printLevel != DEFAULT_OUTPUT_LEVEL)
00232 {
00233 osoutput->SetPrintLevel("stdout", (ENUM_OUTPUT_LEVEL)oscommandline->printLevel);
00234
00235 #ifndef NDEBUG
00236 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, echo_cl.str());
00237 #endif
00238 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info, outStr.str());
00239 }
00240 #ifndef NDEBUG
00241 else
00242 {
00243 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, echo_cl.str());
00244 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00245 }
00246 #endif
00247
00248 if (oscommandline->logFile != "")
00249 {
00250 int status = osoutput->AddChannel(oscommandline->logFile);
00251
00252 switch(status)
00253 {
00254 case 0:
00255 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info,
00256 "Added channel " + oscommandline->logFile);
00257 break;
00258 case 1:
00259 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info,
00260 "Output channel " + oscommandline->logFile + " previously defined");
00261 break;
00262 default:
00263 throw ErrorClass("Could not add output channel " + oscommandline->logFile);
00264 }
00265
00266
00267 outStr.str("");
00268 outStr.clear();
00269 outStr << std::endl << "using print level " << oscommandline->filePrintLevel;
00270 outStr << " for " << oscommandline->logFile << std::endl;
00271
00272 if (oscommandline->filePrintLevel != DEFAULT_OUTPUT_LEVEL)
00273 {
00274 osoutput->SetPrintLevel(oscommandline->logFile, (ENUM_OUTPUT_LEVEL)oscommandline->filePrintLevel);
00275 }
00276 else
00277 {
00278 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00279 }
00280 }
00281
00282 if (oscommandline->invokeHelp == true)
00283 {
00284 outStr.str("");
00285 outStr.clear();
00286 outStr << std::endl << std::endl << get_help() << std::endl;
00287 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());
00288
00289 delete oscommandlinereader;
00290 oscommandlinereader = NULL;
00291 return 0;
00292 }
00293
00294 if (oscommandline->writeVersion == true)
00295 {
00296 outStr.str("");
00297 outStr.clear();
00298 outStr << std::endl << std::endl << OSgetVersionInfo() << std::endl;
00299 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());
00300
00301 delete oscommandlinereader;
00302 oscommandlinereader = NULL;
00303 return 0;
00304 }
00305 }
00306 catch (const ErrorClass& eclass)
00307 {
00308 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00309 delete oscommandlinereader;
00310 oscommandlinereader = NULL;
00311 return 1;
00312 }
00313
00314 #ifndef NDEBUG
00315 outStr.str("");
00316 outStr.clear();
00317
00318 outStr << "HERE ARE THE OPTION VALUES:" << endl;
00319 if(oscommandline->configFile != "") outStr << "Config file = " << oscommandline->configFile << endl;
00320 if(oscommandline->osilFile != "") outStr << "OSiL file = " << oscommandline->osilFile << endl;
00321 if(oscommandline->osolFile != "") outStr << "OSoL file = " << oscommandline->osolFile << endl;
00322 if(oscommandline->osrlFile != "") outStr << "OSrL file = " << oscommandline->osrlFile << endl;
00323
00324 if(oscommandline->osplInputFile != "") outStr << "OSpL Input file = " << oscommandline->osplInputFile << endl;
00325 if(oscommandline->serviceMethod != "") outStr << "Service Method = " << oscommandline->serviceMethod << endl;
00326 if(oscommandline->mpsFile != "") outStr << "MPS File Name = " << oscommandline->mpsFile << endl;
00327 if(oscommandline->nlFile != "") outStr << "NL File Name = " << oscommandline->nlFile << endl;
00328 if(oscommandline->gamsControlFile != "") outStr << "gams Control File Name = " << oscommandline->gamsControlFile << endl;
00329 if(oscommandline->browser != "") outStr << "Browser Value = " << oscommandline->browser << endl;
00330 if(oscommandline->solverName != "") outStr << "Selected Solver = " << oscommandline->solverName << endl;
00331 if(oscommandline->serviceLocation != "") outStr << "Service Location = " << oscommandline->serviceLocation << endl;
00332 if(oscommandline->jobID != "") outStr << "Job ID = " << oscommandline->jobID << endl;
00333 if(oscommandline->printModel) outStr << "print model prior to solve/send" << endl;
00334 if(oscommandline->printRowNumberAsString != "") outStr << "print model row " << oscommandline->printRowNumberAsString << " prior to solve/send" << endl;
00335 outStr << "print level for stdout: " << oscommandline->printLevel << endl;
00336 if(oscommandline->logFile != "")
00337 {
00338 outStr << "also send output to " << oscommandline->logFile << endl;
00339 outStr << "print level for file output: " << oscommandline->filePrintLevel << endl;
00340 }
00341
00342 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00343 #endif
00344
00345
00346
00347 unsigned int k;
00348 for (k = 0; k < oscommandline->solverName.length(); k++)
00349 {
00350 oscommandline->solverName[k] = (char)tolower(oscommandline->solverName[k]);
00351 }
00352
00353
00354
00355 if (oscommandline->serviceMethod == "") oscommandline->serviceMethod = "solve";
00356
00357 try
00358 {
00359
00360 if (oscommandline->serviceLocation == "")
00361 if (oscommandline->serviceMethod != "solve")
00362 throw ErrorClass("No serviceLocation given. Only \'solve\' is available locally.");
00363
00364 if (oscommandline->serviceMethod[0] == 's')
00365 {
00366
00367 if (oscommandline->osil == "")
00368 {
00369 if (!findInstance(oscommandline, nl2OS))
00370 throw ErrorClass("No instance could be found");
00371 }
00372
00373
00374 if (oscommandline->printModel == true)
00375 {
00376 if (oscommandline->osinstance == NULL)
00377 {
00378 oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
00379 }
00380 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always,
00381 oscommandline->osinstance->printModel());
00382 }
00383 else if (oscommandline->printRowNumberAsString != "")
00384 {
00385 if (oscommandline->osinstance == NULL)
00386 {
00387 oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
00388 }
00389 doPrintRow(oscommandline->osinstance, oscommandline->printRowNumberAsString);
00390 }
00391
00392
00393 if (oscommandline->osilOutputFile != "")
00394 {
00395 fileUtil = new FileUtil();
00396 if (oscommandline->osil == "")
00397 {
00398 OSiLWriter* osilwriter = new OSiLWriter();
00399 oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
00400 delete osilwriter;
00401 osilwriter = NULL;
00402 }
00403 fileUtil->writeFileFromString(oscommandline->osilOutputFile, oscommandline->osil);
00404 delete fileUtil;
00405 fileUtil = NULL;
00406 }
00407
00408
00409 if (oscommandline->osolOutputFile != "")
00410 {
00411 fileUtil = new FileUtil();
00412 if (oscommandline->osol != "" && oscommandline->osoption == NULL)
00413 fileUtil->writeFileFromString(oscommandline->osolOutputFile, oscommandline->osol);
00414 else
00415 {
00416 if (oscommandline->osoption == NULL)
00417 oscommandline->osoption = new OSOption();
00418 OSoLWriter* osolwriter = new OSoLWriter();
00419 fileUtil->writeFileFromString(oscommandline->osolOutputFile,
00420 osolwriter->writeOSoL(oscommandline->osoption));
00421 delete osolwriter;
00422 osolwriter = NULL;
00423 }
00424 delete fileUtil;
00425 fileUtil = NULL;
00426 }
00427
00428
00429 if (oscommandline->serviceLocation == "")
00430 {
00431 std::string osrl;
00432 if (oscommandline->osinstance != NULL)
00433 if (oscommandline->osoption != NULL)
00434 osrl = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osinstance);
00435 else
00436 osrl = runSolver(oscommandline->solverName, oscommandline->osol, oscommandline->osinstance);
00437 else
00438 if (oscommandline->osoption != NULL)
00439 osrl = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osil);
00440 else
00441 osrl = runSolver(oscommandline->solverName, oscommandline->osol, oscommandline->osil);
00442 reportResults(oscommandline, osrl, nl2OS);
00443 }
00444
00445
00446 else
00447 {
00448 if (oscommandline->serviceMethod[1] == 'e')
00449 send(oscommandline, nl2OS);
00450 else
00451 solve(oscommandline, nl2OS);
00452 }
00453 }
00454
00455 else
00456 {
00457 switch (oscommandline->serviceMethod[0])
00458 {
00459 case 'g':
00460 getJobID(oscommandline, nl2OS);
00461 break;
00462 case 'r':
00463 retrieve(oscommandline, nl2OS);
00464 break;
00465 case 'k':
00466 if (oscommandline->serviceMethod[1] == 'i')
00467 kill(oscommandline, nl2OS);
00468 else
00469 knock(oscommandline, nl2OS);
00470 break;
00471 default:
00472 break;
00473 }
00474 }
00475
00476
00477 if (osilreader != NULL)
00478 delete osilreader;
00479 oscommandline->osinstance = NULL;
00480 osilreader = NULL;
00481 delete oscommandlinereader;
00482 oscommandlinereader = NULL;
00483 delete fileUtil;
00484 fileUtil = NULL;
00485
00486 if (nl2OS != NULL)
00487 delete nl2OS;
00488 nl2OS = NULL;
00489
00490 return 0;
00491 }
00492
00493 catch (const ErrorClass& eclass)
00494 {
00495 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00496 if (osilreader != NULL)
00497 delete osilreader;
00498 oscommandline->osinstance = NULL;
00499 osilreader = NULL;
00500 delete oscommandlinereader;
00501 oscommandlinereader = NULL;
00502 delete fileUtil;
00503 fileUtil = NULL;
00504
00505 if (nl2OS != NULL)
00506 delete nl2OS;
00507 nl2OS = NULL;
00508 return 1;
00509 }
00510 }
00511
00512
00519 bool findInstance(OSCommandLine *oscommandline, OSnl2OS *nl2os)
00520 {
00521
00522
00523
00524 try
00525 {
00526
00527 if (!nl2os->readNl(oscommandline->nlFile))
00528 throw ErrorClass("Error reading .nl file.");
00529 nl2os->setOsol(oscommandline->osol);
00530 nl2os->setJobID(oscommandline->jobID);
00531 nl2os->createOSObjects() ;
00532 oscommandline->osinstance = nl2os->osinstance;
00533 return true;
00534 }
00535 catch (const ErrorClass& eclass)
00536 {
00537 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, eclass.errormsg);
00538
00539 return false;
00540 }
00541 }
00542
00543 void makeStrings(OSCommandLine *oscommandline)
00544 {
00545
00546 if (oscommandline->osil == "")
00547 {
00548 OSiLWriter *osilwriter = new OSiLWriter();
00549 oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
00550 #ifndef NDEBUG
00551 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, oscommandline->osil);
00552 #endif
00553 delete osilwriter;
00554 osilwriter = NULL;
00555 }
00556
00562 OSoLReader *osolreader = NULL;
00563 if (oscommandline->osoption != NULL || oscommandline->jobID != "" || oscommandline->osol == "")
00564 {
00565 if (oscommandline->osoption == NULL)
00566 {
00567 oscommandline->osoption = new OSOption();
00568 if (oscommandline->osol != "")
00569 {
00570 osolreader = new OSoLReader();
00571 oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
00572 }
00573 if (oscommandline->jobID != "")
00574 oscommandline->osoption->setJobID(oscommandline->jobID);
00575 }
00576
00577 OSoLWriter *osolwriter = new OSoLWriter();
00578 oscommandline->osol = osolwriter->writeOSoL(oscommandline->osoption);
00579 #ifndef NDEBUG
00580 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, oscommandline->osol);
00581 #endif
00582 delete osolwriter;
00583 osolwriter = NULL;
00584 }
00585 if (osolreader != NULL)
00586 {
00587 delete osolreader;
00588 oscommandline->osoption = NULL;
00589 }
00590 osolreader = NULL;
00591 }
00592
00598 void solve(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
00599 {
00600 std::string osrl = "";
00601 OSSolverAgent* osagent = NULL;
00602 FileUtil *fileUtil = NULL;
00603 fileUtil = new FileUtil();
00604
00605 try
00606 {
00607
00608 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00609
00610
00611 oscommandline->jobID = "";
00612 makeStrings(oscommandline);
00613
00614 osrl = osagent->solve(oscommandline->osil, oscommandline->osol);
00615 delete osagent;
00616 osagent = NULL;
00617
00618 reportResults(oscommandline, osrl, nl2OS);
00619
00620
00621 delete fileUtil;
00622 fileUtil = NULL;
00623
00624 }
00625 catch (const ErrorClass& eclass)
00626 {
00627 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00628
00629
00630 delete fileUtil;
00631 fileUtil = NULL;
00632 }
00633
00634 }
00635
00636
00637 void send(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
00638 {
00639 bool bSend = false;
00640 bool always_print = false;
00641 OSSolverAgent* osagent = NULL;
00642 ostringstream outStr;
00643
00644 try
00645 {
00646 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00647
00648
00649
00650 if (oscommandline->jobID == "NEW")
00651 {
00652 always_print = true;
00653 oscommandline->jobID = osagent->getJobID("");
00654 if (oscommandline->osoption != NULL)
00655 oscommandline->osoption->setJobID(oscommandline->jobID);
00656 }
00657
00658 makeStrings(oscommandline);
00659
00660 outStr.str("");
00661 outStr.clear();
00662 outStr << "Submitting Job " << oscommandline->jobID << std::endl;
00663 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info, outStr.str());
00664
00665 bSend = osagent->send(oscommandline->osil, oscommandline->osol);
00666
00667 outStr.str("");
00668 outStr.clear();
00669 outStr << "Job " << oscommandline->jobID;
00670
00671 if (bSend == true)
00672 {
00673 outStr << " successfully submitted." << std::endl;
00674 if (always_print)
00675 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());
00676 else
00677 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info, outStr.str());
00678 }
00679 else
00680 {
00681 outStr << ": send failed." << std::endl;
00682 outStr << "Check to make sure you sent a jobID not on the system." << std::endl;
00683 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, outStr.str());
00684 }
00685
00686 delete osagent;
00687 osagent = NULL;
00688 }
00689
00690 catch (const ErrorClass& eclass)
00691 {
00692 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00693 if (osagent != NULL)
00694 delete osagent;
00695 osagent = NULL;
00696 }
00697 }
00698
00699 void getJobID(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
00700 {
00701 OSSolverAgent* osagent = NULL;
00702 try
00703 {
00704 if (oscommandline->serviceLocation != "")
00705 {
00706 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00707 oscommandline->jobID = osagent->getJobID(oscommandline->osol);
00708 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, oscommandline->jobID);
00709 delete osagent;
00710 osagent = NULL;
00711 }
00712 else
00713 {
00714 delete osagent;
00715 osagent = NULL;
00716 throw ErrorClass("please specify service location (url)");
00717 }
00718 }
00719
00720 catch (const ErrorClass& eclass)
00721 {
00722 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00723 if (osagent != NULL)
00724 delete osagent;
00725 osagent = NULL;
00726 }
00727 }
00728
00729
00730 void knock(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
00731 {
00732 std::string osplOutput = "";
00733 OSSolverAgent* osagent = NULL;
00734 FileUtil *fileUtil = NULL;
00735 fileUtil = new FileUtil();
00736 try
00737 {
00738 if (oscommandline->serviceLocation != "")
00739 {
00740 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00741
00742 if (oscommandline->osol == "")
00743 {
00744
00745 OSOption *osOption = NULL;
00746 osOption = new OSOption();
00747
00748 if(oscommandline->jobID == "") osOption->setJobID( oscommandline->jobID);
00749
00750 OSoLWriter *osolWriter = NULL;
00751 osolWriter = new OSoLWriter();
00752 oscommandline->osol = osolWriter->writeOSoL( osOption);
00753 delete osOption;
00754 osOption = NULL;
00755 delete osolWriter;
00756 osolWriter = NULL;
00757 }
00758
00759 osplOutput = osagent->knock(oscommandline->osplInput, oscommandline->osol);
00760 if (oscommandline->osplOutputFile != "")
00761 fileUtil->writeFileFromString(oscommandline->osplOutputFile,
00762 osplOutput);
00763 else
00764 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, osplOutput);
00765 delete osagent;
00766 }
00767 else
00768 {
00769 delete osagent;
00770 throw ErrorClass("please specify service location (url)");
00771 }
00772 delete fileUtil;
00773 fileUtil = NULL;
00774 }
00775 catch (const ErrorClass& eclass)
00776 {
00777 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00778 if (osagent != NULL)
00779 delete osagent;
00780 osagent = NULL;
00781
00782 delete fileUtil;
00783 fileUtil = NULL;
00784 }
00785 }
00786
00787 void retrieve(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
00788 {
00789 FileUtil *fileUtil = NULL;
00790 fileUtil = new FileUtil();
00791 std::string osrl = "";
00792 OSSolverAgent* osagent = NULL;
00793
00794 try
00795 {
00796 if (oscommandline->serviceLocation != "")
00797 {
00798 if (!nl2OS->readNl(oscommandline->nlFile))
00799 throw ErrorClass("Error reading .nl file.");
00800
00801 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00802
00803 if (oscommandline->osol == "")
00804 {
00805
00806 OSOption *osOption = NULL;
00807 osOption = new OSOption();
00808
00809 if (oscommandline->jobID == "") throw ErrorClass("there is no JobID");
00810
00811 osOption->setJobID( oscommandline->jobID);
00812
00813 OSoLWriter *osolWriter = NULL;
00814 osolWriter = new OSoLWriter();
00815 oscommandline->osol = osolWriter->writeOSoL( osOption);
00816 delete osOption;
00817 osOption = NULL;
00818 delete osolWriter;
00819 osolWriter = NULL;
00820 }
00821
00822 osrl = osagent->retrieve(oscommandline->osol);
00823 reportResults(oscommandline, osrl, nl2OS);
00824 delete osagent;
00825 osagent = NULL;
00826 }
00827 else
00828 {
00829 delete osagent;
00830 osagent = NULL;
00831 throw ErrorClass("please specify service location (url)");
00832 }
00833 delete fileUtil;
00834 fileUtil = NULL;
00835 }
00836 catch (const ErrorClass& eclass)
00837 {
00838 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00839 if (osagent != NULL)
00840 delete osagent;
00841 osagent = NULL;
00842
00843 delete fileUtil;
00844 fileUtil = NULL;
00845 }
00846 }
00847
00848 void kill(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
00849 {
00850 FileUtil *fileUtil = NULL;
00851 fileUtil = new FileUtil();
00852 std::string osplOutput = "";
00853 OSSolverAgent* osagent = NULL;
00854 try
00855 {
00856 if (oscommandline->serviceLocation != "")
00857 {
00858 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00859
00860 if (oscommandline->osol == "")
00861 {
00862
00863 OSOption *osOption = NULL;
00864 osOption = new OSOption();
00865
00866 if (oscommandline->jobID == "") throw ErrorClass("there is no JobID");
00867
00868 osOption->setJobID( oscommandline->jobID);
00869
00870 OSoLWriter *osolWriter = NULL;
00871 osolWriter = new OSoLWriter();
00872 oscommandline->osol = osolWriter->writeOSoL( osOption);
00873 delete osOption;
00874 osOption = NULL;
00875 delete osolWriter;
00876 osolWriter = NULL;
00877 }
00878
00879 osplOutput = osagent->kill(oscommandline->osol);
00880
00881 if (oscommandline->osplOutputFile != "")
00882 fileUtil->writeFileFromString(oscommandline->osplOutputFile, osplOutput);
00883 else
00884 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_summary, osplOutput);
00885 delete osagent;
00886 osagent = NULL;
00887 }
00888 else
00889 {
00890 delete osagent;
00891 osagent = NULL;
00892 throw ErrorClass("please specify service location (url)");
00893 }
00894 delete fileUtil;
00895 fileUtil = NULL;
00896 }
00897 catch (const ErrorClass& eclass)
00898 {
00899 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00900 if (osagent != NULL)
00901 delete osagent;
00902 osagent = NULL;
00903
00904 delete fileUtil;
00905 fileUtil = NULL;
00906 }
00907 }
00908
00909
00910
00913 void reportResults(OSCommandLine *oscommandline, std::string osrl, OSnl2OS* nl2OS)
00914 {
00915 ostringstream outStr;
00916
00917 if (oscommandline->osrlFile == "")
00918 oscommandline->osrlFile = oscommandline->nlFile + ".osrl";
00919
00920 FileUtil* fileUtil = new FileUtil();
00921 fileUtil->writeFileFromString(oscommandline->osrlFile, osrl);
00922
00923 if (oscommandline->browser != "")
00924 {
00925 std::string str = oscommandline->browser + " "
00926 + oscommandline->osrlFile;
00927 const char *ch = &str[0];
00928 std::system(ch);
00929 }
00930
00931
00932
00933 OSosrl2ampl *solWriter = new OSosrl2ampl();
00934
00935 try
00936 {
00937 std::cout << osrl << std::endl << std::endl;
00938
00939 std::string::size_type pos1 = osrl.find( "error");
00940 if(pos1 == std::string::npos)
00941 {
00942 OSrLReader *osrlreader = new OSrLReader();
00943 OSResult *osresult = osrlreader->readOSrL( osrl);
00944 solWriter->writeSolFile(osrl, nl2OS->getASL("asl"), oscommandline->nlFile + ".sol");
00945
00946 delete osrlreader;
00947 osrlreader = NULL;
00948 }
00949 else
00950 {
00951 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, osrl);
00952 solWriter->writeSolFile(osrl, nl2OS->getASL("asl"), oscommandline->nlFile + ".sol");
00953 }
00954 if (fileUtil != NULL)
00955 delete fileUtil;
00956 fileUtil = NULL;
00957 }
00958 catch(const ErrorClass& eclass)
00959 {
00960 outStr.str("");
00961 outStr.clear();
00962 outStr << "There was an error parsing the OSrL string" << endl << eclass.errormsg << endl << endl;
00963 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00964 if (fileUtil != NULL)
00965 delete fileUtil;
00966 fileUtil = NULL;
00967 }
00968
00969 }
00970
00973 void reportErrors(OSCommandLine *oscommandline, std::string errormsg, OSnl2OS* nl2OS)
00974 {
00975 std::string osrl = "";
00976 OSResult *osresult = NULL;
00977 OSrLWriter *osrlwriter = NULL;
00978
00979
00980 std::string::size_type pos1 = errormsg.find( "<osrl");
00981 if(pos1 == std::string::npos)
00982 {
00983 osrlwriter = new OSrLWriter();
00984 osresult = new OSResult();
00985 osresult->setGeneralMessage(errormsg);
00986 osresult->setGeneralStatusType("error");
00987 osrl = osrlwriter->writeOSrL(osresult);
00988 }
00989 else
00990 {
00991 osrl = errormsg;
00992 }
00993 reportResults(oscommandline, osrl, nl2OS);
00994 }
00995
00996
00997 void doPrintRow(OSInstance *osinstance, std::string rownumberstring)
00998 {
00999 ostringstream outStr;
01000 int rownumber;
01001 if (rownumberstring == "")
01002 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always,
01003 "no row number given; print command ignored");
01004 else
01005 {
01006 try
01007 {
01008 rownumber = atoi((rownumberstring).c_str());
01009 }
01010 catch (const ErrorClass& eclass)
01011 {
01012 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always,
01013 "invalid row number; print command ignored");
01014 }
01015
01016 if (osinstance == NULL)
01017 {
01018 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always,
01019 "no instance defined; print command ignored");
01020 }
01021 else
01022 {
01023 outStr << std::endl << "Row " << rownumber << ":" << std::endl << std::endl;
01024 outStr << osinstance->printModel(rownumber) << std::endl;
01025 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());
01026 }
01027 }
01028 }
01029
01030
01031 std::string get_help()
01032 {
01033 std::ostringstream helpMsg;
01034
01035 helpMsg << "************************* HELP *************************"
01036 << endl << endl;
01037 helpMsg
01038 << "In this HELP file we assume that the solve service method is used and "
01039 << endl;
01040 helpMsg
01041 << "that we are solving problems locally, that is the solver is on the "
01042 << endl;
01043 helpMsg
01044 << "machine running this OSSolverService. See Section 10.3 of the User\'s "
01045 << endl;
01046 helpMsg
01047 << "Manual for other service methods or calling a server remotely. "
01048 << endl;
01049 helpMsg << "The OSSolverService takes the parameters listed below. "
01050 << endl;
01051 helpMsg
01052 << "The order of the parameters is irrelevant. Not all the parameters "
01053 << endl;
01054 helpMsg << "are required. However, the location of an instance file is "
01055 << endl;
01056 helpMsg
01057 << "required when using the solve service method. The location of the "
01058 << endl;
01059 helpMsg << "instance file is specified using the osil option. " << endl;
01060
01061 helpMsg << endl;
01062
01063 helpMsg
01064 << "-osil xxx.osil this is the name of the file that contains the "
01065 << endl;
01066 helpMsg << "optimization instance in OSiL format. This option may be "
01067 << endl;
01068 helpMsg << "specified in the OSoL solver options file. " << endl;
01069
01070 helpMsg << endl;
01071
01072 helpMsg
01073 << "-osol xxx.osol this is the name of the file that contains the solver options. "
01074 << endl;
01075 helpMsg << "It is not necessary to specify this option. " << endl;
01076
01077 helpMsg << endl;
01078
01079 helpMsg
01080 << "-osrl xxx.osrl this is the name of the file to which the solver solution is written. "
01081 << endl;
01082 helpMsg
01083 << "It is not necessary to specify this option. If this option is not specified, "
01084 << endl;
01085 helpMsg << "the result will be printed to standard out. " << endl;
01086
01087 helpMsg << endl;
01088
01089 helpMsg
01090 << "-osplInput xxx.ospl this is the name of an input file in the OS Process"
01091 << endl;
01092 helpMsg << " Language (OSpL), this is used as input to the knock method."
01093 << endl;
01094
01095 helpMsg << endl;
01096
01097 helpMsg
01098 << "-osplOutput xxx.ospl this is the name of an output file in the OS Process"
01099 << endl;
01100 helpMsg
01101 << "Language (OSpL), this the output string from the knock and kill methods."
01102 << endl;
01103
01104 helpMsg << endl;
01105
01106 helpMsg << "-serviceLocation url is the URL of the solver service. "
01107 << endl;
01108 helpMsg
01109 << "This is not required, and if not specified it is assumed that "
01110 << endl;
01111 helpMsg << "the problem is solved locally. " << endl;
01112
01113 helpMsg << endl;
01114
01115 helpMsg
01116 << "-serviceMethod methodName this is the method on the solver service to be invoked. "
01117 << endl;
01118 helpMsg
01119 << "The options are solve, send, kill, knock, getJobID, and retrieve. "
01120 << endl;
01121 helpMsg
01122 << "This option is not required, and the default value is solve. "
01123 << endl;
01124
01125 helpMsg << endl;
01126
01127 helpMsg
01128 << "-mps xxx.mps this is the name of the mps file if the problem instance "
01129 << endl;
01130 helpMsg
01131 << "is in mps format. The default file format is OSiL so this option is not required. "
01132 << endl;
01133
01134 helpMsg << endl;
01135
01136 helpMsg
01137 << "-nl xxx.nl this is the name of the AMPL nl file if the problem "
01138 << endl;
01139 helpMsg
01140 << "instance is in AMPL nl format. The default file format is OSiL "
01141 << endl;
01142 helpMsg << "so this option is not required. " << endl;
01143
01144 helpMsg << endl;
01145
01146 helpMsg
01147 << "-solver solverName Possible values for default OS installation "
01148 << endl;
01149 helpMsg
01150 << "are bonmin(COIN-OR Bonmin), couenne (COIN-OR Couenne), clp (COIN-OR Clp),"
01151 << endl;
01152 helpMsg << "cbc (COIN-OR Cbc), dylp (COIN-OR DyLP), ipopt (COIN-OR Ipopt),"
01153 << endl;
01154 helpMsg << "and symphony (COIN-OR SYMPHONY). Other solvers supported"
01155 << endl;
01156 helpMsg
01157 << "(if the necessary libraries are present) are cplex (Cplex through COIN-OR Osi),"
01158 << endl;
01159 helpMsg
01160 << "glpk (glpk through COIN-OR Osi), knitro (Knitro), and lindo (LINDO)."
01161 << endl;
01162 helpMsg << "If no value is specified for this parameter," << endl;
01163 helpMsg << "then cbc is the default value of this parameter." << endl;
01164
01165 helpMsg << endl;
01166
01167 helpMsg
01168 << "-browser browserName this paramater is a path to the browser on the "
01169 << endl;
01170 helpMsg
01171 << "local machine. If this optional parameter is specified then the "
01172 << endl;
01173 helpMsg << "solver result in OSrL format is transformed using XSLT into "
01174 << endl;
01175 helpMsg << "HTML and displayed in the browser. " << endl;
01176
01177 helpMsg << endl;
01178
01179 helpMsg
01180 << "-config pathToConfigureFile this parameter specifies a path on "
01181
01182 << endl;
01183 helpMsg
01184 << "the local machine to a text file containing values for the input parameters. "
01185 << endl;
01186 helpMsg
01187 << "This is convenient for the user not wishing to constantly retype parameter values. "
01188 << endl;
01189 helpMsg
01190 << "This configure file can contain values for all of the other parameters. "
01191 << endl;
01192
01193 helpMsg << endl;
01194
01195 helpMsg << "--version or -v get the current version of this executable "
01196 << endl;
01197
01198 helpMsg << endl;
01199
01200 helpMsg << "--help or -h to get this help file " << endl;
01201
01202 helpMsg << endl;
01203
01204 helpMsg
01205 << "Note: If you specify a configure file by using the -config option, you can "
01206 << endl;
01207 helpMsg
01208 << "override the values of the options in the configure file by putting them in "
01209 << endl;
01210 helpMsg << "at the command line. " << endl << endl;
01211
01212 helpMsg
01213 << "See the OS User\' Manual: http://www.coin-or.org/OS/doc/osUsersManual.pdf"
01214 << endl;
01215 helpMsg << "for more detail on how to use the OS project. " << endl;
01216
01217 helpMsg << endl;
01218 helpMsg << "********************************************************"
01219 << endl << endl;
01220
01221 return helpMsg.str();
01222 }
01223
01224