我是 pyomo 的新手,我在向集合和参数声明数据时遇到了麻烦。我总是收到语法错误的消息

Im new in pyomo, and im having trouble on declarating data to the sets and params. I always get the message of syntaxerror

我不确定问题是否取决于我使用的版本,或者我是否没有导入某些东西,但是我得到这个例子的来源没有提到任何其他环境而不是 pyomo.environ 的环境(我正在工作在 python 3.6) 上。我下载了 coopr,因为我认为它很重要,但没有任何变化。我已经尝试制作一个 .dat 文件,但是当我 运行 求解器时,我得到了相同的错误消息。有什么建议么?我正在阅读 pyomo (2012) 的书,这是声明数据的最简单方法,所以我想学习它。另外,如果您知道 pyomo 课程(正式或非正式),请分享。非常感谢您的宝贵时间。

from pyomo.environ import*
from coopr.pyomo import*

model = AbstractModel()
# nodes in the networks
model.N = Set()
#network arcs
model.A = Set(within=model.N*model.N) #within=set used for validation, it is a sueprset off the set being declared

# model parameters

#source node
model.s = Param(within=model.N)
#sink node
model.t = Param(within=model.N)
#flow capacity limits
model.c = Param(model.A)

#the flow over each arc
model.f = Var(model.A, within=NonNegativeReals)

# Maximize the flow into the sink nodes
def total_rule(model):
    return sum(model.f[i,j] for (i,j) in model.A if j== value(model.t))
model.total = Objective(rule=total_rule, sense=maximize)

# Enforce an upper limit on the flow across each arc
def limit_rule(model, i, j):
    return model.f[i,j] <= model.c[i,j]
model.limit = Constraint(model.A, rule=limit_rule)

# Enforce flow through each node
def flow_rule(model, k):
    if k == value(model.s) or k == value(model.t):
        return Constraint.Skip
    inFlow = sum(model.f[i,j] for (i,j) in model.A if j==k)
    outFlow = sum(model.f[i,j] for (i,j) in model.A if i==k)
    return inFlow == outflow
model.flow = Constraint(model.N, rule=flow_rule)

set N := Zoo A B C D E Home;                                   <-----

  File "<ipython-input-6-1d6651500789>", line 1                <-----
    set: N := Zoo A B C D E Home;                              <------
           ^                                                   <------
SyntaxError: invalid syntax                                    <------

我会从具体的模型开始...只是因为它使您无需处理单独的数据文件,而且 Python 在模型的框架内制作集合和数据非常容易。您可以像这样开始您的模型:

from pyomo.environ import*
#from coopr.pyomo import*

model = ConcreteModel()   # AbstractModel()
# nodes in the networks
nodes = {'Zoo', 'A', 'B', 'C', 'D', 'E', 'Home'}
model.N = Set(initialize=nodes)

如果你走抽象路线,上面会显示你在 python 文件中有你的设置定义??该数据需要在单独的文件中。

https://pyomo.readthedocs.io/en/stable/pyomo_overview/simple_examples.html#a-simple-concrete-pyomo-model