Bilinear_c ++。CPP


Bilinear_c ++。CPP


/ *版权所有2021,Gurobi优狗万app足彩化,LLC * // *此示例制定并解决以下简单的双线性模型:最大化X受X + Y + Z <= 10 x * Y <= 2(双线性不等式)x * z +y * z == 1(双线性平等)x,y,z非负(x积分在第二版中)* / #include  #include“gurobi_c ++。h”使用命名空间std;int main(int argc,char * argv []){try {grbenv env = grbenv();GrbModel Model = GrbModel(ENV);//创建变量grbvar x = model.addvar(0.0,grb_infinity,0.0,grb_continuous,“x”);grbvar y = model.addvar(0.0,grb_infinity,0.0,grb_xcontinuous,“y”);grbvar z = model.addvar(0.0,grb_infinity,0.0,grb_continuous,“z”);//设置目标grblinexpr obj = x;Model.SetObjective(OBJ,GRB_MAXIMIZE);//添加线性约束:x + y + z <= 10 model.addconstr(x + y + z <= 10,“c0”); // Add bilinear inequality constraint: x * y <= 2 model.addQConstr(x*y <= 2, "bilinear0"); // Add bilinear equality constraint: y * z == 1 model.addQConstr(x*z + y*z == 1, "bilinear1"); // First optimize() call will fail - need to set NonConvex to 2 try { model.optimize(); assert(0); } catch (GRBException e) { cout << "Failed (as expected)" << endl; } model.set(GRB_IntParam_NonConvex, 2); 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; // Constrain x to be integral and solve again x.set(GRB_CharAttr_VType, GRB_INTEGER); 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; }