sensitivity_cs.cs


//一个简单的灵敏度分析示例,从//文狗万app足彩件中读取MIP模型并求解它。然后利用场景特征分析每个二元变量设为// 1-X时目标函数的影响,其中X为其在最优解中的值。// //用法:// sensitivity_cs  using System;使用Gurobi;类敏感度_cs{//考虑公共场景的最大数量const int MAXSCENARIOS = 100;static void Main(string[] args) {const int maxscenes = sensitivity_cs. maxscenes;如果(arg游戏。长度< 1){Console.Out。WriteLine("Usage: sensitivity_cs filename");返回; } try { // Create environment GRBEnv env = new GRBEnv(); // Read model GRBModel model = new GRBModel(env, args[0]); int scenarios; if (model.IsMIP == 0) { Console.WriteLine("Model is not a MIP"); return; } // Solve model 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); scenarios = 0; // Count number of unfixed, binary variables in model. For each we // create a scenario. for (int i = 0; i < vars.Length; i++) { GRBVar v = vars[i]; char vType = v.VType; if (v.LB == 0.0 && v.UB == 1.0 && (vType == GRB.BINARY || vType == GRB.INTEGER) ) { scenarios++; if (scenarios >= maxscenarios) break; } } Console.WriteLine("### construct multi-scenario model with " + scenarios + " scenarios"); // Set the number of scenarios in the model */ model.NumScenarios = scenarios; scenarios = 0; // Create a (single) scenario model by iterating through unfixed // binary variables in the model and create for each of these // variables a scenario by fixing the variable to 1-X, where X is its // value in the computed optimal solution for (int i = 0; i < vars.Length; i++) { GRBVar v = vars[i]; char vType = v.VType; if (v.LB == 0.0 && v.UB == 1.0 && (vType == GRB.BINARY || vType == GRB.INTEGER) && scenarios < maxscenarios ) { // Set ScenarioNumber parameter to select the corresponding // scenario for adjustments model.Parameters.ScenarioNumber = scenarios; // Set variable to 1-X, where X is its value in the optimal solution */ if (origX[i] < 0.5) v.ScenNLB = 1.0; else v.ScenNUB = 0.0; scenarios++; } else { // Add MIP start for all other variables using the optimal solution // of the base model v.Start = origX[i]; } } // Solve multi-scenario model model.Optimize(); // In case we solved the scenario model to optimality capture the // sensitivity information if (model.Status == GRB.Status.OPTIMAL) { // get the model sense (minimization or maximization) int modelSense = model.ModelSense; scenarios = 0; for (int i = 0; i < vars.Length; i++) { GRBVar v = vars[i]; char vType = v.VType; if (v.LB == 0.0 && v.UB == 1.0 && (vType == GRB.BINARY || vType == GRB.INTEGER) ) { // Set scenario parameter to collect the objective value of the // corresponding scenario model.Parameters.ScenarioNumber = scenarios; double scenarioObjVal = model.ScenNObjVal; double scenarioObjBound = model.ScenNObjBound; Console.Write("Objective sensitivity for variable " + v.VarName + " is "); // Check if we found a feasible solution for this scenario if (modelSense * scenarioObjVal >= GRB.INFINITY) { // Check if the scenario is infeasible if (modelSense * scenarioObjBound >= GRB.INFINITY) Console.WriteLine("infeasible"); else Console.WriteLine("unknown (no solution available)"); } else { // Scenario is feasible and a solution is available Console.WriteLine(modelSense * (scenarioObjVal - origObjVal)); } scenarios++; if (scenarios >= maxscenarios) break; } } } // 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); } } }