sensitivity.py


# !/usr/bin/env python3.7 # 2023年版权,Gurobi优化狗万app足彩,LLC #一个简单的灵敏度分析示例,从文件中读取MIP模型#并解决它。然后使用场景特性来分析影响# w.r.t.每个二进制变量的目标函数,如果设置为# 1 - X,其中X是其价值的最佳解决方案。# #用法:#敏感度。py <模型文件名> #导入系统导入gurobipy从gurobipy gp进口伽马线暴#最大数量的场景被认为是maxScenarios = 100如果len (sys.argv) < 2:印刷(“用法:敏感性。py文件名”)sys.exit(0) #阅读模型模型= gp.read (sys.argv[1])如果模型。IsMIP = = 0:打印(模型不是一个MIP) sys.exit(0) #解决模型model.optimize()如果模型。地位! =伽马线暴。优:打印(的优化以地位% d ' % model.Status) sys.exit(0) #存储最优解origObjVal =模型。为v ObjVal model.getVars (): v。_origX = v。Xscenarios = 0 # Count number of unfixed, binary variables in model. For each we create a # scenario. for v in model.getVars(): if (v.LB == 0.0 and v.UB == 1.0 and v.VType in (GRB.BINARY, GRB.INTEGER)): scenarios += 1 if scenarios >= maxScenarios: break # Set the number of scenarios in the model model.NumScenarios = scenarios scenarios = 0 print('### construct multi-scenario model with %d scenarios' % scenarios) # 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 v in model.getVars(): if (v.LB == 0.0 and v.UB == 1.0 and v.VType in (GRB.BINARY, GRB.INTEGER) and scenarios < maxScenarios): # Set ScenarioNumber parameter to select the corresponding scenario # for adjustments model.Params.ScenarioNumber = scenarios # Set variable to 1-X, where X is its value in the optimal solution if v._origX < 0.5: v.ScenNLB = 1.0 else: v.ScenNUB = 0.0 scenarios += 1 else: # Add MIP start for all other variables using the optimal solution # of the base model v.Start = v._origX # Solve multi-scenario model model.optimize() # In case we solved the scenario model to optimality capture the # sensitivity information if model.Status == GRB.OPTIMAL: modelSense = model.ModelSense scenarios = 0 # Capture sensitivity information from each scenario for v in model.getVars(): if (v.LB == 0.0 and v.UB == 1.0 and v.VType in (GRB.BINARY, GRB.INTEGER)): # Set scenario parameter to collect the objective value of the # corresponding scenario model.Params.ScenarioNumber = scenarios # Collect objective value and bound for the scenario scenarioObjVal = model.ScenNObjVal scenarioObjBound = model.ScenNObjBound # 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: print('Objective sensitivity for variable %s is infeasible' % v.VarName) else: print('Objective sensitivity for variable %s is unknown (no solution available)' % v.VarName) else: # Scenario is feasible and a solution is available print('Objective sensitivity for variable %s is %g' % (v.VarName, modelSense * (scenarioObjVal - origObjVal))) scenarios += 1 if scenarios >= maxScenarios: break

尝试Gurobi免费

选择最适合你的评估许可,开始使用我们的专家团队提供技术指导和支持。

评估许可
得到一个免费的,功能齐全的许可证的Gurobi性能优化经验,支持,基准测试和调优服务我们提供我们产品的一部分。
学术许可
Gurobi支持教学和学术机构内使用的优化。我们提供免费的,功能齐全的Gurobi用于类的副本,并进行研究。
云试验

申请免费试用时间,所以你可以看到一个模型可以解决如何快速而方便地在云上。

<\/i>","library":"fa-solid"}}" data-widget_type="nav-menu.default">

Gurobi通讯