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