long GenModelCplex::SetDirectParam(int whichparam, genmodel_param value, string type, string message) { int status = 0; if (type == "dbl") status = CPXsetdblparam(static_cast<CplexData*>(solverdata)->env, whichparam, value.dblval); else if (type == "long") status = CPXsetintparam(static_cast<CplexData*>(solverdata)->env, whichparam, value.longval); else if (type == "str") status = CPXsetstrparam(static_cast<CplexData*>(solverdata)->env, whichparam, value.strval); if (status) return ThrowError(getcplexerror(static_cast<CplexData*>(solverdata)->env, status) + string(". ") + message); return 0; }
long GenModelCplex::Init(string name) { //strParam.count("log_file") //dblParam.count("relative_mip_gap_tolerance") //dblParam.count("absolute_mip_gap_tolerance") //dblParam.count("time_limit") //dblParam.count("bounds_feasibility_tolerance") //dblParam.count("optimality_tolerance") //dblParam.count("markowitz_tolerance")) //longParam.count("threads") //longParam.count("cutpass") //longParam.count("pumplevel") //longParam.count("mipemphasis") //longParam.count("probinglevel") //longParam.count("max_iteration_limit"); //boolParam.count("preprocoff") : turn on/off preprocessing //boolParam.count("datacheckoff") //boolParam.count("screen_output") //boolParam.count("usecutcb") if(solverdata == NULL) solverdata = new CplexData(); else { static_cast<CplexData*>(solverdata)->Delete(); static_cast<CplexData*>(solverdata)->Reset(); } CplexData* d = static_cast<CplexData*>(solverdata); int status = 0; d->env = CPXopenCPLEX (&status); // If an error occurs if ( d->env == NULL ) return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Could not open CPLEX environment")); hassolution = false; // Log file if(strParam.count("log_file") > 0) { d->cpxfileptr = CPXfopen(strParam["log_file"].c_str(), "w"); status = CPXsetlogfile(d->env, d->cpxfileptr); if ( status ) return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Failure to set the log file")); } // General settings boolParam["log_output_stdout"] = true; SetParam("log_output_stdout", CPX_PARAM_SCRIND, "bool", "Failure to turn on/off log output to stdout"); SetParam("log_level", CPX_PARAM_MIPDISPLAY, "long", "Failure to set log level"); SetParam("use_data_checking", CPX_PARAM_DATACHECK, "bool", "Failure to turn on/off data checking"); SetParam("nb_threads", CPX_PARAM_THREADS, "long", "Failure to set the number of threads"); if(boolParam.count("use_preprocessor") > 0 && !boolParam["use_preprocessor"]) { SetDirectParam(CPX_PARAM_AGGFILL, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_AGGFILL)"); SetDirectParam(CPX_PARAM_PREPASS, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_PREPASS)"); SetDirectParam(CPX_PARAM_AGGIND, long2param(CPX_OFF), "long", "Failure to use preprocessor (CPX_PARAM_AGGIND)"); SetDirectParam(CPX_PARAM_DEPIND, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_DEPIND)"); SetDirectParam(CPX_PARAM_PRELINEAR, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_PRELINEAR)"); SetDirectParam(CPX_PARAM_PREDUAL, long2param(-1), "long", "Failure to use preprocessor (CPX_PARAM_PREDUAL)"); SetDirectParam(CPX_PARAM_REDUCE, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_REDUCE)"); SetDirectParam(CPX_PARAM_PREIND, long2param(CPX_OFF), "long", "Failure to use preprocessor (CPX_PARAM_PREIND)"); } // MIP settings SetParam("nb_cut_pass", CPX_PARAM_CUTPASS, "long", "Failure to set the number of cut pass"); SetParam("feasibility_pump_level", CPX_PARAM_FPHEUR, "long", "Failure to set the feasibility pump level"); SetParam("probing_level", CPX_PARAM_PROBE, "long", "Failure to set the probing level"); SetParam("mip_emphasis", CPX_PARAM_MIPEMPHASIS, "long", "Failure to set the MIP emphasis"); SetParam("mip_search", CPX_PARAM_MIPSEARCH, "long", "Failure to set the MIP search strategy"); SetParam("starting_algo", CPX_PARAM_STARTALG, "long", "Failure to set the starting algo parameter"); SetParam("node_algo", CPX_PARAM_SUBALG, "long", "Failure to set the node algo parameter"); if(boolParam.count("use_cut_callback") > 0 && boolParam["use_cut_callback"]) { SetDirectParam(CPX_PARAM_PRELINEAR, long2param(0), "long", "Failure to use cut callback (CPX_PARAM_PRELINEAR)"); SetDirectParam(CPX_PARAM_MIPCBREDLP, long2param(0), "long", "Failure to use cut callback (CPX_PARAM_MIPCBREDLP)"); } // Tolerance and limits SetParam("time_limit", CPX_PARAM_TILIM, "dbl", "Failure to set time limit"); SetParam("max_iteration_limit", CPX_PARAM_ITLIM, "long", "Failure to set the maximal number of simplex iterations"); SetParam("bounds_feasibility_tolerance", CPX_PARAM_EPRHS, "dbl", "Failure to set bounds feasibility tolerance"); SetParam("optimality_tolerance", CPX_PARAM_EPOPT, "dbl", "Failure to set optimality tolerance"); SetParam("markowitz_tolerance", CPX_PARAM_EPMRK, "dbl", "Failure to set Markowitz tolerance"); SetParam("absolute_mip_gap_tolerance", CPX_PARAM_EPAGAP, "dbl", "Failure to set absolute gap tolerance"); SetParam("relative_mip_gap_tolerance", CPX_PARAM_EPGAP, "dbl", "Failure to set relative gap tolerance"); if(boolParam.count("maximize") > 0 && boolParam["maximize"]) SetParam("lp_objective_limit", CPX_PARAM_OBJULIM, "dbl", "Failure to set lp objective limit"); else SetParam("lp_objective_limit", CPX_PARAM_OBJLLIM, "dbl", "Failure to set lp objective limit"); // MIP Emphasis if(longParam.count("mipemphasis")) { status = CPXsetintparam (d->env, CPX_PARAM_MIPEMPHASIS, longParam["mipemphasis"]); } // Probing level if(longParam.count("probinglevel")) { status = CPXsetintparam (d->env, CPX_PARAM_PROBE, longParam["probinglevel"]); } if ( status ) { fprintf (stderr, "Failure to set cut callback parameters, error %d->\n", status); return 1; } binit = true; if(dblParam.count("feastol")) status = CPXsetdblparam (d->env, CPX_PARAM_EPRHS, dblParam["feastol"]); if ( status ) { fprintf (stderr, "Failure to change feasibility tolerance, error %d->\n", status); return 1; } if(dblParam.count("opttol")) status = CPXsetdblparam (d->env, CPX_PARAM_EPOPT, dblParam["opttol"]); if ( status ) { fprintf (stderr, "Failure to change optimality tolerance, error %d->\n", status); return 1; } if(dblParam.count("marktol")) status = CPXsetdblparam (d->env, CPX_PARAM_EPMRK, dblParam["marktol"]); if ( status ) { fprintf (stderr, "Failure to change Markowitz tolerance, error %d->\n", status); return 1; } if(longParam.count("threads")) { printf("Threads: %ld\n", longParam["threads"]); status = CPXsetintparam (d->env, CPX_PARAM_THREADS, longParam["threads"]); if ( status ) { fprintf (stderr, "Failure to change the number of threads, error %d->\n", status); return 1; } } // Turn off preprocessing if(boolParam.count("preprocoff") > 0 && boolParam["preprocoff"]) { status = CPXsetintparam (d->env, CPX_PARAM_AGGFILL, 0); status = status && CPXsetintparam (d->env, CPX_PARAM_PREPASS, 0); status = status && CPXsetintparam (d->env, CPX_PARAM_AGGIND, CPX_OFF); status = status && CPXsetintparam (d->env, CPX_PARAM_DEPIND, 0); status = status && CPXsetintparam (d->env, CPX_PARAM_PRELINEAR, 0); status = status && CPXsetintparam (d->env, CPX_PARAM_PREDUAL, -1); status = status && CPXsetintparam (d->env, CPX_PARAM_REDUCE, 0); status = status && CPXsetintparam (d->env, CPX_PARAM_PREIND, CPX_OFF); } if ( status ) { fprintf (stderr, "Failure to turn off preprocessing, error %d->\n", status); return 1; } // Turn on data checking if(boolParam.count("datacheckoff") > 0 && boolParam["datacheckoff"]) status = CPXsetintparam (d->env, CPX_PARAM_DATACHECK, CPX_OFF); else status = CPXsetintparam (d->env, CPX_PARAM_DATACHECK, CPX_ON); if ( status ) { fprintf (stderr, "Failure to turn on data checking, error %d->\n", status); return 1; } // Sets a relative tolerance on the gap between the best integer objective and the objective of the best node remaining (between 0.0 and 1.0) if(dblParam.count("epgap")) { //printf("setting epgap\n"); status = CPXsetdblparam (d->env, CPX_PARAM_EPGAP, dblParam["epgap"]); if ( status ) { fprintf (stderr, "Failure to set relative gap tolerance, error %d->\n", status); return 1; } } // Create the problem d->lp = CPXcreateprob (d->env, &status, name.c_str()); if ( d->lp == NULL ) { fprintf (stderr, "Failed to create LP.\n"); return 1; } fflush(stdout); return 0; }
long GenModelCplex::SetSol() { if(!bcreated) throw string("SetSol() not available : Problem not created yet"); vars.sol.clear(); vars.sol.resize(vars.n,0); vars.rc.clear(); vars.rc.resize(vars.n,0); CplexData* d = (CplexData*)solverdata; int status = 0; if(d->x != NULL) delete[] d->x; if(d->dual != NULL) delete[] d->dual; if(d->rcost != NULL) delete[] d->rcost; if(d->slack != NULL) delete[] d->slack; d->x = new double[nc]; d->dual = new double[nr]; d->rcost = new double[nc]; d->slack = new double[nr]; int tempstat = CPXgetstat (d->env, d->lp); int tempfeas; int tempdualfeas; int temphassol; int currmeth = CPXgetmethod(d->env, d->lp); status = CPXsolninfo(d->env, d->lp, &currmeth, &temphassol, &tempfeas, &tempdualfeas); if ( status ) return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Failure to set set solution (CPXsolninfo)")); feasible = static_cast<bool>(tempfeas); dualfeasible = static_cast<bool>(tempdualfeas); hassolution= static_cast<bool>(temphassol); if(!hassolution) return 1; if(boolParam.count("mip") > 0 && boolParam["mip"]) status = CPXsolution (d->env, d->lp, &solstat, &objval, d->x, NULL, NULL, NULL); else status = CPXsolution (d->env, d->lp, &solstat, &objval, d->x, d->dual, d->slack, d->rcost); if ( status ) return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Failure to set set solution (CPXsolution)")); solstat = tempstat; for(long i = 0; i < long(nc); i++) { vars.sol[i] = d->x[i]; vars.rc[i] = d->rcost[i]; } for(long i = 0; i < long(nr); i++) { consts[i].dual = d->dual[i]; consts[i].slack = d->slack[i]; } if(boolParam.count("print_version") > 0 && boolParam["print_version"]) printf("*********** Genmodel version = %s ***********\n", version.c_str()); return solstat; }
long GenModelCplex::Init(string name) { //strParam.count("log_file") //dblParam.count("relative_mip_gap_tolerance") //dblParam.count("absolute_mip_gap_tolerance") //dblParam.count("time_limit") //dblParam.count("bounds_feasibility_tolerance") //dblParam.count("optimality_tolerance") //dblParam.count("markowitz_tolerance")) //longParam.count("threads") //longParam.count("cutpass") //longParam.count("pumplevel") //longParam.count("mipemphasis") //longParam.count("probinglevel") //longParam.count("max_iteration_limit"); //boolParam.count("preprocoff") : turn on/off preprocessing //boolParam.count("datacheckoff") //boolParam.count("screen_output") //boolParam.count("usecutcb") if(solverdata == NULL) solverdata = new CplexData(); else { static_cast<CplexData*>(solverdata)->Delete(); static_cast<CplexData*>(solverdata)->Reset(); } CplexData* d = static_cast<CplexData*>(solverdata); int status = 0; d->env = CPXopenCPLEX (&status); // If an error occurs if ( d->env == NULL ) return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Could not open CPLEX environment")); hassolution = false; // Log file if(strParam.count("log_file") > 0) { d->cpxfileptr = CPXfopen(strParam["log_file"].c_str(), "w"); status = CPXsetlogfile(d->env, d->cpxfileptr); if ( status ) return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Failure to set the log file")); } // General settings boolParam["log_output_stdout"] = true; SetParam("log_output_stdout", CPX_PARAM_SCRIND, "bool", "Failure to turn on/off log output to stdout"); SetParam("log_level", 0, "long", "Failure to set log level", false); SetParam("use_data_checking", CPX_PARAM_DATACHECK, "bool", "Failure to turn on/off data checking"); SetParam("nb_threads", CPX_PARAM_THREADS, "long", "Failure to set the number of threads"); if(boolParam.count("use_preprocessor") > 0 && !boolParam["use_preprocessor"]) { SetDirectParam(CPX_PARAM_AGGFILL, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_AGGFILL)"); SetDirectParam(CPX_PARAM_PREPASS, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_PREPASS)"); SetDirectParam(CPX_PARAM_AGGIND, long2param(CPX_OFF), "long", "Failure to use preprocessor (CPX_PARAM_AGGIND)"); SetDirectParam(CPX_PARAM_DEPIND, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_DEPIND)"); SetDirectParam(CPX_PARAM_PRELINEAR, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_PRELINEAR)"); SetDirectParam(CPX_PARAM_PREDUAL, long2param(-1), "long", "Failure to use preprocessor (CPX_PARAM_PREDUAL)"); SetDirectParam(CPX_PARAM_REDUCE, long2param(0), "long", "Failure to use preprocessor (CPX_PARAM_REDUCE)"); SetDirectParam(CPX_PARAM_PREIND, long2param(CPX_OFF), "long", "Failure to use preprocessor (CPX_PARAM_PREIND)"); } // MIP settings SetParam("nb_cut_pass", CPX_PARAM_CUTPASS, "long", "Failure to set the number of cut pass"); SetParam("feasibility_pump_level", CPX_PARAM_FPHEUR, "long", "Failure to set the feasibility pump level"); SetParam("probing_level", CPX_PARAM_PROBE, "long", "Failure to set the probing level"); SetParam("mip_emphasis", CPX_PARAM_MIPEMPHASIS, "long", "Failure to set the MIP emphasis"); if(boolParam.count("use_cut_callback") > 0 && boolParam["use_cut_callback"]) { SetDirectParam(CPX_PARAM_PRELINEAR, long2param(0), "long", "Failure to use cut callback (CPX_PARAM_PRELINEAR)"); SetDirectParam(CPX_PARAM_MIPCBREDLP, long2param(0), "long", "Failure to use cut callback (CPX_PARAM_MIPCBREDLP)"); } // Tolerance and limits SetParam("time_limit", CPX_PARAM_TILIM, "dbl", "Failure to set time limit"); SetParam("max_iteration_limit", CPX_PARAM_ITLIM, "long", "Failure to set the maximal number of simplex iterations"); SetParam("bounds_feasibility_tolerance", CPX_PARAM_EPRHS, "dbl", "Failure to set bounds feasibility tolerance"); SetParam("optimality_tolerance", CPX_PARAM_EPOPT, "dbl", "Failure to set optimality tolerance"); SetParam("markowitz_tolerance", CPX_PARAM_EPMRK, "dbl", "Failure to set Markowitz tolerance"); SetParam("absolute_mip_gap_tolerance", CPX_PARAM_EPAGAP, "dbl", "Failure to set absolute gap tolerance"); SetParam("relative_mip_gap_tolerance", CPX_PARAM_EPGAP, "dbl", "Failure to set relative gap tolerance"); if(boolParam.count("maximize") > 0 && boolParam["maximize"]) SetParam("lp_objective_limit", CPX_PARAM_OBJULIM, "dbl", "Failure to set lp objective limit"); else SetParam("lp_objective_limit", CPX_PARAM_OBJLLIM, "dbl", "Failure to set lp objective limit"); // Create the problem d->lp = CPXcreateprob (d->env, &status, name.c_str()); if ( d->lp == NULL ) return ThrowError(getcplexerror(d->env, status)+string(". ")+string("Failure to create Cplex optimization problem")); binit = true; return 0; }