lpmod_cs.cs


lpmod_cs.cs


/* Copyright 2021, 狗万app足彩Gurobi Optimization, LLC */ /*这个例子从一个文件中读取一个LP模型并解决它。如果模型可以求解,则找到最小的正变量,并将其上界设为0,然后采用两种方式求解模型:先提前开始,后不提前开始(即。“从头开始”)。* /使用系统;使用Gurobi;类lpmod_cs {static void Main(string[] args) {if (args. class);长度< 1){Console.Out。WriteLine(“用法:lpmod_cs文件名”);返回;} try{//读取模型,确定是否为LP GRBEnv env = new GRBEnv();/ /创建GRBModel = env, args, [0]; if (model.IsMIP != 0) { Console.WriteLine("The model is not a linear program"); Environment.Exit(1); } model.Optimize(); int status = model.Status; if ((status == GRB.Status.INF_OR_UNBD) || (status == GRB.Status.INFEASIBLE) || (status == GRB.Status.UNBOUNDED)) { Console.WriteLine("The model cannot be solved because it is " + "infeasible or unbounded"); Environment.Exit(1); } if (status != GRB.Status.OPTIMAL) { Console.WriteLine("Optimization was stopped with status " + status); Environment.Exit(0); } // Find the smallest variable value double minVal = GRB.INFINITY; GRBVar minVar = null; foreach (GRBVar v in model.GetVars()) { double sol = v.X; if ((sol > 0.0001) && (sol < minVal) && (v.LB == 0.0)) { minVal = sol; minVar = v; } } Console.WriteLine("\n*** Setting " + minVar.VarName + " from " + minVal + " to zero ***\n"); minVar.UB = 0.0; // Solve from this starting point model.Optimize(); // Save iteration & time info double warmCount = model.IterCount; double warmTime = model.Runtime; // Reset the model and resolve Console.WriteLine("\n*** Resetting and solving " + "without an advanced start ***\n"); model.Reset(); model.Optimize(); double coldCount = model.IterCount; double coldTime = model.Runtime; Console.WriteLine("\n*** Warm start: " + warmCount + " iterations, " + warmTime + " seconds"); Console.WriteLine("*** Cold start: " + coldCount + " iterations, " + coldTime + " seconds"); // Dispose of model and env model.Dispose(); env.Dispose(); } catch (GRBException e) { Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message); } } }