过滤内容,
版本
文本搜索
Bilinear.java
/ * 2023年版权,Gurobi优狗万app足彩化,LLC * / / *本例制定和解决以下简单的双线性模型:最大化受到x + y + z < = 10 x * y < = 2(双线性不等式)x * z + y * z = = 1(双线性平等)x, y, z非负(x积分在第二版)* /进口Gurobi。*;公共类双线性{公共静态void main (String [] args){尝试{GRBEnv env = new GRBEnv (“bilinear.log”);GRBModel模型= new GRBModel (env);/ /创建变量GRBVar x = model.addVar(0.0,伽马线暴。无穷,0.0,伽马线暴。连续的," x ");GRBVar y = model.addVar(0.0,伽马线暴。无穷,0.0,伽马线暴。连续的," y ");GRBVar z = model.addVar(0.0,伽马线暴。无穷,0.0,伽马线暴。连续的,“z”);/ /设置目标GRBLinExpr obj = new GRBLinExpr ();obj.addTerm (1.0 x);模型。setObjective (obj GRB.MAXIMIZE); // Add linear constraint: x + y + z <= 10 GRBLinExpr expr = new GRBLinExpr(); expr.addTerm(1.0, x); expr.addTerm(1.0, y); expr.addTerm(1.0, z); model.addConstr(expr, GRB.LESS_EQUAL, 10.0, "c0"); // Add bilinear inequality: x * y <= 2 GRBQuadExpr qexpr = new GRBQuadExpr(); qexpr.addTerm(1.0, x, y); model.addQConstr(qexpr, GRB.LESS_EQUAL, 2.0, "bilinear0"); // Add bilinear equality: x * z + y * z == 1 qexpr = new GRBQuadExpr(); qexpr.addTerm(1.0, x, z); qexpr.addTerm(1.0, y, z); model.addQConstr(qexpr, GRB.EQUAL, 1.0, "bilinear1"); // First optimize() call will fail - need to set NonConvex to 2 try { model.optimize(); assert false; } catch (GRBException e) { System.out.println("Failed (as expected)"); } // Change parameter and optimize again model.set(GRB.IntParam.NonConvex, 2); model.optimize(); System.out.println(x.get(GRB.StringAttr.VarName) + " " +x.get(GRB.DoubleAttr.X)); System.out.println(y.get(GRB.StringAttr.VarName) + " " +y.get(GRB.DoubleAttr.X)); System.out.println(z.get(GRB.StringAttr.VarName) + " " +z.get(GRB.DoubleAttr.X)); System.out.println("Obj: " + model.get(GRB.DoubleAttr.ObjVal) + " " + obj.getValue()); System.out.println(); // Constrain x to be integral and solve again x.set(GRB.CharAttr.VType, GRB.INTEGER); model.optimize(); System.out.println(x.get(GRB.StringAttr.VarName) + " " +x.get(GRB.DoubleAttr.X)); System.out.println(y.get(GRB.StringAttr.VarName) + " " +y.get(GRB.DoubleAttr.X)); System.out.println(z.get(GRB.StringAttr.VarName) + " " +z.get(GRB.DoubleAttr.X)); System.out.println("Obj: " + model.get(GRB.DoubleAttr.ObjVal) + " " + obj.getValue()); System.out.println(); // Dispose of model and environment model.dispose(); env.dispose(); } catch (GRBException e) { System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage()); } } }