例子mip1_c + + . cpp

例子mip1_c + + . cpp

这是我们示例的完整源代码(也可在< installdir > /例子/ c++ / mip1_c + + . cpp)…


/ * 2020年版权,Gurobi优狗万app足彩化,LLC * / / *本例制定和解决以下简单的MIP模型:最大化x + y + 2 z受到x + 2 y + 3 z < = 4 x + y > = 1 x, y, z二进制* / # include使用名称空间性病“gurobi_c + + . h”;int main(int argc, char *argv[]) {try{//创建一个环境env。设置(“记录文件”、“mip1.log”);env.start ();//创建一个空模型GRBModel model = GRBModel(env);//创建变量GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "x");GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "y");GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "z");//设定目标:最大化x + y + 2z模型。setObjective(x + y + 2 * z, grb_maximum); // Add constraint: x + 2 y + 3 z <= 4 model.addConstr(x + 2 * y + 3 * z <= 4, "c0"); // Add constraint: x + y >= 1 model.addConstr(x + y >= 1, "c1"); // Optimize model model.optimize(); cout << x.get(GRB_StringAttr_VarName) << " " << x.get(GRB_DoubleAttr_X) << endl; cout << y.get(GRB_StringAttr_VarName) << " " << y.get(GRB_DoubleAttr_X) << endl; cout << z.get(GRB_StringAttr_VarName) << " " << z.get(GRB_DoubleAttr_X) << endl; cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl; } catch(GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch(...) { cout << "Exception during optimization" << endl; } return 0; }