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