加载模型时出现意外异常:(0, 0)
Unexpected exception while loading model: (0, 0)
我刚开始使用 pyomo 框架和 python。我正在尝试解决简单的模型。当我制定 objective 函数时,在 运行 代码
之前收到以下错误
按键错误:(0, 0)
问题的表述如下:
Formulation
代码:
from pyomo.environ import *
N = 3
M = 4
P = 3
d = {(1, 1): 1.7, (1, 2): 7.2, (1, 3): 9.0, (1, 4): 8.3,
(2, 1): 2.9, (2, 2): 6.3, (2, 3): 9.8, (2, 4): 0.7,
(3, 1): 4.5, (3, 2): 4.8, (3, 3): 4.2, (3, 4): 9.3}
seq = ConcreteModel()
seq.Locations = range(N) #Set N
seq.Customers = range(M) #Set M
seq.x = Var(seq.Locations, seq.Customers, bounds=(0.0, 1.0)) #x[n,m]
seq.y = Var(seq.Locations, within=Binary)
seq.obj=Objective(expr = sum( d[n,m]*seq.x[n,m] for n in seq.Locations for m in seq.Customers))
seq.single_x = ConstraintList()
for m in seq.Customers:
seq.single_x.add(
sum(seq.x[n,m] for n in model.Locations ) == 1.0)
seq.bound_y = ConstraintList()
for n in seq.Locations:
for m in seq.Customers:
seq.bound_y.add( seq.x[n,m] <= seq.y[n] )
seq.num_facilities = Constraint(
expr=sum( seq.y[n] for n in seq.Locations) == P)
可能是什么问题?谢谢!
您遇到的关键错误是因为您的字典 d
没有 [0, 0] 的条目。
我的建议是将所有内容都带入模型(请参阅下面我的编辑)。这样你就可以“漂亮地打印”模型,这使得在构建时排除故障变得非常容易。
祝你好运!
from pyomo.environ import *
N = 3
M = 4
P = 3
d = {(1, 1): 1.7, (1, 2): 7.2, (1, 3): 9.0, (1, 4): 8.3,
(2, 1): 2.9, (2, 2): 6.3, (2, 3): 9.8, (2, 4): 0.7,
(3, 1): 4.5, (3, 2): 4.8, (3, 3): 4.2, (3, 4): 9.3}
seq = ConcreteModel()
### SETS
seq.Locations = Set(initialize = list(range(1, N+1))) #Set N # note the capitalization Set, the pyomo type
seq.Customers = Set(initialize = list(range(1, M+1))) #Set M
### PARAMS
seq.d = Param(seq.Locations, seq.Customers, initialize=d)
### VARS
seq.x = Var(seq.Locations, seq.Customers, bounds=(0.0, 1.0)) #x[n,m]
seq.y = Var(seq.Locations, within=Binary)
seq.obj=Objective(expr = sum( seq.d[n,m]*seq.x[n,m] for n in seq.Locations for m in seq.Customers))
seq.single_x = ConstraintList()
for m in seq.Customers:
seq.single_x.add(
sum(seq.x[n,m] for n in seq.Locations ) == 1.0)
seq.bound_y = ConstraintList()
for n in seq.Locations:
for m in seq.Customers:
seq.bound_y.add( seq.x[n,m] <= seq.y[n] )
seq.num_facilities = Constraint(
expr=sum( seq.y[n] for n in seq.Locations) == P)
seq.pprint()
我刚开始使用 pyomo 框架和 python。我正在尝试解决简单的模型。当我制定 objective 函数时,在 运行 代码
之前收到以下错误按键错误:(0, 0)
问题的表述如下: Formulation
代码:
from pyomo.environ import *
N = 3
M = 4
P = 3
d = {(1, 1): 1.7, (1, 2): 7.2, (1, 3): 9.0, (1, 4): 8.3,
(2, 1): 2.9, (2, 2): 6.3, (2, 3): 9.8, (2, 4): 0.7,
(3, 1): 4.5, (3, 2): 4.8, (3, 3): 4.2, (3, 4): 9.3}
seq = ConcreteModel()
seq.Locations = range(N) #Set N
seq.Customers = range(M) #Set M
seq.x = Var(seq.Locations, seq.Customers, bounds=(0.0, 1.0)) #x[n,m]
seq.y = Var(seq.Locations, within=Binary)
seq.obj=Objective(expr = sum( d[n,m]*seq.x[n,m] for n in seq.Locations for m in seq.Customers))
seq.single_x = ConstraintList()
for m in seq.Customers:
seq.single_x.add(
sum(seq.x[n,m] for n in model.Locations ) == 1.0)
seq.bound_y = ConstraintList()
for n in seq.Locations:
for m in seq.Customers:
seq.bound_y.add( seq.x[n,m] <= seq.y[n] )
seq.num_facilities = Constraint(
expr=sum( seq.y[n] for n in seq.Locations) == P)
可能是什么问题?谢谢!
您遇到的关键错误是因为您的字典 d
没有 [0, 0] 的条目。
我的建议是将所有内容都带入模型(请参阅下面我的编辑)。这样你就可以“漂亮地打印”模型,这使得在构建时排除故障变得非常容易。
祝你好运!
from pyomo.environ import *
N = 3
M = 4
P = 3
d = {(1, 1): 1.7, (1, 2): 7.2, (1, 3): 9.0, (1, 4): 8.3,
(2, 1): 2.9, (2, 2): 6.3, (2, 3): 9.8, (2, 4): 0.7,
(3, 1): 4.5, (3, 2): 4.8, (3, 3): 4.2, (3, 4): 9.3}
seq = ConcreteModel()
### SETS
seq.Locations = Set(initialize = list(range(1, N+1))) #Set N # note the capitalization Set, the pyomo type
seq.Customers = Set(initialize = list(range(1, M+1))) #Set M
### PARAMS
seq.d = Param(seq.Locations, seq.Customers, initialize=d)
### VARS
seq.x = Var(seq.Locations, seq.Customers, bounds=(0.0, 1.0)) #x[n,m]
seq.y = Var(seq.Locations, within=Binary)
seq.obj=Objective(expr = sum( seq.d[n,m]*seq.x[n,m] for n in seq.Locations for m in seq.Customers))
seq.single_x = ConstraintList()
for m in seq.Customers:
seq.single_x.add(
sum(seq.x[n,m] for n in seq.Locations ) == 1.0)
seq.bound_y = ConstraintList()
for n in seq.Locations:
for m in seq.Customers:
seq.bound_y.add( seq.x[n,m] <= seq.y[n] )
seq.num_facilities = Constraint(
expr=sum( seq.y[n] for n in seq.Locations) == P)
seq.pprint()