00001
00040 #include "CoinSolver.h"
00041 #include "OSConfig.h"
00042 #include "OSnl2osil.h"
00043 #include "OSiLReader.h"
00044 #include "OSrLReader.h"
00045 #include "OSiLWriter.h"
00046 #include "OSrLWriter.h"
00047 #include "OSInstance.h"
00048 #include "OSResult.h"
00049 #ifdef COIN_HAS_LINDO
00050 #include "LindoSolver.h"
00051 #endif
00052 #ifdef COIN_HAS_IPOPT
00053 #include "IpoptSolver.h"
00054 #endif
00055
00056 #include "DefaultSolver.h"
00057 #include "OSSolverAgent.h"
00058 #include "OShL.h"
00059 #include "ErrorClass.h"
00060 #include <sstream>
00061
00062 #ifdef HAVE_CSTRING
00063 # include <cstring>
00064 #else
00065 # ifdef HAVE_STRING_H
00066 # include <string.h>
00067 # else
00068 # error "don't have header file for string"
00069 # endif
00070 #endif
00071
00072
00073
00074 #include <asl.h>
00075 using std::cerr;
00076 using std::cout;
00077 using std::endl;
00078
00079
00080 int main(int argc, char **argv)
00081 {
00082 char *stub;
00083
00084 ASL *asl;
00085 asl = ASL_alloc(ASL_read_fg);
00086 stub = argv[1];
00087 jac0dim((char*)stub, (fint)strlen(stub));
00088 OSnl2osil *nl2osil = NULL;
00089
00090 nl2osil = new OSnl2osil( stub);
00091
00092 OSInstance *osinstance;
00093 std::cout << " call nl2osil" << std::endl;
00094 try{
00095 nl2osil->createOSInstance() ;
00096 }
00097 catch(const ErrorClass& eclass){
00098 std::cout << eclass.errormsg << std::endl;
00099 return 0;
00100 }
00101 std::cout << " return from nl2osil" << std::endl;
00102 osinstance = nl2osil->osinstance;
00103 std::cout << " osinstance created" << std::endl;
00104
00105
00106
00114 char *amplclient_options = NULL;
00115 char *agent_address = NULL;
00116 char *solver_option = NULL;
00117
00118 DefaultSolver *solverType = NULL;
00119 OSrLReader *osrlreader = NULL;
00120 osrlreader = new OSrLReader();
00121 OSrLWriter *osrlwriter;
00122 osrlwriter = new OSrLWriter();
00123 OSResult *osresult = NULL;
00124 std::string osrl = "";
00125 std::string sSolverName = "";
00126
00127 std::string osol = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <osol xmlns=\"os.optimizationservices.org\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"os.optimizationservices.org http://www.optimizationservices.org/schemas/OSoL.xsd\"><other> </other></osol>";
00128
00129 amplclient_options = getenv("amplClient_options");
00130 if(amplclient_options != NULL) cout << "HERE ARE THE AMPL CLIENT OPTIONS " << amplclient_options << endl;
00131 try{
00132 if(amplclient_options == NULL ) throw ErrorClass( "a local solver was not specified in AMPL option");
00133 else{
00134 if( strstr(amplclient_options, "lindo") != NULL) {
00135
00136 bool bLindoIsPresent = false;
00137 #ifdef COIN_HAS_LINDO
00138 sSolverName = "lindo";
00139 bLindoIsPresent = true;
00140 solver_option = getenv("lindo_options");
00141 if(( solver_option == NULL) || strstr(solver_option, "service") == NULL) solverType = new LindoSolver();
00142 #endif
00143 if(bLindoIsPresent == false) throw ErrorClass( "the Lindo solver requested is not present");
00144 }
00145 else{
00146 if( strstr(amplclient_options, "clp") != NULL){
00147 sSolverName = "clp";
00148 solver_option = getenv("clp_options");
00149 if( ( solver_option == NULL) || (strstr(solver_option, "service") == NULL) ){
00150 solverType = new CoinSolver();
00151 solverType->sSolverName = "clp";
00152 }
00153 }
00154 else{
00155 if( strstr(amplclient_options, "cbc") != NULL){
00156 sSolverName = "cbc";
00157 solver_option = getenv("cbc_options");
00158 if( ( solver_option == NULL) || (strstr(solver_option, "service") == NULL)){
00159 solverType = new CoinSolver();
00160 solverType->sSolverName = "cbc";
00161 }
00162 }
00163 else{
00164 if( strstr(amplclient_options, "cplex") != NULL){
00165 sSolverName = "cplex";
00166 solver_option = getenv("cplex_options");
00167 if( ( solver_option == NULL) || (strstr(solver_option, "service") == NULL)){
00168 solverType = new CoinSolver();
00169 solverType->sSolverName = "cplex";
00170 }
00171 }
00172 else{
00173 if( strstr(amplclient_options, "glpk") != NULL){
00174 solverType = new CoinSolver();
00175 sSolverName = "glpk";
00176 solver_option = getenv("glpk_options");
00177 if( ( solver_option == NULL) || (strstr(solver_option, "service") == NULL)){
00178 solverType = new CoinSolver();
00179 solverType->sSolverName = "glpk";
00180 }
00181 }
00182 else{
00183 if( strstr(amplclient_options, "ipopt") != NULL){
00184
00185
00186 sSolverName = "ipopt";
00187 solver_option = getenv("ipopt_options");
00188 if( amplclient_options != NULL) cout << "HERE ARE THE AMPLCLIENT OPTIONS " << amplclient_options << endl;
00189 if( solver_option != NULL) cout << "HERE ARE THE IPOPT OPTIONS " << solver_option << endl;
00190 bool bIpoptIsPresent = false;
00191 if( ( solver_option == NULL) || (strstr(solver_option, "service") == NULL) ){
00192 #ifdef COIN_HAS_IPOPT
00193 bIpoptIsPresent = true;
00194
00195 SmartPtr<IpoptSolver> ipoptSolver = new IpoptSolver();
00196 ipoptSolver->osol = osol;
00197 ipoptSolver->osinstance = osinstance;
00198 ipoptSolver->solve();
00199
00200 osrl = ipoptSolver->osrl ;
00201
00202
00203 #endif
00204 if(bIpoptIsPresent == false) throw ErrorClass( "the Ipopt solver requested is not present");
00205 }
00206 }
00207 else{
00208 if( strstr(amplclient_options, "symphony") != NULL){
00209 sSolverName = "symphony";
00210 solver_option = getenv("symphony_options");
00211 if( ( solver_option == NULL) || (strstr(solver_option, "service") == NULL)){
00212 solverType = new CoinSolver();
00213 solverType->sSolverName = "symphony";
00214 }
00215 }
00216 else{
00217 if( strstr(amplclient_options, "dylp") != NULL){
00218 sSolverName = "dylp";
00219 solver_option = getenv("dylp_options");
00220 if( ( solver_option == NULL) || (strstr(solver_option, "service") == NULL) ){
00221 solverType = new CoinSolver();
00222 solverType->sSolverName = "dylp";
00223 }
00224 }
00225 else{
00226 std::cout << "HERE I AM J" << std::endl;
00227 throw ErrorClass( "a supported solver has not been selected");
00228 }
00229 }
00230 }
00231 }
00232 }
00233 }
00234 }
00235 }
00236 }
00237
00238 if( (strstr(amplclient_options, "ipopt") == NULL) && (solver_option == NULL)){
00239 solverType->osol = osol;
00240 solverType->osinstance = osinstance;
00241 solverType->solve();
00242 osrl = solverType->osrl ;
00243 std::cout << osrl << std::endl;
00244 }
00245 }
00246 catch(const ErrorClass& eclass){
00247 osresult = new OSResult();
00248 osrl = eclass.errormsg;
00249 if(solverType == NULL){
00250 osresult->setGeneralMessage( eclass.errormsg);
00251 osresult->setGeneralStatusType( "error");
00252 osrl = osrlwriter->writeOSrL( osresult);
00253 }
00254 else {
00255 osrl = eclass.errormsg;
00256 }
00257 cout << osrl << endl << endl <<endl;
00258 write_sol(const_cast<char*>(osrl.c_str()),
00259 osresult->getOptimalPrimalVariableValues( -1),
00260 osresult->getOptimalDualVariableValues( -1), NULL);
00261 return 0;
00262 }
00263
00264 if( (solver_option != NULL) && (strstr(solver_option, "service") != NULL)){
00265 OSSolverAgent* osagent = NULL;
00266 OSiLWriter *osilwriter = NULL;
00267 osilwriter = new OSiLWriter();
00268 std::string osil = osilwriter->writeOSiL( osinstance);
00270 agent_address = strstr(solver_option, "service");
00271 agent_address += 7;
00272
00273 while(*agent_address == ' ') agent_address++;
00275 osagent = new OSSolverAgent( agent_address);
00276
00277 string::size_type iStringpos;
00278 iStringpos = osol.find("</osol");
00279
00280 std::string solverInput = "<other name=\"os_solver\">"
00281 + sSolverName + "</other>";
00282 osol.insert(iStringpos, solverInput);
00283 cout << "Place remote synchronous call" << endl;
00284 osrl = osagent->solve(osil, osol);
00285 }
00286
00287 try{
00288 cout << osrl << endl << endl <<endl;
00289 osresult = osrlreader->readOSrL( osrl);
00290 write_sol(const_cast<char*>(osresult->getSolutionMessage( 0).c_str()),
00291 osresult->getOptimalPrimalVariableValues( -1),
00292 osresult->getOptimalDualVariableValues( -1), NULL);
00293 delete osresult;
00294
00295 }
00296 catch(const ErrorClass& eclass){
00297 cout << "There was an error parsing the OSrL" << endl << eclass.errormsg << endl << endl;
00298 }
00299 delete osrlreader;
00300 osrlreader = NULL;
00301 if( strstr(amplclient_options, "ipopt") == NULL && ((solver_option != NULL) || (strstr(solver_option, "service") == NULL)) ){
00302
00303 delete solverType;
00304 solverType = NULL;
00305 }
00306 delete osrlwriter;
00307 osrlwriter = NULL;
00308 delete nl2osil;
00309 nl2osil = NULL;
00310 return 0;
00311 }