// Copyright 2025, Gurobi Optimization, LLC// A simple sensitivity analysis example which reads a MIP model from a// file and solves it. Then uses the scenario feature to analyze the impact// w.r.t. the objective function of each binary variable if it is set to// 1-X, where X is its value in the optimal solution.//// Usage:// sensitivity_cs <model filename>usingSystem;usingGurobi;classsensitivity_cs{// Maximum number of scenarios to be consideredpublicconstintMAXSCENARIOS=100;staticvoidMain(string[]args){constintmaxscenarios=sensitivity_cs.MAXSCENARIOS;if(args.Length<1){Console.Out.WriteLine("Usage: sensitivity_cs filename");return;}try{// Create environmentGRBEnvenv=newGRBEnv();// Read modelGRBModelmodel=newGRBModel(env,args[0]);intscenarios;if(model.IsMIP==0){Console.WriteLine("Model is not a MIP");return;}// Solve modelmodel.Optimize();if(model.Status!=GRB.Status.OPTIMAL){Console.WriteLine("Optimization ended with status "+model.Status);return;}// Store the optimal solutiondoubleorigObjVal=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(inti=0;i<vars.Length;i++){GRBVarv=vars[i];charvType=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 solutionfor(inti=0;i<vars.Length;i++){GRBVarv=vars[i];charvType=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 adjustmentsmodel.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;elsev.ScenNUB=0.0;scenarios++;}else{// Add MIP start for all other variables using the optimal solution// of the base modelv.Start=origX[i];}}// Solve multi-scenario modelmodel.Optimize();// In case we solved the scenario model to optimality capture the// sensitivity informationif(model.Status==GRB.Status.OPTIMAL){// get the model sense (minimization or maximization)intmodelSense=model.ModelSense;scenarios=0;for(inti=0;i<vars.Length;i++){GRBVarv=vars[i];charvType=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 scenariomodel.Parameters.ScenarioNumber=scenarios;doublescenarioObjVal=model.ScenNObjVal;doublescenarioObjBound=model.ScenNObjBound;Console.Write("Objective sensitivity for variable "+v.VarName+" is ");// Check if we found a feasible solution for this scenarioif(modelSense*scenarioObjVal>=GRB.INFINITY){// Check if the scenario is infeasibleif(modelSense*scenarioObjBound>=GRB.INFINITY)Console.WriteLine("infeasible");elseConsole.WriteLine("unknown (no solution available)");}else{// Scenario is feasible and a solution is availableConsole.WriteLine(modelSense*(scenarioObjVal-origObjVal));}scenarios++;if(scenarios>=maxscenarios)break;}}}// Dispose of model and environmentmodel.Dispose();env.Dispose();}catch(GRBExceptione){Console.WriteLine("Error code: "+e.ErrorCode);Console.WriteLine(e.Message);Console.WriteLine(e.StackTrace);}}}
Help and Feedback