sensitivity_vb.vb.


sensitivity_vb.vb.


一个简单的灵敏度分析示例,从“文件”狗万app足彩中读取MIP模型并解决它。然后使用场景特征分析每个二元变量的目标函数的影响,如果它被设置为“1-X”,其中X是它在最优解中的值。' '使用方法:' sensitivity_cs 导入系统导入Gurobi类sensitivity_vb共享子Main(args As String())如果args. 'Length < 1 Then Console.Out。如果Try '最大场景数被认为是昏暗的MAXSCENARIOS as Integer = 100 '创建环境昏暗的env as New GRBEnv() '读取模型昏暗模型作为新的GRBModel(env, args(0))昏暗场景作为整数If模型。IsMIP = 0然后是Console。WriteLine("Model is not a MIP")返回End If '解决模型模型。优化()如果模型。Status <> GRB.Status.OPTIMAL Then Console。WriteLine("Optimization ended with status " & _ model. status)返回End If '存储最优解决方案Dim origObjVal As Double = model。ObjVal Dim vars As GRBVar() = model.GetVars() Dim origX As Double() = model.Get(GRB.DoubleAttr.)。计数模型中不固定的二进制变量的数量。我们为每一个人创造一个场景。For i As Integer = 0 To vars。Length - 1 Dim v As GRBVar = vars(i) Dim vType As Char = v. vType If v. lb = 0.0 AndAlso _ v. ub = 1.0 AndAlso _ (vType = GRB)BINARY OrElse vType = GRB.INTEGER) Then scenarios += 1 If scenarios >= MAXSCENARIOS Then Exit For End If End If Next 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 i As Integer = 0 To vars.Length - 1 Dim v As GRBVar = vars(i) Dim vType As Char = v.VType If v.LB = 0.0 AndAlso _ v.UB = 1.0 AndAlso _ (vType = GRB.BINARY OrElse vType = GRB.INTEGER) AndAlso _ scenarios < MAXSCENARIOS Then ' 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 Then v.ScenNLB = 1.0 Else v.ScenNUB = 0.0 End If scenarios += 1 Else ' Add MIP start for all other variables using the optimal solution ' of the base model v.Start = origX(i) End If Next ' 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 Then Dim modelSense As Integer = model.ModelSense scenarios = 0 For i As Integer = 0 To vars.Length - 1 Dim v As GRBVar = vars(i) Dim vType As Char = v.VType If v.LB = 0.0 AndAlso _ v.UB = 1.0 AndAlso _ (vType = GRB.BINARY OrElse vType = GRB.INTEGER) Then ' Set scenario parameter to collect the objective value of the ' corresponding scenario model.Parameters.ScenarioNumber = scenarios ' Collect objective value and bound for the scenario Dim scenarioObjVal As Double = model.ScenNObjVal Dim scenarioObjBound As Double = model.ScenNObjBound Console.Write("Objective sensitivity for variable " _ & v.VarName & " is ") ' Check if we found a feasible solution for this scenario If scenarioObjVal >= modelSense * GRB.INFINITY Then ' Check if the scenario is infeasible If scenarioObjBound >= modelSense * GRB.INFINITY Then Console.WriteLine("infeasible") Else Console.WriteLine("unknown (no solution available)") End If Else ' Scenario is feasible and a solution is available Console.WriteLine(modelSense * (scenarioObjVal - origObjVal)) End If scenarios += 1 If scenarios >= MAXSCENARIOS Then Exit For End If End If Next End If ' Dispose of model and environment model.Dispose() env.Dispose() Catch e As GRBException Console.WriteLine("Error code: " + e.ErrorCode) Console.WriteLine(e.Message) Console.WriteLine(e.StackTrace) End Try End Sub End Class