sensitivity_vb.vb


版权2023年,Gurobi优化,L狗万app足彩LC的一个简单的灵敏度分析示例,从文件和读取MIP模型解决了它。然后使用场景特性分析影响w.r.t.目标函数的每个二进制变量是否设置为“1 - X,其中X是其价值的最佳解决方案。“用法:“sensitivity_cs <模型文件名>进口系统进口Gurobi类sensitivity_vb共享子主要如果参数(参数为字符串())。< 1然后Console.Out长度。WriteLine(用法:sensitivity_vb文件名)返回如果尝试的最大数量的场景被认为是暗MAXSCENARIOS Integer = 100”新建环境昏暗env GRBEnv ()“阅读模式模糊模型新GRBModel (env, args(0))整数如果昏暗的场景模型。IsMIP = 0然后控制台。WriteLine(模型不是一个MIP)返回结束如果“解决模型model.Optimize()如果模型。< > GRB.Status地位。最优的控制台。WriteLine(“优化了状态”& _ model.Status)返回结束如果存储最优解模糊origObjVal双=模型。ObjVal昏暗的var GRBVar () = model.GetVars()暗origX () = model.Get (GRB.DoubleAttr两倍。X, var)场景= 0的数不固定的,二进制变量模型。对于每个我们创建一个场景。我作为整数var = 0。v - 1的长度作为GRBVar = var(我)暗vType Char = v。VType如果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 modelSense * scenarioObjVal >= GRB.INFINITY Then ' Check if the scenario is infeasible If modelSense * scenarioObjBound >= 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