如何显示 class 型 Pyomo 模型的最优变量值?
How to display optimal variable values of a class-type Pyomo model?
我是新 Pyomo/Python 用户,我只是想知道如何在 class 类型的 Pyomo 模型中显示最佳变量值。
我刚刚尝试了 Pyomo 示例库中的标准示例 "min-cost-flow model"。该代码在 https://github.com/Pyomo/PyomoGallery/wiki/Min-cost-flow
中可用
在代码的底部,它说:
sp = MinCostFlow('nodes.csv', 'arcs.csv')
sp.solve()
print('\n\n---------------------------')
print('Cost: ', sp.m.OBJ())
输出为
Academic license - for non-commercial use only
Read LP format model from file.
Reading time = 0.00 seconds
x8: 7 rows, 8 columns, 16 nonzeros
No parameters matching 'mip_tolerances_integrality' found
No parameters matching 'mip_tolerances_mipgap' found
Optimize a model with 7 rows, 8 columns and 16 nonzeros
Coefficient statistics:
Matrix range [1e+00, 1e+00]
Objective range [1e+00, 5e+00]
Bounds range [0e+00, 0e+00]
RHS range [1e+00, 1e+00]
Presolve removed 7 rows and 8 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Iteration Objective Primal Inf. Dual Inf. Time
0 5.0000000e+00 0.000000e+00 0.000000e+00 0s
Solved in 0 iterations and 0.01 seconds
Optimal objective 5.000000000e+00
我只能得到最优的objective,但是最优的变量值呢?我还搜索了文档,它告诉我使用类似的东西:
print("x[2]=",pyo.value(model.x[2])).
但它不适用于 class 类型的模型,例如最小成本流模型。
我也试过修改class中的函数定义:
def solve(self):
"""Solve the model."""
solver = pyomo.opt.SolverFactory('gurobi')
results = solver.solve(self.m, tee=True, keepfiles=False, options_string="mip_tolerances_integrality=1e-9, mip_tolerances_mipgap=0")
print('\n\n---------------------------')
print('First Variable: ', self.m.Y[0])
但效果不佳。输出是:
KeyError: "Index '0' is not valid for indexed component 'Y'"
你能帮我解决这个问题吗?谢谢!
加布里埃尔
求解后显示模型结果的最直接的方法是使用model.display()
函数。在你的情况下,self.m.display()
.
display()
函数也适用于 Var
对象,所以如果你有一个变量 self.m.x
,你可以 self.m.x.display()
.
我是新 Pyomo/Python 用户,我只是想知道如何在 class 类型的 Pyomo 模型中显示最佳变量值。
我刚刚尝试了 Pyomo 示例库中的标准示例 "min-cost-flow model"。该代码在 https://github.com/Pyomo/PyomoGallery/wiki/Min-cost-flow
中可用在代码的底部,它说:
sp = MinCostFlow('nodes.csv', 'arcs.csv')
sp.solve()
print('\n\n---------------------------')
print('Cost: ', sp.m.OBJ())
输出为
Academic license - for non-commercial use only
Read LP format model from file.
Reading time = 0.00 seconds
x8: 7 rows, 8 columns, 16 nonzeros
No parameters matching 'mip_tolerances_integrality' found
No parameters matching 'mip_tolerances_mipgap' found
Optimize a model with 7 rows, 8 columns and 16 nonzeros
Coefficient statistics:
Matrix range [1e+00, 1e+00]
Objective range [1e+00, 5e+00]
Bounds range [0e+00, 0e+00]
RHS range [1e+00, 1e+00]
Presolve removed 7 rows and 8 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Iteration Objective Primal Inf. Dual Inf. Time
0 5.0000000e+00 0.000000e+00 0.000000e+00 0s
Solved in 0 iterations and 0.01 seconds
Optimal objective 5.000000000e+00
我只能得到最优的objective,但是最优的变量值呢?我还搜索了文档,它告诉我使用类似的东西:
print("x[2]=",pyo.value(model.x[2])).
但它不适用于 class 类型的模型,例如最小成本流模型。
我也试过修改class中的函数定义:
def solve(self):
"""Solve the model."""
solver = pyomo.opt.SolverFactory('gurobi')
results = solver.solve(self.m, tee=True, keepfiles=False, options_string="mip_tolerances_integrality=1e-9, mip_tolerances_mipgap=0")
print('\n\n---------------------------')
print('First Variable: ', self.m.Y[0])
但效果不佳。输出是:
KeyError: "Index '0' is not valid for indexed component 'Y'"
你能帮我解决这个问题吗?谢谢!
加布里埃尔
求解后显示模型结果的最直接的方法是使用model.display()
函数。在你的情况下,self.m.display()
.
display()
函数也适用于 Var
对象,所以如果你有一个变量 self.m.x
,你可以 self.m.x.display()
.