00001
00069 #include "OSCoinSolver.h"
00070 #include "OSResult.h"
00071 #include "OSiLReader.h"
00072 #include "OSiLWriter.h"
00073 #include "OSoLReader.h"
00074 #include "OSrLReader.h"
00075 #include "OSrLWriter.h"
00076 #include "OSInstance.h"
00077 #include "OSOption.h"
00078 #include "OSoLWriter.h"
00079 #include "OSFileUtil.h"
00080 #include "OSConfig.h"
00081 #include "OSDefaultSolver.h"
00082 #include "OSWSUtil.h"
00083 #include "OSSolverAgent.h"
00084 #include "OShL.h"
00085 #include "OSErrorClass.h"
00086 #include "OSmps2osil.h"
00087 #include "OSBase64.h"
00088 #include "OSRunSolver.h"
00089 #include "OSCommandLine.h"
00090 #include "OSCommandLineReader.h"
00091 #include "OSServiceMethods.h"
00092
00093 #ifdef COIN_HAS_KNITRO
00094 #include "OSKnitroSolver.h"
00095 #endif
00096
00097 #ifdef COIN_HAS_LINDO
00098 #include "OSLindoSolver.h"
00099 #endif
00100
00101 #ifdef COIN_HAS_ASL
00102 #include "OSnl2osil.h"
00103 #endif
00104
00105 #ifdef COIN_HAS_GAMSUTILS
00106 #include "OSgams2osil.hpp"
00107 #endif
00108
00109
00110
00111
00112
00113 #ifdef COIN_HAS_IPOPT
00114 #ifndef COIN_HAS_ASL
00115 #include "OSIpoptSolver.h"
00116 #undef COIN_HAS_ASL
00117 #else
00118 #include "OSIpoptSolver.h"
00119 #endif
00120 #endif
00121
00122 #ifdef COIN_HAS_BONMIN
00123 #include "OSBonminSolver.h"
00124 #endif
00125
00126 #ifdef COIN_HAS_COUENNE
00127 #include "OSCouenneSolver.h"
00128 #endif
00129
00130
00131
00132 #include<stdio.h>
00133 #include <map>
00134
00135
00136 using std::cout;
00137 using std::endl;
00138 using std::ostringstream;
00139 using std::string;
00140 using std::map;
00141
00142
00143
00144 void interactiveShell();
00145
00146 std::string get_help();
00147 std::string get_version();
00148 std::string get_options();
00149
00150
00151 bool callServiceMethod(OSCommandLine* oscommandline);
00152
00153
00154 void doPrintModel(OSCommandLine* oscommandline);
00155 void doPrintModel(OSInstance *osinstance);
00156 void doPrintRow(OSCommandLine* oscommandline);
00157 void doPrintRow(OSInstance *osinstance, std::string rownumberstring);
00158
00159
00160 int main(int argC, const char* argV[])
00161 {
00162 WindowsErrorPopupBlocker();
00163
00164 std::cout << OSgetVersionInfo();
00165 std::string osss;
00166
00167 if (argC < 2)
00168 {
00169 interactiveShell();
00170 return 0;
00171 }
00172
00173
00174 ostringstream outStr;
00175 outStr << argV[1];
00176 for (int i=2; i < argC; i++)
00177 outStr << " " << argV[i];
00178 osss = outStr.str();
00179
00180 #ifdef DEBUG_CL_INTERFACE
00181 std::cout << "Input String = " << osss << std::endl;
00182 #endif
00183
00184 FileUtil *fileUtil = NULL;
00185 OSCommandLine *oscommandline;
00186 oscommandline = new OSCommandLine();
00187 OSCommandLineReader *clreader;
00188 clreader = new OSCommandLineReader();
00189
00190
00191 try
00192 {
00193 oscommandline = clreader->readCommandLine(osss);
00194 if (oscommandline->serviceMethod == "")
00195 oscommandline->serviceMethod = "solve";
00196 oscommandline->convertSolverNametoLowerCase();
00197
00198 }
00199 catch (const ErrorClass& eclass)
00200 {
00201 OSResult *osresult = NULL;
00202 osresult = new OSResult();
00203 osresult->setGeneralMessage(eclass.errormsg);
00204 osresult->setGeneralStatusType("error");
00205 OSrLWriter *osrlwriter = NULL;
00206 osrlwriter = new OSrLWriter();
00207 std::string osrl = osrlwriter->writeOSrL(osresult);
00208 if (oscommandline->osrlFile != "")
00209 {
00210 fileUtil->writeFileFromString(oscommandline->osrlFile, osrl);
00211 if (oscommandline->browser != "")
00212 {
00213 std::string str = oscommandline->browser + " "
00214 + oscommandline->osrlFile;
00215 const char *ch = &str[0];
00216 std::system(ch);
00217 }
00218 }
00219 else
00220 {
00221 std::cout << osrl << std::endl;
00222 }
00223
00224 delete osresult;
00225 osresult = NULL;
00226 delete osrlwriter;
00227 osrlwriter = NULL;
00228 delete fileUtil;
00229 fileUtil = NULL;
00230 delete oscommandline;
00231 oscommandline = NULL;
00232 delete clreader;
00233 clreader = NULL;
00234 return 1;
00235 }
00236
00237 #ifdef DEBUG_CL_INTERFACE
00238 std::cout << oscommandline->list_options() << std::endl;
00239 #endif
00240
00241
00242 if (oscommandline->quit || oscommandline->invokeHelp || oscommandline->writeVersion || oscommandline->listOptions)
00243 {
00244 if (oscommandline->invokeHelp)
00245 std::cout << std::endl << std::endl << get_help() << std::endl;
00246 if (oscommandline->listOptions)
00247 std::cout << std::endl << std::endl << oscommandline->list_options() << std::endl;
00248 delete oscommandline;
00249 oscommandline = NULL;
00250 return 0;
00251 }
00252
00253
00254 bool result = callServiceMethod(oscommandline);
00255 if (result == true)
00256 return 0;
00257 else
00258 return 1;
00259 }
00260
00261
00262
00263 void interactiveShell()
00264 {
00265
00266 FileUtil *fileUtil = NULL;
00267 std::string configFileName = "";
00268
00269 OSCommandLine *oscommandline = new OSCommandLine();
00270 OSCommandLineReader *clreader = new OSCommandLineReader();
00271 OSServiceMethods *osservicemethods;
00272 osservicemethods = new OSServiceMethods();
00273
00274
00275
00276
00277
00278
00279
00280 std::string lineText;
00281
00282 std::string wordSep = " ";
00283 std::string optionName = "";
00284 std::string optionValue = "";
00285 std::string::size_type indexStart;
00286 std::string::size_type indexEnd;
00287 unsigned int k;
00288
00289
00290 std::string commandArray[6] = { "solve", "send", "getJobID", "retrieve", "kill", "knock"};
00291
00292 std::string actionItem[9] = { "quit", "exit", "reset", "list", "?", "help", "version",
00293 "printModel", "printRow"
00294 };
00295
00296 std::string optionArray[11] = { "osil", "osrl", "osol", "mps", "nl", "dat", "osilOutput",
00297 "serviceLocation", "solver", "osplInput", "osplOutput"
00298 };
00299
00300
00301 size_t size_of_commandArray = (sizeof commandArray)
00302 / (sizeof commandArray[0]);
00303
00304 size_t size_of_actionItem = (sizeof actionItem)
00305 / (sizeof actionItem[0]);
00306
00307 size_t size_of_optionArray = (sizeof optionArray)
00308 / (sizeof optionArray[0]);
00309
00310
00311
00312 std::map<string, int> commandMap;
00313
00314 for (k = 0; k < size_of_commandArray; k++)
00315 {
00316 commandMap[ commandArray[ k] ] = k;
00317 }
00318
00319
00320 std::map<string, int> actionItemMap;
00321
00322 for (k = 0; k < size_of_actionItem; k++)
00323 {
00324 actionItemMap[ actionItem[ k] ] = k;
00325 }
00326
00327
00328 std::map<string, int> optionMap;
00329
00330 for (k = 0; k < size_of_optionArray; k++)
00331 {
00332 optionMap[ optionArray[ k] ] = k;
00333 }
00334
00335 std::cout << "At the prompt enter a valid command or option value pair.\n";
00336 std::cout << "Enter the \"solve\" command to optimize.\n";
00337 std::cout << "Type \"quit\" or \"exit\" to leave the application. \n";
00338 std::cout << "Type \"help\" or \"?\" for a list of valid options.\n\n";
00339
00340 while (oscommandline->quit != true)
00341 {
00342 std::cout << "Please enter a command, or an option followed by an option value: ";
00343 getline(std::cin, lineText);
00344 lineText = " " + lineText + " ";
00345
00346 indexStart = lineText.find_first_not_of(wordSep);
00347 if (indexStart == string::npos)
00348 {
00349 std::cout << std::endl;
00350 std::cout << "You did not enter a valid option."
00351 << std::endl;
00352 }
00353 else
00354 {
00355 indexEnd = lineText.find_first_of(wordSep, indexStart + 1);
00356 optionName = lineText.substr(indexStart, indexEnd
00357 - indexStart);
00358
00359 if ( (commandMap.find(optionName) == commandMap.end() ) &&
00360 (actionItemMap.find(optionName) == actionItemMap.end() )&&
00361 (optionMap.find(optionName) == optionMap.end() ) )
00362 {
00363 std::cout << std::endl;
00364 std::cout << "You did not enter a valid option."
00365 << std::endl;
00366 }
00367 else
00368 {
00369
00370 indexStart = lineText.find_first_not_of(wordSep,indexEnd + 1);
00371 indexEnd = lineText.find_first_of(wordSep, indexStart + 1);
00372 if (indexStart != std::string::npos && indexEnd != std::string::npos)
00373 {
00374 optionValue = lineText.substr(indexStart, indexEnd - indexStart);
00375 }
00376 else
00377 {
00378 optionValue = "";
00379 }
00380
00381 #ifdef DEBUG_CL_INTERFACE
00382 std::cout << "Option name = " << optionName << std::endl;
00383 std::cout << "Option Value = " << optionValue << std::endl;
00384 #endif
00385
00386 try
00387 {
00388
00389 if (commandMap.find(optionName) != commandMap.end() )
00390 {
00391 oscommandline->serviceMethod = optionName;
00392 if (optionName == "solve")
00393 {
00394 if (oscommandline->osil == "" && oscommandline->mps == "" && oscommandline->nl == "")
00395 {
00396 std::cout
00397 << std::endl
00398 << "You did not specify an optimization instance!!!\n"
00399 << "Please enter file format option (osil, nl, or mps) \n"
00400 << "followed by the option value which is the file location. \n"
00401 << std::endl;
00402 }
00403 }
00404 else
00405 {
00406 if (oscommandline->serviceLocation == "")
00407 {
00408 std::cout
00409 << std::endl
00410 << "A service location is required"
00411 << std::endl;
00412 std::cout
00413 << "Please type the URL of the remote service: ";
00414 getline(std::cin, oscommandline->serviceLocation);
00415 }
00416 if (oscommandline->osolFile == "")
00417 {
00418 std::cout
00419 << std::endl
00420 << "Cannot execute command: no JobID and no OSoL file"
00421 << std::endl;
00422 }
00423 if (optionName == "knock")
00424 {
00425 if (oscommandline->osplInputFile == "")
00426 {
00427 std::cout
00428 << std::endl
00429 << "Cannot knock -- no OSplInputFile specified"
00430 << std::endl;
00431 }
00432 }
00433 }
00434
00435 bool result = callServiceMethod(oscommandline);
00436 }
00437
00438 else if (actionItemMap.find(optionName) != actionItemMap.end() )
00439 {
00440 switch (actionItemMap[ optionName] )
00441 {
00442 case 0:
00443 return;
00444
00445 case 1:
00446 return;
00447
00448 case 2:
00449 oscommandline->reset_options();
00450 std::cout << "\nAll options reset.\n";
00451 break;
00452
00453 case 3:
00454 oscommandline->list_options();
00455 break;
00456
00457 case 4:
00458 std::cout << get_options() << std::endl;
00459 break;
00460
00461 case 5:
00462 std::cout << get_options() << std::endl;
00463 break;
00464
00465 case 6:
00466 std::cout << OSgetVersionInfo() << std::endl;
00467 break;
00468
00469 case 7:
00470 doPrintModel(oscommandline);
00471 break;
00472
00473 case 8:
00474 if (optionValue == "")
00475 {
00476 std::cout
00477 << "Please enter a row number (>=0) or objective number (<0): ";
00478 getline(std::cin, optionValue);
00479 }
00480 doPrintRow(oscommandline);
00481 break;
00482
00483 default:
00484 throw ErrorClass("we don't have a valid command");
00485
00486
00487 }
00488
00489 }
00490
00491 else
00492 {
00493 if (optionValue == "")
00494 {
00495
00496 if(optionMap.find(optionName) != optionMap.end() )
00497 {
00498
00499 switch (optionMap[ optionName] )
00500 {
00501
00502 case 0:
00503 std::cout
00504 << "Please enter the name of an osil file: ";
00505 break;
00506
00507
00508 case 1:
00509 std::cout
00510 << "Please enter the name of an osrl file: ";
00511 break;
00512
00513 case 2:
00514 std::cout
00515 << "Please enter the name of an osol file: ";
00516 break;
00517
00518 case 3:
00519 std::cout
00520 << "Please enter the name of an mps file: ";
00521 break;
00522
00523 case 4:
00524 std::cout
00525 << "Please enter the name of an AMPL nl file: ";
00526 break;
00527
00528 case 5:
00529 std::cout
00530 << "Please enter the name of a dat file: ";
00531 break;
00532
00533 case 6:
00534 std::cout
00535 << "Please enter the serviceLocation: ";
00536 break;
00537
00538 case 7:
00539 std::cout
00540 << "Please enter the name of the solver: ";
00541 break;
00542
00543 case 8:
00544 std::cout
00545 << "Please enter the name of an osplInput file: ";
00546 break;
00547
00548 case 9:
00549 std::cout
00550 << "Please enter the name of an osplOutput file: ";
00551 break;
00552
00553
00554 case 10:
00555 std::cout
00556 << "Please enter the number of a constraint (>=0) or objective (<0): ";
00557 break;
00558
00559
00560 }
00561
00562
00563 getline(std::cin, optionValue);
00564 }
00565
00566 }
00567
00568 lineText = optionName + " " + optionValue + " ";
00569
00570 if(optionMap.find(optionName) != optionMap.end() )
00571 {
00572
00573 switch (optionMap[ optionName] )
00574 {
00575
00576 case 0:
00577 oscommandline->osilFile = optionValue;
00578 oscommandline->osil
00579 = fileUtil->getFileAsString(
00580 (oscommandline->osilFile).c_str());
00581 break;
00582
00583
00584 case 1:
00585 oscommandline->osrlFile = optionValue;
00586 break;
00587
00588 case 2:
00589 oscommandline->osolFile = optionValue;
00590 oscommandline->osol
00591 = fileUtil->getFileAsString(
00592 (oscommandline->osolFile).c_str());
00593 break;
00594
00595 case 3:
00596 oscommandline->mpsFile = optionValue;
00597 oscommandline->mps
00598 = fileUtil->getFileAsString(
00599 (oscommandline->mpsFile).c_str());
00600 break;
00601
00602 case 4:
00603 oscommandline->nlFile = optionValue;
00604 oscommandline->nl
00605 = fileUtil->getFileAsString(
00606 (oscommandline->nlFile).c_str());
00607 break;
00608
00609 case 5:
00610 oscommandline->datFile = optionValue;
00611 oscommandline->dat
00612 = fileUtil->getFileAsString(
00613 (oscommandline->datFile).c_str());
00614 break;
00615
00616 case 6:
00617 oscommandline->serviceLocation = optionValue;
00618 break;
00619
00620 case 7:
00621
00622
00623 for (k = 0; k < oscommandline->solverName.length(); k++)
00624 {
00625 oscommandline->solverName[k] =
00626 (char)tolower(oscommandline->solverName[k]);
00627 }
00628 oscommandline->solverName = optionValue;
00629 break;
00630
00631 case 8:
00632 oscommandline->osplInputFile = optionValue;
00633 oscommandline->osplInput
00634 = fileUtil->getFileAsString(
00635 (oscommandline->osplInputFile).c_str());
00636 break;
00637
00638 case 9:
00639 oscommandline->osplOutputFile = optionValue;
00640 break;
00641
00642
00643
00644
00645
00646
00647
00648
00649 }
00650 oscommandline->list_options();
00651
00652 }
00653
00654 }
00655
00656 std::cout << std::endl;
00657 }
00658 catch (const ErrorClass& eclass)
00659 {
00660 std::cout << eclass.errormsg << std::endl;
00661 }
00662 }
00663 }
00664 }
00665 delete oscommandline;
00666 oscommandline = NULL;
00667 delete fileUtil;
00668 fileUtil = NULL;
00669 }
00670
00671 std::string get_help()
00672 {
00673
00674 std::ostringstream helpMsg;
00675
00676 helpMsg << "************************* HELP *************************"
00677 << endl << endl;
00678 helpMsg
00679 << "In this HELP file we assume that the solve service method is used and "
00680 << endl;
00681 helpMsg
00682 << "that we are solving problems locally, that is the solver is on the "
00683 << endl;
00684 helpMsg
00685 << "machine running this OSSolverService. See Section 10.3 of the User\'s "
00686 << endl;
00687 helpMsg
00688 << "Manual for other service methods or calling a server remotely. "
00689 << endl;
00690 helpMsg << "The OSSolverService takes the parameters listed below. "
00691 << endl;
00692 helpMsg
00693 << "The order of the parameters is irrelevant. Not all the parameters "
00694 << endl;
00695 helpMsg << "are required. However, the location of an instance file is "
00696 << endl;
00697 helpMsg
00698 << "required when using the solve service method. The location of the "
00699 << endl;
00700 helpMsg << "instance file is specified using the osil option. " << endl;
00701
00702 helpMsg << endl;
00703
00704 helpMsg
00705 << "-osil xxx.osil this is the name of the file that contains the "
00706 << endl;
00707 helpMsg << "optimization instance in OSiL format. This option may be "
00708 << endl;
00709 helpMsg << "specified in the OSoL solver options file. " << endl;
00710
00711 helpMsg << endl;
00712
00713 helpMsg
00714 << "-osol xxx.osol this is the name of the file that contains the solver options. "
00715 << endl;
00716 helpMsg << "It is not necessary to specify this option. " << endl;
00717
00718 helpMsg << endl;
00719
00720 helpMsg
00721 << "-osrl xxx.osrl this is the name of the file to which the solver solution is written. "
00722 << endl;
00723 helpMsg
00724 << "It is not necessary to specify this option. If this option is not specified, "
00725 << endl;
00726 helpMsg << "the result will be printed to standard out. " << endl;
00727
00728 helpMsg << endl;
00729
00730 helpMsg
00731 << "-osplInput xxx.ospl this is the name of an input file in the OS Process"
00732 << endl;
00733 helpMsg << " Language (OSpL), this is used as input to the knock method."
00734 << endl;
00735
00736 helpMsg << endl;
00737
00738 helpMsg
00739 << "-osplOutput xxx.ospl this is the name of an output file in the OS Process"
00740 << endl;
00741 helpMsg
00742 << "Language (OSpL), this the output string from the knock and kill methods."
00743 << endl;
00744
00745 helpMsg << endl;
00746
00747 helpMsg << "-serviceLocation url is the URL of the solver service. "
00748 << endl;
00749 helpMsg
00750 << "This is not required, and if not specified it is assumed that "
00751 << endl;
00752 helpMsg << "the problem is solved locally. " << endl;
00753
00754 helpMsg << endl;
00755
00756 helpMsg
00757 << "-serviceMethod methodName this is the method on the solver service to be invoked. "
00758 << endl;
00759 helpMsg
00760 << "The options are solve, send, kill, knock, getJobID, and retrieve. "
00761 << endl;
00762 helpMsg
00763 << "This option is not required, and the default value is solve. "
00764 << endl;
00765
00766 helpMsg << endl;
00767
00768 helpMsg
00769 << "-mps xxx.mps this is the name of the mps file if the problem instance "
00770 << endl;
00771 helpMsg
00772 << "is in mps format. The default file format is OSiL so this option is not required. "
00773 << endl;
00774
00775 helpMsg << endl;
00776
00777 helpMsg
00778 << "-nl xxx.nl this is the name of the AMPL nl file if the problem "
00779 << endl;
00780 helpMsg
00781 << "instance is in AMPL nl format. The default file format is OSiL "
00782 << endl;
00783 helpMsg << "so this option is not required. " << endl;
00784
00785 helpMsg << endl;
00786
00787 helpMsg
00788 << "-solver solverName Possible values for default OS installation "
00789 << endl;
00790 helpMsg
00791 << "are bonmin(COIN-OR Bonmin), couenne (COIN-OR Couenne), clp (COIN-OR Clp),"
00792 << endl;
00793 helpMsg << "cbc (COIN-OR Cbc), dylp (COIN-OR DyLP), ipopt (COIN-OR Ipopt),"
00794 << endl;
00795 helpMsg << "and symphony (COIN-OR SYMPHONY). Other solvers supported"
00796 << endl;
00797 helpMsg
00798 << "(if the necessary libraries are present) are cplex (Cplex through COIN-OR Osi),"
00799 << endl;
00800 helpMsg
00801 << "glpk (glpk through COIN-OR Osi), knitro (Knitro), and lindo (LINDO)."
00802 << endl;
00803 helpMsg << "If no value is specified for this parameter," << endl;
00804 helpMsg << "then cbc is the default value of this parameter." << endl;
00805
00806 helpMsg << endl;
00807
00808 helpMsg
00809 << "-browser browserName this paramater is a path to the browser on the "
00810 << endl;
00811 helpMsg
00812 << "local machine. If this optional parameter is specified then the "
00813 << endl;
00814 helpMsg << "solver result in OSrL format is transformed using XSLT into "
00815 << endl;
00816 helpMsg << "HTML and displayed in the browser. " << endl;
00817
00818 helpMsg << endl;
00819
00820 helpMsg
00821 << "-config pathToConfigureFile this parameter specifies a path on "
00822 << endl;
00823 helpMsg
00824 << "the local machine to a text file containing values for the input parameters. "
00825 << endl;
00826 helpMsg
00827 << "This is convenient for the user not wishing to constantly retype parameter values. "
00828 << endl;
00829 helpMsg
00830 << "This configure file can contain values for all of the other parameters. "
00831 << endl;
00832
00833 helpMsg << endl;
00834
00835 helpMsg << "--version or -v get the current version of this executable "
00836 << endl;
00837
00838 helpMsg << endl;
00839
00840 helpMsg << "--help or -h to get this help file " << endl;
00841
00842 helpMsg << endl;
00843
00844 helpMsg
00845 << "Note: If you specify a configure file by using the -config option, you can "
00846 << endl;
00847 helpMsg
00848 << "override the values of the options in the configure file by putting them in "
00849 << endl;
00850 helpMsg << "at the command line. " << endl << endl;
00851
00852 helpMsg
00853 << "See the OS User\' Manual: http://www.coin-or.org/OS/doc/osUsersManual.pdf"
00854 << endl;
00855 helpMsg << "for more detail on how to use the OS project. " << endl;
00856
00857 helpMsg << endl;
00858 helpMsg << "********************************************************"
00859 << endl << endl;
00860
00861 return helpMsg.str();
00862 }
00863
00864
00865 std::string get_version()
00866 {
00867 std::ostringstream versionMsg;
00868 versionMsg << "In order to find the version of this project " << endl;
00869 versionMsg << "connect to the directory where you downloaded " << endl;
00870 versionMsg << "and do: " << endl;
00871 versionMsg << "svn info " << endl;
00872
00873 return versionMsg.str();
00874 }
00875
00876 #if 0
00877 void reset_options()
00878 {
00879 osoptions->configFile = "";
00880 osoptions->osilFile = "";
00881 osoptions->osil = "";
00882 osoptions->osolFile = "";
00883 osoptions->osol = "";
00884 osoptions->osrlFile = "";
00885 osoptions->osrl = "";
00886 osoptions->insListFile = "";
00887 osoptions->insList = "";
00888 osoptions->serviceLocation = "";
00889 osoptions->serviceMethod = "";
00890 osoptions->osplInputFile = "";
00891 osoptions->osplInput = "";
00892 osoptions->osplOutputFile = "";
00893 osoptions->osplOutput = "";
00894 osoptions->mpsFile = "";
00895 osoptions->mps = "";
00896 osoptions->nlFile = "";
00897 osoptions->nl = "";
00898 osoptions->gamsControlFile = "";
00899 osoptions->solverName = "";
00900 osoptions->browser = "";
00901 osoptions->jobID = "";
00902 osoptions->invokeHelp = false;
00903 osoptions->writeVersion = false;
00904 osoptions->printModel = false;
00905 osoptions->printRowNumberAsString = "";
00906 osoptions->quit = false;
00907 osoptions->exit = false;
00908 }
00909 #endif
00910
00911
00912 std::string get_options()
00913 {
00914 std::ostringstream optionMsg;
00915
00916 optionMsg << endl;
00917
00918 optionMsg
00919 << "***************** VALID COMMANDS AND OPTIONS ********************"
00920 << endl ;
00921 optionMsg
00922 << "COMMANDS:"
00923 << endl;
00924 optionMsg
00925 << "quit/exit -- terminate the executable"
00926 << endl;
00927 optionMsg
00928 << "help/? -- produce this list of options"
00929 << endl;
00930 optionMsg
00931 << "reset -- erase all previous option settings"
00932 << endl ;
00933 optionMsg
00934 << "list -- list the current option values"
00935 << endl ;
00936 optionMsg
00937 << "solve -- call the solver synchronously"
00938 << endl ;
00939 optionMsg
00940 << "send -- call the solver asynchronously"
00941 << endl ;
00942 optionMsg
00943 << "kill -- end a job on the remote server"
00944 << endl ;
00945 optionMsg
00946 << "retrieve -- get job result on the remote server"
00947 << endl ;
00948 optionMsg
00949 << "knock -- get job information on the remote server"
00950 << endl ;
00951 optionMsg
00952 << "getJobID -- get a job ID from the remote server"
00953 << endl << endl;
00954
00955
00956 optionMsg
00957 << "OPTIONS (THESE REQUIRE A VALUE):"
00958 << endl;
00959 optionMsg
00960 << "osil -- the location of the model instance in OSiL format"
00961 << endl;
00962 optionMsg
00963 << "mps -- the location of the model instance in MPS format"
00964 << endl ;
00965 optionMsg
00966 << "nl -- the location of the model instance in AMPL nl format"
00967 << endl;
00968 optionMsg
00969 << "osol -- the location of the solver option file in OSoL format"
00970 << endl;
00971 optionMsg
00972 << "osrl -- the location of the solver result file in OSrL format"
00973 << endl;
00974 optionMsg
00975 << "osplInput -- the name of an input file in OSpL format"
00976 << endl;
00977 optionMsg
00978 << "osplOutput -- the name of an output file in the OSpL format"
00979 << endl ;
00980 optionMsg
00981 << "serviceLocation -- the URL of a remote solver service"
00982 << endl;
00983 optionMsg
00984 << "solver -- specify the solver to invoke"
00985 << endl <<endl;
00986 optionMsg
00987 << "See http://www.coin-or.org/OS/"
00988 << endl;
00989 optionMsg
00990 << "for more detail on how to use the OS project."
00991 << endl << endl;
00992
00993 optionMsg
00994 << "PRINT OPTIONS:"
00995 << endl;
00996 optionMsg
00997 << "printModel -- print the currently defined model"
00998 << endl;
00999 optionMsg
01000 << "printRow nnn -- print row nnn of the currently defined model"
01001 << endl;
01002 optionMsg
01003 << " if nnn >= 0, prints a constraint, otherwise prints an objective row"
01004 << endl << endl;
01005
01006 optionMsg
01007 << "*****************************************************************"
01008 << endl << endl;
01009 optionMsg
01010 << "At the prompt enter a valid command or option value pair."
01011 << endl;
01012 optionMsg
01013 << "Enter the \"solve\" command to optimize."
01014 << endl;
01015 optionMsg
01016 << "Type \"quit/exit\" to leave the application."
01017 << endl;
01018 optionMsg
01019 << "Type \"help\" or \"?\" for a list of valid options."
01020 << endl;
01021
01022
01023 return optionMsg.str();
01024 }
01025
01026
01027
01028 void doPrintModel(OSCommandLine *oscommandline)
01029 {
01030 if (oscommandline->osil == "" && oscommandline->mps == "" && oscommandline->nl == "")
01031 {
01032 std::cout
01033 << "no instance defined; print command ignored" << std::endl;
01034 }
01035 else
01036 {
01037 if (oscommandline->osil != "")
01038 {
01039 OSiLReader *osilreader;
01040 osilreader = new OSiLReader();
01041 std::cout << osilreader->readOSiL(oscommandline->osil)->printModel() << std::endl;
01042 delete osilreader;
01043 osilreader = NULL;
01044 }
01045 else if (oscommandline->mps != "")
01046 {
01047 OSmps2osil *mps2osil;
01048 mps2osil = new OSmps2osil(oscommandline->mpsFile);
01049 mps2osil->createOSInstance();
01050 std::cout << mps2osil->osinstance->printModel() << std::endl;
01051 delete mps2osil;
01052 mps2osil = NULL;
01053 }
01054 else if (oscommandline->nl != "")
01055 {
01056 #ifdef COIN_HAS_ASL
01057
01058
01059
01060
01061
01062
01063 OSnl2osil *nl2osil;
01064 nl2osil = new OSnl2osil( oscommandline->nlFile);
01065 nl2osil->createOSInstance();
01066 std::cout << nl2osil->osinstance->printModel() << std::endl;
01067 delete nl2osil;
01068 nl2osil = NULL;
01069 #else
01070 std::cout << "no ASL present to read nl file; print command ignored" << std::endl;
01071 #endif
01072 }
01073 }
01074 }
01075
01076
01077 void doPrintModel(OSInstance *osinstance)
01078 {
01079 if (osinstance == NULL)
01080 {
01081 std::cout
01082 << "no instance defined; print command ignored" << std::endl;
01083 }
01084 else
01085 {
01086 std::cout << osinstance->printModel() << std::endl;
01087 }
01088 }
01089
01090 void doPrintRow(OSCommandLine *oscommandline)
01091 {
01092 int rownumber;
01093 if (oscommandline->printRowNumberAsString == "")
01094 std::cout << "no line number given; print command ignored" << std::endl;
01095 else
01096 {
01097 try
01098 {
01099 rownumber = atoi((oscommandline->printRowNumberAsString).c_str());
01100 }
01101 catch (const ErrorClass& eclass)
01102 {
01103 std::cout << "invalid row number; print command ignored" << std::endl;
01104 }
01105
01106 if (oscommandline->osil == "" && oscommandline->mps == "" && oscommandline->nl == "")
01107 {
01108 std::cout
01109 << "no instance defined; print command ignored" << std::endl;
01110 }
01111 else
01112 {
01113 std::cout << std::endl << "Row " << rownumber << ":" << std::endl << std::endl;
01114 if (oscommandline->osil != "")
01115 {
01116 OSiLReader *osilreader;
01117 osilreader = new OSiLReader();
01118 std::cout << osilreader->readOSiL(oscommandline->osil)->printModel(rownumber) << std::endl;
01119 delete osilreader;
01120 osilreader = NULL;
01121 }
01122 else if (oscommandline->mps != "")
01123 {
01124 OSmps2osil *mps2osil;
01125 mps2osil = new OSmps2osil(oscommandline->mpsFile);
01126 mps2osil->createOSInstance();
01127 std::cout << mps2osil->osinstance->printModel(rownumber) << std::endl;
01128 delete mps2osil;
01129 mps2osil = NULL;
01130 }
01131 else if (oscommandline->nl != "")
01132 {
01133 #ifdef COIN_HAS_ASL
01134
01135
01136
01137
01138
01139
01140 OSnl2osil *nl2osil;
01141 nl2osil = new OSnl2osil( oscommandline->nlFile);
01142 nl2osil->createOSInstance();
01143 std::cout << nl2osil->osinstance->printModel(rownumber) << std::endl;
01144 delete nl2osil;
01145 nl2osil = NULL;
01146 #else
01147 std::cout << "no ASL present to read nl file; print command ignored" << std::endl;
01148 #endif
01149 }
01150 }
01151 }
01152 }
01153
01154 void doPrintRow(OSInstance *osinstance, std::string rownumberstring)
01155 {
01156 int rownumber;
01157 if (rownumberstring == "")
01158 std::cout << "no line number given; print command ignored" << std::endl;
01159 else
01160 {
01161 try
01162 {
01163 rownumber = atoi((rownumberstring).c_str());
01164 }
01165 catch (const ErrorClass& eclass)
01166 {
01167 std::cout << "invalid row number; print command ignored" << std::endl;
01168 }
01169
01170 if (osinstance == NULL)
01171 {
01172 std::cout
01173 << "no instance defined; print command ignored" << std::endl;
01174 }
01175 else
01176 {
01177 std::cout << std::endl << "Row " << rownumber << ":" << std::endl << std::endl;
01178 std::cout << osinstance->printModel(rownumber) << std::endl;
01179 }
01180 }
01181 }
01182
01183 bool callServiceMethod(OSCommandLine* oscommandline)
01184 {
01185 FileUtil *fileUtil = new FileUtil();
01186 try
01187 {
01188 bool result;
01189 OSServiceMethods *osservicemethods;
01190 osservicemethods = new OSServiceMethods(oscommandline);
01191
01192 if (oscommandline->printModel)
01193 oscommandline->osinstance->printModel();
01194 if (oscommandline->printRowNumberAsString != "")
01195 oscommandline->osinstance->printModel(atoi((oscommandline->printRowNumberAsString).c_str()));
01196
01197 result = osservicemethods->executeServiceMethod(oscommandline);
01198
01199
01200 if (result == false)
01201 throw ErrorClass(osservicemethods->resultString);
01202 else
01203 {
01204 if (oscommandline->serviceMethod == "getJobID")
01205 {
01206 std::cout << "The job ID generated by the system is:" << std::endl << std::endl;
01207 std::cout << osservicemethods->resultString << std::endl << std::endl;
01208 std::cout << "Be sure to make a record of this job ID for later." << std::endl;
01209 std::cout << "You will need it to retrieve the job or inquire into its status." << std::endl;
01210 }
01211 else if (oscommandline->serviceMethod == "solve" || oscommandline->serviceMethod == "retrieve")
01212 {
01213 if (oscommandline->osrlFile != "")
01214 {
01215 fileUtil->writeFileFromString(oscommandline->osrlFile, osservicemethods->resultString);
01216 if (oscommandline->browser != "")
01217 {
01218 std::string str = oscommandline->browser + " "
01219 + oscommandline->osrlFile;
01220 const char *ch = &str[0];
01221 std::system(ch);
01222 }
01223 std::cout << "\nSolve command executed. Please see " << oscommandline->osrlFile << " for results." << std::endl;
01224 }
01225 else
01226 {
01227 std::cout << osservicemethods->resultString << std::endl;
01228 }
01229 }
01230 else if (oscommandline->serviceMethod == "kill" || oscommandline->serviceMethod == "knock")
01231 {
01232 if (oscommandline->osplOutputFile != "")
01233 {
01234 fileUtil->writeFileFromString(oscommandline->osplOutputFile, osservicemethods->resultString);
01235 }
01236 else
01237 {
01238 std::cout << osservicemethods->resultString << std::endl;
01239 }
01240 }
01241 }
01242 return 0;
01243 }
01244 catch (const ErrorClass& eclass)
01245 {
01246 OSResult *osresult = NULL;
01247 OSrLWriter *osrlwriter = NULL;
01248 osrlwriter = new OSrLWriter();
01249 osresult = new OSResult();
01250 osresult->setGeneralMessage(eclass.errormsg);
01251 osresult->setGeneralStatusType("error");
01252 std::string osrl = osrlwriter->writeOSrL(osresult);
01253 if (oscommandline->osrlFile != "")
01254 {
01255 fileUtil->writeFileFromString(oscommandline->osrlFile, osrl);
01256 if (oscommandline->browser != "")
01257 {
01258 std::string str = oscommandline->browser + " "
01259 + oscommandline->osrlFile;
01260 const char *ch = &str[0];
01261 std::system(ch);
01262 }
01263 }
01264 else
01265 {
01266 std::cout << osrl << std::endl;
01267 }
01268
01269 delete osresult;
01270 osresult = NULL;
01271 delete osrlwriter;
01272 osrlwriter = NULL;
01273
01274 delete oscommandline;
01275 oscommandline = NULL;
01276 delete fileUtil;
01277 fileUtil = NULL;
01278 return 1;
01279 }
01280 }