mip2_c++.cpp
/* Copyright 2009, Gurobi Optimization, Inc. */
/* This example reads a MIP model from a file,
solves it, creates the fixed model from it and
solves it */
#include "gurobi_c++.h"
#include <cmath>
using namespace std;
int
main(int argc,
char *argv[])
{
if (argc < 2) {
cout << "Usage: mip2_c++ filename" << endl;
return 1;
}
GRBEnv *env = 0;
GRBVar *vars = 0;
try {
env = new GRBEnv();
GRBModel model = GRBModel(*env, argv[1]);
model.optimize();
int optimstatus = model.get(GRB_IntAttr_Status);
cout << "Optimization complete" << endl;
double objval = 0;
if (optimstatus == GRB_OPTIMAL) {
objval = model.get(GRB_DoubleAttr_ObjVal);
cout << "Optimal objective: " << objval << endl;
} else if (optimstatus == GRB_INF_OR_UNBD) {
cout << "Model is infeasible or unbounded" << endl;
return 0;
} else if (optimstatus == GRB_INFEASIBLE) {
cout << "Model is infeasible" << endl;
return 0;
} else if (optimstatus == GRB_UNBOUNDED) {
cout << "Model is unbounded" << endl;
return 0;
} else {
cout << "Optimization was stopped with status = "
<< optimstatus << endl;
return 0;
}
GRBModel fixed = model.fixedModel();
fixed.getEnv().set(GRB_IntParam_Presolve, 0);
fixed.optimize();
int foptimstatus = fixed.get(GRB_IntAttr_Status);
if (foptimstatus != GRB_OPTIMAL) {
cerr << "Error: fixed model isn't optimal" << endl;
return 0;
}
double fobjval = fixed.get(GRB_DoubleAttr_ObjVal);
if (fabs(fobjval - objval) > 1.0e-6 * (1.0 + fabs(objval))) {
cerr << "Error: objective values are different" << endl;
return 0;
}
vars = fixed.getVars();
int numvars = fixed.get(GRB_IntAttr_NumVars);
for (int j = 0; j < numvars; j++) {
GRBVar v = vars[j];
cout << v.get(GRB_StringAttr_VarName) << " "
<< v.get(GRB_DoubleAttr_X) << endl;
}
} catch(GRBException e) {
cout << "Error code = " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
} catch (...) {
cout << "Error during optimization" << endl;
}
delete[] vars;
delete env;
return 0;
}
