/** Deal with any error messages generated by the call to one of the service methods */ void reportErrors(OSCommandLine *oscommandline, std::string errormsg, OSnl2OS* nl2OS) { std::string osrl = ""; OSResult *osresult = NULL; OSrLWriter *osrlwriter = NULL; //first check to see if we already have OSrL, //if so don't create a new osresult object std::string::size_type pos1 = errormsg.find( "<osrl"); if(pos1 == std::string::npos) { osrlwriter = new OSrLWriter(); osresult = new OSResult(); osresult->setGeneralMessage(errormsg); osresult->setGeneralStatusType("error"); osrl = osrlwriter->writeOSrL(osresult); } else { osrl = errormsg; } reportResults(oscommandline, osrl, nl2OS); }// reportErrors
bool callServiceMethod(OSCommandLine* oscommandline) { FileUtil *fileUtil = new FileUtil(); try { bool result; OSServiceMethods *osservicemethods; osservicemethods = new OSServiceMethods(oscommandline); if (oscommandline->printModel) oscommandline->osinstance->printModel(); if (oscommandline->printRowNumberAsString != "") oscommandline->osinstance->printModel(atoi((oscommandline->printRowNumberAsString).c_str())); result = osservicemethods->executeServiceMethod(oscommandline); // deal with the output if (result == false) throw ErrorClass(osservicemethods->resultString); else { if (oscommandline->serviceMethod == "getJobID") { std::cout << "The job ID generated by the system is:" << std::endl << std::endl; std::cout << osservicemethods->resultString << std::endl << std::endl; std::cout << "Be sure to make a record of this job ID for later." << std::endl; std::cout << "You will need it to retrieve the job or inquire into its status." << std::endl; } else if (oscommandline->serviceMethod == "solve" || oscommandline->serviceMethod == "retrieve") { if (oscommandline->osrlFile != "") { fileUtil->writeFileFromString(oscommandline->osrlFile, osservicemethods->resultString); if (oscommandline->browser != "") { std::string str = oscommandline->browser + " " + oscommandline->osrlFile; const char *ch = &str[0]; std::system(ch); } std::cout << "\nSolve command executed. Please see " << oscommandline->osrlFile << " for results." << std::endl; } else { std::cout << osservicemethods->resultString << std::endl; } } else if (oscommandline->serviceMethod == "kill" || oscommandline->serviceMethod == "knock") { if (oscommandline->osplOutputFile != "") { fileUtil->writeFileFromString(oscommandline->osplOutputFile, osservicemethods->resultString); } else { std::cout << osservicemethods->resultString << std::endl; } } } return 0; } catch (const ErrorClass& eclass) { OSResult *osresult = NULL; OSrLWriter *osrlwriter = NULL; osrlwriter = new OSrLWriter(); osresult = new OSResult(); osresult->setGeneralMessage(eclass.errormsg); osresult->setGeneralStatusType("error"); std::string osrl = osrlwriter->writeOSrL(osresult); if (oscommandline->osrlFile != "") { fileUtil->writeFileFromString(oscommandline->osrlFile, osrl); if (oscommandline->browser != "") { std::string str = oscommandline->browser + " " + oscommandline->osrlFile; const char *ch = &str[0]; std::system(ch); } } else { std::cout << osrl << std::endl; } //catch garbage collection delete osresult; osresult = NULL; delete osrlwriter; osrlwriter = NULL; delete oscommandline; oscommandline = NULL; delete fileUtil; fileUtil = NULL; return 1; } }
int main( ){ WindowsErrorPopupBlocker(); // test OS code samples here FileUtil *fileUtil = NULL; fileUtil = new FileUtil(); const char dirsep = CoinFindDirSeparator(); // Set directory containing mps data files. std::string dataDir; dataDir = dirsep == '/' ? "../data/" : "..\\data\\"; cout << "Start Building the Model" << endl; try{ // get the p0033 problem std::string osilFileName; osilFileName = dataDir + "p0033.osil"; std::cout << "Try to read a sample file" << std::endl; std::cout << "The file is: " ; std::cout << osilFileName << std::endl; std::string osil = fileUtil->getFileAsString( osilFileName.c_str() ); OSiLReader *osilreader = NULL; osilreader = new OSiLReader(); OSInstance *osinstance; osinstance = osilreader->readOSiL( osil); // done writing the model cout << "Done writing the Model" << endl; // now solve the model CoinSolver *solver = NULL; solver = new CoinSolver(); solver->sSolverName ="cbc"; solver->osinstance = osinstance; solver->buildSolverInstance(); solver->osiSolver->setHintParam(OsiDoReducePrint, true, OsiHintTry); solver->osiSolver->initialSolve(); cout << "Here is the initial objective value " << solver->osiSolver->getObjValue() << endl; CglKnapsackCover cover; CglSimpleRounding round; CglGomory gomory; CbcModel *model = new CbcModel( *solver->osiSolver); //model->setBestObjectiveValue(4000); model->setMaximumNodes(100000); // model->addCutGenerator(&gomory, 1, "Gomory"); model->addCutGenerator(&cover, 1, "Cover"); model->addCutGenerator(&round, 1, "Round"); model->branchAndBound(); // now create a result object OSResult *osresult = new OSResult(); // if we are throw an exception if the problem is nonlinear double *x = NULL; double *y = NULL; double *z = NULL; //int i = 0; std::string *rcost = NULL; // resultHeader infomration if(osresult->setServiceName("Solved with Coin Solver: " + solver->sSolverName) != true) throw ErrorClass("OSResult error: setServiceName"); if(osresult->setInstanceName( solver->osinstance->getInstanceName()) != true) throw ErrorClass("OSResult error: setInstanceName"); if(osresult->setVariableNumber( solver->osinstance->getVariableNumber()) != true) throw ErrorClass("OSResult error: setVariableNumer"); if(osresult->setObjectiveNumber( 1) != true) throw ErrorClass("OSResult error: setObjectiveNumber"); if(osresult->setConstraintNumber( solver->osinstance->getConstraintNumber()) != true) throw ErrorClass("OSResult error: setConstraintNumber"); if(osresult->setSolutionNumber( 1) != true) throw ErrorClass("OSResult error: setSolutionNumer"); int solIdx = 0; std::string description = ""; osresult->setGeneralStatusType("success"); std::cout << "PROVEN OPTIMAL " << model->isProvenOptimal() << std::endl; int i; if (model->isProvenOptimal() == 1){ osresult->setSolutionStatus(solIdx, "optimal", description); /* Retrieve the solution */ x = new double[solver->osinstance->getVariableNumber() ]; y = new double[solver->osinstance->getConstraintNumber() ]; z = new double[1]; rcost = new std::string[ solver->osinstance->getVariableNumber()]; // *(z + 0) = model->getObjValue(); osresult->setObjectiveValuesDense(solIdx, z); for(i=0; i < solver->osinstance->getVariableNumber(); i++){ *(x + i) = model->getColSolution()[i]; } osresult->setPrimalVariableValuesDense(solIdx, x ); //if( solver->sSolverName.find( "symphony") == std::string::npos){ for(i=0; i < solver->osinstance->getConstraintNumber(); i++){ *(y + i) = model->getRowPrice()[ i]; } osresult->setDualVariableValuesDense(solIdx, y); // // now put the reduced costs into the osrl int numberOfOtherVariableResult = 1; int otherIdx = 0; // first set the number of Other Variable Results osresult->setNumberOfOtherVariableResults(solIdx, numberOfOtherVariableResult); std::ostringstream outStr; int numberOfVar = solver->osinstance->getVariableNumber(); for(i=0; i < numberOfVar; i++){ outStr << model->getReducedCost()[ i]; rcost[ i] = outStr.str(); outStr.str(""); } osresult->setAnOtherVariableResultDense(solIdx, otherIdx, "reduced costs", "", "the variable reduced costs", rcost); // end of settiing reduced costs } else{ if(solver->osiSolver->isProvenPrimalInfeasible() == true) osresult->setSolutionStatus(solIdx, "infeasible", description); else if(solver->osiSolver->isProvenDualInfeasible() == true) osresult->setSolutionStatus(solIdx, "dualinfeasible", description); else osresult->setSolutionStatus(solIdx, "other", description); } OSrLWriter *osrlwriter = new OSrLWriter(); std::cout << osrlwriter->writeOSrL( osresult) << std::endl; if(solver->osinstance->getVariableNumber() > 0){ delete[] x; x = NULL; } if(solver->osinstance->getConstraintNumber()) delete[] y; y = NULL; delete[] z; z = NULL; if(solver->osinstance->getVariableNumber() > 0){ delete[] rcost; rcost = NULL; } // do garbage collection delete osresult; osresult = NULL; delete osrlwriter; osrlwriter = NULL; delete solver; solver = NULL; delete osilreader; osilreader = NULL; delete fileUtil; fileUtil = NULL; delete model; model = NULL; cout << "Done with garbage collection" << endl; return 0; } catch(const ErrorClass& eclass){ delete fileUtil; std::cout << eclass.errormsg << std::endl; return 0; } }// end main
int main(int argc, char **argv) { WindowsErrorPopupBlocker(); char *stub; // set AMPL structures ASL *asl; asl = ASL_alloc(ASL_read_fg); stub = argv[1]; jac0dim((char*)stub, (fint)strlen(stub)); OSnl2osil *nl2osil = NULL; std::ostringstream outStr; std::cout << OSAmplClientVersionInfo(); std::string osss; outStr << stub << " " << getenv("OSAmplClient_options"); osss = outStr.str(); #ifdef DEBUG_AMPL_CLIENT std::cout << "Input String = " << osss << std::endl; #endif FileUtil *fileUtil = NULL; OSCommandLine *oscommandline; oscommandline = new OSCommandLine(); OSCommandLineReader *clreader; clreader = new OSCommandLineReader(); //parse the command line items into a CommandLine object try { oscommandline = clreader->readCommandLine(osss); if (oscommandline->serviceMethod == "") oscommandline->serviceMethod = "solve"; oscommandline->convertSolverNametoLowerCase(); } catch (const ErrorClass& eclass) { OSResult *osresult = NULL; osresult = new OSResult(); osresult->setGeneralMessage(eclass.errormsg); osresult->setGeneralStatusType("error"); OSrLWriter *osrlwriter = NULL; osrlwriter = new OSrLWriter(); std::string osrl = osrlwriter->writeOSrL(osresult); if (oscommandline->osrlFile != "") { fileUtil->writeFileFromString(oscommandline->osrlFile, osrl); if (oscommandline->browser != "") { std::string str = oscommandline->browser + " " + oscommandline->osrlFile; const char *ch = &str[0]; std::system(ch); } } else { std::cout << osrl << std::endl; } //catch garbage collection delete osresult; osresult = NULL; delete osrlwriter; osrlwriter = NULL; delete fileUtil; fileUtil = NULL; delete oscommandline; oscommandline = NULL; delete clreader; clreader = NULL; return 1; } #ifdef DEBUG_AMPL_CLIENT std::cout << oscommandline->list_options() << std::endl; #endif // deal with "action items": help, quit, listOptions if (oscommandline->quit || oscommandline->invokeHelp || oscommandline->writeVersion || oscommandline->listOptions) { if (oscommandline->invokeHelp) std::cout << std::endl << std::endl << get_AmplClient_help() << std::endl; if (oscommandline->listOptions) std::cout << std::endl << std::endl << oscommandline->list_options() << std::endl; delete oscommandline; oscommandline = NULL; return 0; } // call service method bool result = callServiceMethod(oscommandline); if (result == true) return 0; else return 1; }
bool OSServiceMethods::executeServiceMethod(OSCommandLine *oscommandline) { OSSolverAgent* osagent = NULL; /** * The required file conversions are all assumed * to have been taken care of in the nonstandard constructor, * so this wrapper merely directs traffic */ try { /** the only local service method is solve() */ if (oscommandline->serviceLocation == "") if (oscommandline->serviceMethod == "solve") //resultString = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osinstance); int junk; else throw ErrorClass("No service location (URL) specified. Only \"solve\" is available locally"); /** Here we have a remote call --- reuse as much code as possible */ else { osagent = new OSSolverAgent(oscommandline->serviceLocation); //solve if (oscommandline->serviceMethod == "solve") resultString = osagent->solve(oscommandline->osil, oscommandline->osol); //send --- first check that there is a jobID. If not, get one else if (oscommandline->serviceMethod == "send") { if (oscommandline->osol.find( "<jobID") == std::string::npos) { OSoLReader *osolreader = NULL; osolreader = new OSoLReader(); OSoLWriter *osolwriter = NULL; osolwriter = new OSoLWriter(); try { if (oscommandline->osol != "") oscommandline->osoption = osolreader->readOSoL(oscommandline->osol); oscommandline->osoption->setJobID(osagent->getJobID("")); // now write the osOption object into a string oscommandline->osol = osolwriter->writeOSoL(oscommandline->osoption); delete osolreader; osolreader = NULL; delete osolwriter; osolwriter = NULL; } catch (const ErrorClass& eclass) { delete osolreader; osolreader = NULL; delete osolwriter; osolwriter = NULL; throw ErrorClass(eclass.errormsg); } } bool sendResult; sendResult = osagent->send(oscommandline->osil, oscommandline->osol); if (sendResult == false) throw ErrorClass("send() method failed"); } //retrieve else if (oscommandline->serviceMethod == "retrieve") { if (oscommandline->osol.find( "<jobID") == std::string::npos) throw ErrorClass("there is no JobID to retrieve"); resultString = osagent->retrieve(oscommandline->osol); } //getJobID else if (oscommandline->serviceMethod == "getJobID") resultString = osagent->getJobID(oscommandline->osol); //knock else if (oscommandline->serviceMethod == "knock") resultString = osagent->knock(oscommandline->osplInput, oscommandline->osol); //kill else if (oscommandline->serviceMethod == "kill") { if (oscommandline->osol.find( "<jobID") == std::string::npos) throw ErrorClass("there is no JobID to kill"); resultString = osagent->kill(oscommandline->osol); } else throw ErrorClass("serviceMethod not recognized"); delete osagent; osagent = NULL; } return true; } catch (const ErrorClass& eclass) { OSResult *osresult = NULL; OSrLWriter *osrlwriter = NULL; osrlwriter = new OSrLWriter(); osresult = new OSResult(); osresult->setGeneralMessage(eclass.errormsg); osresult->setGeneralStatusType("error"); resultString = osrlwriter->writeOSrL(osresult); //catch garbage collection delete osresult; osresult = NULL; delete osrlwriter; osrlwriter = NULL; if (osagent != NULL) delete osagent; osagent = NULL; return false; } }//executeServiceMethod