过滤内容
版本
文本搜索
qcp_c + + . cpp
/*版权2023,Gurobi Opt狗万app足彩imization, LLC */ /*这个例子提出并解决了以下简单的QCP模型:最大化x服从x + y + z = 1 x^2 + y^2 <= z^2(二阶锥)x^2 <= yz(旋转的二阶锥)x, y, z非负*/ #include "gurobi_c++.h"使用命名空间std;int main(int argc, char *argv[]) {try {GRBEnv env = GRBEnv();GRBModel模型= 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_CONTINUOUS, "y");GRBVar z = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "z");//设置目标GRBLinExpr obj = x;模型。setObjective (obj GRB_MAXIMIZE);//添加线性约束:x + y + z = 1模型。addConstr(x + y + z == 1, "c0"); // Add second-order cone: x^2 + y^2 <= z^2 model.addQConstr(x*x + y*y <= z*z, "qc0"); // Add rotated cone: x^2 <= yz model.addQConstr(x*x <= y*z, "qc1"); // 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; }