int main(int argc, char *argv[]) { if (argc < 2) { cout << "Usage: lpmod_c++ filename" << endl; return 1; } GRBEnv* env = 0; GRBVar* v = 0; try { // Read model and determine whether it is an LP env = new GRBEnv(); GRBModel model = GRBModel(*env, argv[1]); if (model.get(GRB_IntAttr_IsMIP) != 0) { cout << "The model is not a linear program" << endl; return 1; } model.optimize(); int status = model.get(GRB_IntAttr_Status); if ((status == GRB_INF_OR_UNBD) || (status == GRB_INFEASIBLE) || (status == GRB_UNBOUNDED)) { cout << "The model cannot be solved because it is " << "infeasible or unbounded" << endl; return 1; } if (status != GRB_OPTIMAL) { cout << "Optimization was stopped with status " << status << endl; return 0; } // Find the smallest variable value double minVal = GRB_INFINITY; int minVar = 0; v = model.getVars(); for (int j = 0; j < model.get(GRB_IntAttr_NumVars); ++j) { double sol = v[j].get(GRB_DoubleAttr_X); if ((sol > 0.0001) && (sol < minVal) && (v[j].get(GRB_DoubleAttr_LB) == 0.0)) { minVal = sol; minVar = j; } } cout << "\n*** Setting " << v[minVar].get(GRB_StringAttr_VarName) << " from " << minVal << " to zero ***" << endl << endl; v[minVar].set(GRB_DoubleAttr_UB, 0.0); // Solve from this starting point model.optimize(); // Save iteration & time info double warmCount = model.get(GRB_DoubleAttr_IterCount); double warmTime = model.get(GRB_DoubleAttr_Runtime); // Reset the model and resolve cout << "\n*** Resetting and solving " << "without an advanced start ***\n" << endl; model.reset(); model.optimize(); // Save iteration & time info double coldCount = model.get(GRB_DoubleAttr_IterCount); double coldTime = model.get(GRB_DoubleAttr_Runtime); cout << "\n*** Warm start: " << warmCount << " iterations, " << warmTime << " seconds" << endl; cout << "*** Cold start: " << coldCount << " iterations, " << coldTime << " seconds" << endl; } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } delete[] v; delete env; return 0; }
std::map<std::string,int> ILP(char* argv) { map<string, int > results; GRBVar* vars = 0; // if (argc < 2) { // cout << "Usage: lp_c++ filename" << endl; // return 1; // } try { GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env, argv); //model.getEnv().set(GRB_DoubleParam_IntFeasTol,1e-6); model.getEnv().set(GRB_DoubleParam_Heuristics,0.95); model.getEnv().set(GRB_DoubleParam_TimeLimit,timeMax); model.getEnv().set(GRB_DoubleParam_MIPGap, ilpGap); vars = model.getVars(); model.optimize(); int optimstatus = model.get(GRB_IntAttr_Status); if (optimstatus == GRB_INF_OR_UNBD) { model.getEnv().set(GRB_IntParam_Presolve, 0); model.optimize(); optimstatus = model.get(GRB_IntAttr_Status); } if (optimstatus == GRB_OPTIMAL) { double objval = model.get(GRB_DoubleAttr_ObjVal); cout << "Optimal objective: " << objval << endl; for(int i =0; i<model.get(GRB_IntAttr_NumVars);i++){ string varName = vars[i].get(GRB_StringAttr_VarName); results[varName] = vars[i].get(GRB_DoubleAttr_X); // cout << vars[i].get(GRB_StringAttr_VarName) << " " << vars[i].get(GRB_DoubleAttr_X) << endl; } ofstream varNames; ofstream varResults; varNames.open("varName.txt"); varResults.open("varResults.txt"); for(int i =0; i<model.get(GRB_IntAttr_NumVars);i++){ string varName = vars[i].get(GRB_StringAttr_VarName); results[varName] = vars[i].get(GRB_DoubleAttr_X)+0.5; // +0.5 to make sure its round if(varName == "co2o3storagey3" ){ cout << "gotte ya" <<endl; } varNames << varName << "\n"; varResults << varName << " "<< results[varName] << "\n"; //cout << vars[i].get(GRB_StringAttr_VarName) << " " << vars[i].get(GRB_DoubleAttr_X) << endl; } varNames.close(); varResults.close(); cout << "size of results is " << sizeof(results)*results.size()<< endl; cout << "using mapSize function, size is " << mapSize(results)<<endl; toFile(results,mapSize(results)); } else if (optimstatus == GRB_INFEASIBLE) { cout << "Model is infeasible" << endl; // compute and write out IIS //model.computeIIS(); //model.write("model.ilp"); } else if (optimstatus == GRB_UNBOUNDED) { cout << "Model is unbounded" << endl; } else { cout << "Optimization was stopped with status = " << optimstatus << endl; double objval = model.get(GRB_DoubleAttr_ObjVal); cout << "Optimal objective: " << objval << endl; for(int i =0; i<model.get(GRB_IntAttr_NumVars);i++){ string varName = vars[i].get(GRB_StringAttr_VarName); results[varName] = vars[i].get(GRB_DoubleAttr_X); // cout << vars[i].get(GRB_StringAttr_VarName) << " " << vars[i].get(GRB_DoubleAttr_X) << endl; } ofstream varNames; ofstream varResults; varNames.open("varName.txt"); varResults.open("varResults.txt"); for(int i =0; i<model.get(GRB_IntAttr_NumVars);i++){ string varName = vars[i].get(GRB_StringAttr_VarName); results[varName] = vars[i].get(GRB_DoubleAttr_X)+0.5; // +0.5 to make sure its round if(varName == "co2o3storagey3" ){ cout << "gotte ya" <<endl; } varNames << varName << "\n"; varResults << varName << " = "<< results[varName] << "\n"; //cout << vars[i].get(GRB_StringAttr_VarName) << " " << vars[i].get(GRB_DoubleAttr_X) << endl; } varNames.close(); varResults.close(); } } catch(GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } return results; //return 0; }