提问者:小点点

Pyomo:创建抽象模型和AMPL数据


我只是从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

共2个答案

匿名用户

嗯,我想你很接近了。你有几件事要清理。

你不需要模式. m模式. n我不确定你在那里尝试做什么。

对于集合I和J,只需将它们列为Set(),因为您正在AMPL数据中为它们提供值。比如:

model.I = Set()     
model.J = Set()     

在你的公式中,你是双重索引c[i,j],但在你的公式和数据中,c只被模型索引。我

类似地,在你的模型中,你只对一个[i]进行了单一索引,但是你在你的数据和公式中对它进行了双重索引。

在约束中,定义应该只包含“for each”部分的变量,而不是要求和的变量。

把那些东西清理干净,让它旋转一下,如果它仍然坏了,请给我回复。

编辑:还有几项。。。。。

我建议直观地命名参数和集合,例如:

模型。提供型号。成本等使其更易于阅读

============

编辑#2:您的代码清理得到了极大的改进。几个清理项目仍然存在:

在约束中,如果要对另一个变量求和,则只需为方程的“for each”一侧传递变量。您正在制作模型。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]

注意这里的求和对于每个i都超过了j,所以我也改变了for循环。

翻转另一个约束。

您的a、b、成本、供应与您的数据名称不一致。全部检查一下。在您的数据中,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