Feasopt.java


/ * 2023年版权,Gurobi优狗万app足彩化,LLC * / / *这个例子从文件读取MIP模型,增加了每个约束的人工变量,然后最小化人工变量的总和。解决方案与目标零输入模型对应于一个可行的解决方案。我们也可以使用FeasRelax特性。在这个例子中,我们使用minrelax = 1,即优化模型找到了一个解决方案,最大限度地减少返回原来的目标,但只从那些解决方案,最大限度地减少人工变量的总和。* /进口gurobi。*;公开课Feasopt{公共静态空main (String [] args){如果(args)。长度< 1){system . out。println(“用法:java Feasopt文件名”);system . exit (1);}{尝试GRBEnv env = new GRBEnv ();GRBModel feasmodel = new GRBModel (env, args [0]); // Create a copy to use FeasRelax feature later */ GRBModel feasmodel1 = new GRBModel(feasmodel); // Clear objective feasmodel.setObjective(new GRBLinExpr()); // Add slack variables GRBConstr[] c = feasmodel.getConstrs(); for (int i = 0; i < c.length; ++i) { char sense = c[i].get(GRB.CharAttr.Sense); if (sense != '>') { GRBConstr[] constrs = new GRBConstr[] { c[i] }; double[] coeffs = new double[] { -1 }; feasmodel.addVar(0.0, GRB.INFINITY, 1.0, GRB.CONTINUOUS, constrs, coeffs, "ArtN_" + c[i].get(GRB.StringAttr.ConstrName)); } if (sense != '<') { GRBConstr[] constrs = new GRBConstr[] { c[i] }; double[] coeffs = new double[] { 1 }; feasmodel.addVar(0.0, GRB.INFINITY, 1.0, GRB.CONTINUOUS, constrs, coeffs, "ArtP_" + c[i].get(GRB.StringAttr.ConstrName)); } } // Optimize modified model feasmodel.optimize(); feasmodel.write("feasopt.lp"); // use FeasRelax feature */ feasmodel1.feasRelax(GRB.FEASRELAX_LINEAR, true, false, true); feasmodel1.write("feasopt1.lp"); feasmodel1.optimize(); // Dispose of model and environment feasmodel1.dispose(); feasmodel.dispose(); env.dispose(); } catch (GRBException e) { System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage()); } } }