manbet体育手机客户端


mip2_cs.cs


/* Copyright 2019, 狗万app足彩Gurobi Optimization, LLC */ /*本例从一个文件中读取一个MIP模型,求解它,并从求解MIP时生成的所有可行解决方案中打印出客观值。然后创建固定模型并解决该模型。* /使用系统;使用Gurobi;class mip2_cs {static void Main(string[] args) {if (args. php . php);长度< 1){Console.Out。WriteLine(“用法:mip2_cs文件名”);返回;} try {GRBEnv env = new GRBEnv();GRBModel model = new GRBModel(env, args[0]);如果模型。IsMIP == 0) { Console.WriteLine("Model is not a MIP"); return; } model.Optimize(); int optimstatus = model.Status; double objval = 0; if (optimstatus == GRB.Status.OPTIMAL) { objval = model.ObjVal; Console.WriteLine("Optimal objective: " + objval); } else if (optimstatus == GRB.Status.INF_OR_UNBD) { Console.WriteLine("Model is infeasible or unbounded"); return; } else if (optimstatus == GRB.Status.INFEASIBLE) { Console.WriteLine("Model is infeasible"); return; } else if (optimstatus == GRB.Status.UNBOUNDED) { Console.WriteLine("Model is unbounded"); return; } else { Console.WriteLine("Optimization was stopped with status = " + optimstatus); return; } /* Iterate over the solutions and compute the objectives */ GRBVar[] vars = model.GetVars(); model.Parameters.OutputFlag = 0; Console.WriteLine(); for (int k = 0; k < model.SolCount; ++k) { model.Parameters.SolutionNumber = k; double objn = 0.0; for (int j = 0; j < vars.Length; j++) { objn += vars[j].Obj * vars[j].Xn; } Console.WriteLine("Solution " + k + " has objective: " + objn); } Console.WriteLine(); model.Parameters.OutputFlag = 1; /* Create a fixed model, turn off presolve and solve */ GRBModel fixedmodel = model.FixedModel(); fixedmodel.Parameters.Presolve = 0; fixedmodel.Optimize(); int foptimstatus = fixedmodel.Status; if (foptimstatus != GRB.Status.OPTIMAL) { Console.WriteLine("Error: fixed model isn't optimal"); return; } double fobjval = fixedmodel.ObjVal; if (Math.Abs(fobjval - objval) > 1.0e-6 * (1.0 + Math.Abs(objval))) { Console.WriteLine("Error: objective values are different"); return; } GRBVar[] fvars = fixedmodel.GetVars(); double[] x = fixedmodel.Get(GRB.DoubleAttr.X, fvars); string[] vnames = fixedmodel.Get(GRB.StringAttr.VarName, fvars); for (int j = 0; j < fvars.Length; j++) { if (x[j] != 0.0) Console.WriteLine(vnames[j] + " " + x[j]); } // Dispose of models and env fixedmodel.Dispose(); model.Dispose(); env.Dispose(); } catch (GRBException e) { Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message); } } }