static std::string flush() { static bool alwaysStderr = std::getenv("FAKESOCKET_LOG_ALWAYS_STDERR") != nullptr; if (alwaysStderr) std::cerr << std::this_thread::get_id() << ":" << loggingBuffer.str() << std::endl; else if (loggingCallback != nullptr) loggingCallback(loggingBuffer.str()); loggingBuffer.str(""); return ""; }
/** Native interface to managed logger */ _declspec(dllexport) void Write(int level, const char * msg) { if(loggingCallback!=NULL) loggingCallback(level, msg); }
int main (int argc, char **argv) { char const *vmconfig = NULL; // Check command line length (exactly two arguments are required). if ( argc != 3 ) { usage (argv[0]); return -1; } // Pick up VMC from command line. vmconfig = argv[1]; // Solve the model. int exitcode = 0; IloEnv env; try { // Create and read the model. IloModel model(env); IloCplex cplex(model); IloObjective obj; IloNumVarArray var(env); IloRangeArray rng(env); IloSOS1Array sos1(env); IloSOS2Array sos2(env); IloRangeArray lazy(env); IloRangeArray cuts(env); cplex.importModel(model, argv[2], obj, var, rng, sos1, sos2, lazy, cuts); cplex.extract(model); if ( lazy.getSize() > 0 ) cplex.addLazyConstraints (lazy); if ( cuts.getSize() > 0 ) cplex.addUserCuts (cuts); // Load the virtual machine configuration. // This will force solve() to use parallel distributed MIP. cplex.readVMConfig(vmconfig); // Install logging info callback. IloNum lastObjVal = (obj.getSense() == IloObjective::Minimize ) ? IloInfinity : -IloInfinity; cplex.use(loggingCallback(env, var, -100000, lastObjVal, cplex.getCplexTime(), cplex.getDetTime())); // Turn off CPLEX logging cplex.setParam(IloCplex::Param::MIP::Display, 0); // Solve the problem and display some results. if ( cplex.solve() ) env.out() << "Solution value = " << cplex.getObjValue() << endl; else env.out() << "No solution" << endl; env.out() << "Solution status = " << cplex.getStatus() << endl; // Cleanup. cplex.end(); model.end(); } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; exitcode = -1; } catch (...) { cerr << "Unknown exception caught" << endl; exitcode = -1; } env.end(); return exitcode; } // END main
int main (int argc, char **argv) { IloEnv env; try { IloModel model(env); IloCplex cplex(env); IloBool useLoggingCallback = IloFalse; IloBool useTimeLimitCallback = IloFalse; IloBool useAborter = IloFalse; if (( argc != 3 ) || ( strchr ("lat", argv[2][0]) == NULL ) ) { usage (argv[0]); throw(-1); } switch (argv[2][0]) { case 'l': useLoggingCallback = IloTrue; break; case 't': useTimeLimitCallback = IloTrue; break; case 'a': useAborter = IloTrue; break; default: break; } IloObjective obj; IloNumVarArray var(env); IloRangeArray rng(env); IloSOS1Array sos1(env); IloSOS2Array sos2(env); IloRangeArray lazy(env); IloRangeArray cuts(env); IloCplex::Aborter myAborter; cplex.importModel(model, argv[1], obj, var, rng, sos1, sos2, lazy, cuts); cplex.extract(model); if ( lazy.getSize() > 0 ) cplex.addLazyConstraints (lazy); if ( cuts.getSize() > 0 ) cplex.addUserCuts (cuts); if ( useLoggingCallback ) { // Set an overall node limit in case callback conditions // are not met. cplex.setParam(IloCplex::Param::MIP::Limits::Nodes, 5000); IloNum lastObjVal = (obj.getSense() == IloObjective::Minimize ) ? IloInfinity : -IloInfinity; cplex.use(loggingCallback(env, var, -100000, lastObjVal, cplex.getCplexTime(), cplex.getDetTime())); // Turn off CPLEX logging cplex.setParam(IloCplex::Param::MIP::Display, 0); } else if ( useTimeLimitCallback ) { cplex.use(timeLimitCallback(env, cplex, IloFalse, cplex.getCplexTime(), 1.0, 10.0)); } else if ( useAborter ) { myAborter = IloCplex::Aborter(env); cplex.use(myAborter); // Typically, you would pass the Aborter object to // another thread or pass it to an interrupt handler, // and monitor for some event to occur. When it does, // call the Aborter's abort method. // // To illustrate its use without creating a thread or // an interrupt handler, abort immediately by calling // abort before the solve. // myAborter.abort(); } cplex.solve(); env.out() << endl; env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "CPLEX status = " << cplex.getCplexStatus() << endl; } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); return 0; } // END main