manbet体育手机客户端


Feasopt.java


*/ /*这个例子从一个文件中读取一个狗万app足彩MIP模型,为每个约束添加人工变量,然后最小化人工变量的总和。目标为零的解对应于输入模型的可行解。我们也可以使用FeasRelax功能来实现。在这个例子中,我们使用minrelax=1,即优化返回的模型找到一个使原始目标最小化的解决方案,但只从那些使人工变量之和最小化的解决方案中。* /进口gurobi。*;public class Feasopt {public static void main(String[] args){如果(args.)length < 1) {System.out. length = 1;println(“使用:java Feasopt文件名”);system . exit (1);} try {GRBEnv env = new GRBEnv();= 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()); } } }