manbet体育手机客户端


sensitivity_cs.cs


/* Copyright 2019, 狗万app足彩Gurobi Optimization, LLC */ /*一个简单的敏感性分析示例,从一个文件中读取一个MIP模型并解决它。然后将每个二元变量设为1-X,其中X为其在最优解中的值,并报告对目标函数值的影响。* /使用系统;使用Gurobi;class sensitivity_cs {static void Main(string[] args) {if (args. properties);长度< 1){Console.Out。WriteLine(“用法:sensitivity_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(); if (model.Status != GRB.Status.OPTIMAL) { Console.WriteLine("Optimization ended with status " + model.Status); return; } // Store the optimal solution double origObjVal = model.ObjVal; GRBVar[] vars = model.GetVars(); double[] origX = model.Get(GRB.DoubleAttr.X, vars); // Disable solver output for subsequent solves model.Parameters.OutputFlag = 0; // Iterate through unfixed, binary variables in model for (int i = 0; i < vars.Length; i++) { GRBVar v = vars[i]; char vType = v.VType; if (v.LB == 0 && v.UB == 1 && (vType == GRB.BINARY || vType == GRB.INTEGER)) { // Set variable to 1-X, where X is its value in optimal solution if (origX[i] < 0.5) { v.LB = 1.0; v.Start = 1.0; } else { v.UB = 0.0; v.Start = 0.0; } // Update MIP start for the other variables for (int j = 0; j < vars.Length; j++) { if (j != i) { vars[j].Start = origX[j]; } } // Solve for new value and capture sensitivity information model.Optimize(); if (model.Status == GRB.Status.OPTIMAL) { Console.WriteLine("Objective sensitivity for variable " + v.VarName + " is " + (model.ObjVal - origObjVal)); } else { Console.WriteLine("Objective sensitivity for variable " + v.VarName + " is infinite"); } // Restore the original variable bounds v.LB = 0.0; v.UB = 1.0; } } // Dispose of model and environment model.Dispose(); env.Dispose(); } catch (GRBException e) { Console.WriteLine("Error code: " + e.ErrorCode); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } } }