mip2_cs.cs


/ * 2023年版权,Gurobi优狗万app足彩化,LLC * / / *这个例子从文件读取MIP模型,解决了它并打印客观值生成而解决MIP从所有可行的解决方案。然后它创建固定模型和解决模型。* /使用系统;使用Gurobi;类mip2_cs{静态void Main (string [] args){如果(args)。< 1){Console.Out长度。WriteLine(“用法:mip2_cs文件名”);返回;}{尝试GRBEnv env = new GRBEnv ();GRBModel模型= 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 */ model.Parameters.OutputFlag = 0; Console.WriteLine(); for (int k = 0; k < model.SolCount; ++k) { model.Parameters.SolutionNumber = k; double objn = model.PoolObjVal; 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); } } }