sensitivity_c + + . cpp


/ /版权2023年Gurobi优化,狗万app足彩LLC / /一个简单的灵敏度分析示例,从/ /读取MIP模型文件并解决它。然后使用场景的特性来分析目标函数的影响/ / w.r.t.每个二进制变量是否设置为/ / 1 - X,其中X是其价值的最佳解决方案。/ / / /用法:/ / sensitivity_c + + <模型文件名> # include“gurobi_c + +。使用名称空间std h”;/ /最大数量的场景需要考虑100 # define MAXSCENARIOS int主要(int命令行参数个数,char * argv[]){如果(命令行参数个数< 2){cout < <“用法:sensitivity_c + +文件名”< < endl;返回1;}GRBVar * var =零;双* origX =零;尝试{/ /创建环境GRBEnv env = GRBEnv ();/ /读取模型GRBModel模型= GRBModel (env, argv [1]);int场景; if (model.get(GRB_IntAttr_IsMIP) == 0) { cout << "Model is not a MIP" << endl; return 1; } // Solve model model.optimize(); if (model.get(GRB_IntAttr_Status) != GRB_OPTIMAL) { cout << "Optimization ended with status " << model.get(GRB_IntAttr_Status) << endl; return 1; } // Store the optimal solution double origObjVal = model.get(GRB_DoubleAttr_ObjVal); vars = model.getVars(); int numVars = model.get(GRB_IntAttr_NumVars); origX = model.get(GRB_DoubleAttr_X, vars, numVars); scenarios = 0; // Count number of unfixed, binary variables in model. For each we // create a scenario. for (int i = 0; i < numVars; i++) { GRBVar v = vars[i]; char vType = v.get(GRB_CharAttr_VType); if (v.get(GRB_DoubleAttr_LB) == 0.0 && v.get(GRB_DoubleAttr_UB) == 1.0 && (vType == GRB_BINARY || vType == GRB_INTEGER) ) { scenarios++; if (scenarios >= MAXSCENARIOS) break; } } cout << "### construct multi-scenario model with " << scenarios << " scenarios" << endl; // Set the number of scenarios in the model */ model.set(GRB_IntAttr_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 < numVars; i++) { GRBVar v = vars[i]; char vType = v.get(GRB_CharAttr_VType); if (v.get(GRB_DoubleAttr_LB) == 0.0 && v.get(GRB_DoubleAttr_UB) == 1-0 && (vType == GRB_BINARY || vType == GRB_INTEGER) && scenarios < MAXSCENARIOS ) { // Set ScenarioNumber parameter to select the corresponding // scenario for adjustments model.set(GRB_IntParam_ScenarioNumber, scenarios); // Set variable to 1-X, where X is its value in the optimal solution */ if (origX[i] < 0.5) v.set(GRB_DoubleAttr_ScenNLB, 1.0); else v.set(GRB_DoubleAttr_ScenNUB, 0.0); scenarios++; } else { // Add MIP start for all other variables using the optimal solution // of the base model v.set(GRB_DoubleAttr_Start, origX[i]); } } // Solve multi-scenario model model.optimize(); // In case we solved the scenario model to optimality capture the // sensitivity information if (model.get(GRB_IntAttr_Status) == GRB_OPTIMAL) { // get the model sense (minimization or maximization) int modelSense = model.get(GRB_IntAttr_ModelSense); scenarios = 0; for (int i = 0; i < numVars; i++) { GRBVar v = vars[i]; char vType = v.get(GRB_CharAttr_VType); if (v.get(GRB_DoubleAttr_LB) == 0.0 && v.get(GRB_DoubleAttr_UB) == 1-0 && (vType == GRB_BINARY || vType == GRB_INTEGER) ) { // Set scenario parameter to collect the objective value of the // corresponding scenario model.set(GRB_IntParam_ScenarioNumber, scenarios); // Collect objective value and bound for the scenario double scenarioObjVal = model.get(GRB_DoubleAttr_ScenNObjVal); double scenarioObjBound = model.get(GRB_DoubleAttr_ScenNObjBound); cout << "Objective sensitivity for variable " << v.get(GRB_StringAttr_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) cout << "infeasible" << endl; else cout << "unknown (no solution available)" << endl; } else { // Scenario is feasible and a solution is available cout << modelSense * (scenarioObjVal - origObjVal) << endl; } scenarios++; if (scenarios >= MAXSCENARIOS) break; } } } } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } delete[] vars; delete[] origX; return 0; }