feasopt.java.


feasopt.java.


/ *版权所有2021,Gurobi优狗万app足彩化,LLC * / / *此示例从文件中读取MIP模型,将人工变量添加到每个约束,然后最小化人工变量的总和。具有目标零的解决方案对应于输入模型的可行解决方案。我们还可以使用Feasrelax功能来进行。在此示例中,我们使用Minrelax = 1,即优化返回的模型找到最小化原始目标的解决方案,但只能从最小化人工变量的总和中的那些解决方案中最小化。* /进口gurobi。*;公共类feasopt {公共静态void main(string [] args){if(args.length <1){system.out.println(“用法:java feasopt filename”);system.exit(1);试试{grbenv env = new grbenv();grbmodel feasmodel =新grbmodel(env,args [0]);//创建副本以使用feasrelax功能稍后* / grbmodel feasmodel1 =新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()); } } }