我刚刚开始使用Pyomo,我有一个大问题。我想创建一个抽象模型,并使用AMPL数据格式来喂养它.这个问题是一个经典的运输问题。我需要找到成本的最优解。M的意思是如果在给定的源地和目的地之间不可能运输,则输入M的大成本。 我需要将其转换为AMPL数据。除此之外,我不知道如何创建这个抽象模型。这个表格和模型的代码如下图所示。另外看了这个问题后,我创建的数学模型如下。
我创建的数学模型][1]
[经典运输问题标签][2]
from __future__ import division
from pyomo.environ import *
model = AbstractModel()
model.I = Set()
model.J = Set()
model.a = Param(model.I)
model.b = Param(model.J)
model.cost = Param(model.I,model.J)
model.supply = Var(model.I,model.J)
def obj_expression(model):
return sum(model.supply[i,j] * model.cost[i,j] for i in model.I for j in model.J)
model.OBJ = Objective(rule=obj_expression)
def ax_constraint_rule_1(model, i):
return sum(model.supply[i,j] for j in model.J )<= model.a[i]
def ax_constraint_rule_2(model, j):
return sum(model.supply[i,j] for i in model.I )>= model.b[j]
model.AxbConstraint = Constraint(model.I, rule=ax_constraint_rule_1)
model.AxbConstraint_2 = Constraint(model.J, rule=ax_constraint_rule_2)
pyomo solve --solver=glpk test.py transportation_data.dat
model.pprint()
set I := D1 D2 D3 ;
set J := S1 S2 S3 ;
param cost :=
S1 D1 3
S1 D2 1
S2 D1 4
S2 D2 2
S2 D3 4
S3 D2 3
S3 D3 3
;
param b :=
D1 7
D2 3
D3 5 ;
param a:=
S1 5
S2 7
S3 3
;
有没有人帮我处理这段代码?真的需要帮助建立模型和AMPL数据构建。
还是谢谢你
File "E:/pycharm_project/test.py", line 28
pyomo solve --solver=glpk test.py transportation_data.dat
^
SyntaxError: invalid syntax```
[1]: https://i.stack.imgur.com/DoWXA.png
[2]: https://i.stack.imgur.com/Fwmjb.png
我想,你已经很接近了。 你有几件事情需要清理。
你不需要 model.m
和 model.n
我不知道你想做什么。
对于集合,I和J,只需将它们列为Set(),因为你在AMPL数据中为它们提供了值。 像这样。
model.I = Set()
model.J = Set()
在你的公式中,你把c[i,j]做了双索引 但在你的公式和数据中,c的索引只由... model.I
同样,在你的模型中,你只是对a[i]进行了单索引,但在你的数据和公式中,你对它进行了双索引。
在你的约束条件中,定义中应该只保留 "for each "部分的变量,而不是你要求和的变量。
把这个东西清理一下,给它一个旋转,如果还是坏了就回评论我。
编辑:还有几条.......
我建议用直观的方式来命名你的参数和集合,比如。
model.supply
, model.cost
等。 使得阅读& 故障排除变得更加容易。)
============
编辑#2:你的代码清理工作有了很大的改进。 还剩下几个清理项目。
在你的约束条件中 你只需要为等式的 "for each "部分传递变量 如果你要对其他变量进行求和的话 你正在做 model.I
这里的约束条件,所以这个是合适的。
def ax_constraint_rule_1(model, i): # note removal of j
return sum(model.supply[i,j] for j in model.J ) <= model.a[i]
请注意,这里的总和已经超过了 j
对于每个 i
所以我把你的for循环也改了。
把这个换成另一个约束。
你的 a, b, cost, supply
与你的数据名称不一致。 检查所有的数据。 在你的数据中。a
似是 cost[i, j]
您的成本数据也缺少一些值
在AMPL语法中: set
不是大写的。 如果你把它运行起来,它会吧嗒吧嗒地叫出这个错误。
这是我的切。
from pyomo.environ import *
model = AbstractModel()
# model.m = Param(within=NonNegativeIntegers)
# model.n = Param(within=NonNegativeIntegers)
model.S = Set() # Sources
model.D = Set() # Destinations
model.cost = Param(model.S, model.D) # cost from S->D
model.supply = Param(model.S) # supply at source S
model.demand = Param(model.D) # demad at destination D
model.x = Var(model.S, model.D, domain=NonNegativeReals)
### OBJECTIVE FUNCTION ###
# calculate total cost of decisions
def obj_expression(model):
return sum(model.x[s, d] * model.cost[s, d] for s in model.S for d in model.D)
model.OBJ = Objective(rule=obj_expression)
### CONSTRAINTS ###
# ensure that supply constraint is met for each source in model.S
def supply_constraint(model, s):
return sum(model.x[s, d] for d in model.D ) <= model.supply[s]
# ensure that demand constraint is met for each destination in model.D
def demand_constraint(model, d):
return sum(model.x[s, d] for s in model.S ) >= model.demand[d]
model.sup_constraint = Constraint(model.S, rule=supply_constraint)
model.dem_constraint = Constraint(model.D, rule=demand_constraint)
model.pprint()
数据文件
set D := D1 D2 D3 ;
set S := S1 S2 S3 ;
param cost :=
S1 D1 3
S1 D2 1
S1 D3 10
S2 D1 4
S2 D2 2
S2 D3 4
S3 D1 10
S3 D2 3
S3 D3 3
;
param demand :=
D1 7
D2 3
D3 5 ;
param supply :=
S1 5
S2 7
S3 3
;
产出:
% pyomo solve --solver=glpk transpo_model.py transpo.dat --summary
[ 0.00] Setting up Pyomo environment
[ 0.00] Applying Pyomo preprocessing actions
4 Set Declarations
D : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=False, Bounds=None
Not constructed
S : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=False, Bounds=None
Not constructed
cost_index : Dim=0, Dimen=2, Size=0, Domain=None, Ordered=False, Bounds=None
Virtual
x_index : Dim=0, Dimen=2, Size=0, Domain=None, Ordered=False, Bounds=None
Virtual
3 Param Declarations
cost : Size=0, Index=cost_index, Domain=Any, Default=None, Mutable=False
Not constructed
demand : Size=0, Index=D, Domain=Any, Default=None, Mutable=False
Not constructed
supply : Size=0, Index=S, Domain=Any, Default=None, Mutable=False
Not constructed
1 Var Declarations
x : Size=0, Index=x_index
Not constructed
1 Objective Declarations
OBJ : Size=0, Index=None, Active=True
Not constructed
2 Constraint Declarations
dem_constraint : Size=0, Index=D, Active=True
Not constructed
sup_constraint : Size=0, Index=S, Active=True
Not constructed
11 Declarations: S D cost_index cost supply demand x_index x OBJ sup_constraint dem_constraint
[ 0.29] Creating model
[ 0.32] Applying solver
[ 0.33] Processing results
Number of solutions: 1
Solution Information
Gap: 0.0
Status: feasible
Function Value: 46.0
Solver results file: results.json
==========================================================
Solution Summary
==========================================================
Model unknown
Variables:
x : Size=9, Index=x_index
Key : Lower : Value : Upper : Fixed : Stale : Domain
('S1', 'D1') : 0 : 5.0 : None : False : False : NonNegativeReals
('S1', 'D2') : 0 : 0.0 : None : False : False : NonNegativeReals
('S1', 'D3') : 0 : 0.0 : None : False : False : NonNegativeReals
('S2', 'D1') : 0 : 2.0 : None : False : False : NonNegativeReals
('S2', 'D2') : 0 : 3.0 : None : False : False : NonNegativeReals
('S2', 'D3') : 0 : 2.0 : None : False : False : NonNegativeReals
('S3', 'D1') : 0 : 0.0 : None : False : False : NonNegativeReals
('S3', 'D2') : 0 : 0.0 : None : False : False : NonNegativeReals
('S3', 'D3') : 0 : 3.0 : None : False : False : NonNegativeReals
Objectives:
OBJ : Size=1, Index=None, Active=True
Key : Active : Value
None : True : 46.0
Constraints:
sup_constraint : Size=3
Key : Lower : Body : Upper
S1 : None : 5.0 : 5.0
S2 : None : 7.0 : 7.0
S3 : None : 3.0 : 3.0
dem_constraint : Size=3
Key : Lower : Body : Upper
D1 : 7.0 : 7.0 : None
D2 : 3.0 : 3.0 : None
D3 : 5.0 : 5.0 : None
[ 0.33] Applying Pyomo postprocessing actions
[ 0.33] Pyomo Finished