manbet体育手机客户端


feasopt_cs.cs


*/ /*这个例子从一个文件中读取一个狗万app足彩MIP模型,为每个约束添加人工变量,然后最小化人工变量的和。具有目标零点的解对应于输入模型的一个可行解。我们也可以使用FeasRelax功能来实现这一点。在这个例子中,我们使用minrelax=1,即优化返回的模型,找到一个最小化原始目标的解决方案,但只能从那些最小化人工变量总和的解决方案中找到。使用Gurobi * /;使用系统;类feasopt_cs {static void Main(string[]参数){if(参数。长度< 1){Console.Out。WriteLine(“用法:feasopt_cs文件名”);返回;} try {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].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].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].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 env feasmodel1.Dispose(); feasmodel.Dispose(); env.Dispose(); } catch (GRBException e) { Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message); } } }