TypeError: unhashable type: 'list' and unknown error in definition of objective function in pyomo
TypeError: unhashable type: 'list' and unknown error in definition of objective function in pyomo
我是新手,我正在尝试通过 Jupiter notebook 中的 pyomo 编写优化问题(固定 cost 多商品网络设计)。我导入os, from collections import defaultdict, import networkx as nx, import pandas as pd, import pyomo.environ as pe, import pyomo.opt as po.
model.nodes = pe.Set(initialize=nodes)
model.edges = pe.Set(within=model.nodes*model.nodes, initialize=edges)
model.customer = pe.Set(initialize=customer)
model.p = pe.Set(initialize=p)
model.delta_neg = pe.Param(model.nodes, initialize=delta_neg, within=pe.Any, default=set())
model.delta_pos = pe.Param(model.nodes, initialize=delta_pos, within=pe.Any, default=set())
model.demand = pe.Param(model.customer, initialize=demand)
model.origin = pe.Param(model.customer, initialize=origin)
model.destination = pe.Param(model.customer, initialize=destination)
model.fixcost = pe.Param(model.edges, initialize=fixcost)
model.unitcost = pe.Param(model.edges, initialize=unitcost)
model.capacity = pe.Param(model.edges, initialize=capacity)
model.price = pe.Param(model.p, initialize=price)
model.x = pe.Var(model.edges, model.customer, within=pe.Binary)
model.y = pe.Var(model.edges, within=pe.Binary)
model.g = pe.Var(model.customer, within=pe.Binary)
model.alf= pe.Var(model.customer, model.p, within=pe.Binary)
model.z= pe.Var(model.edges, model.customer, within=pe.PositiveReals)
model.ua= pe.Var(model.nodes, model.customer, within=pe.PositiveReals)
objective函数:
def profit(model):
return sum(model.fixcost[i, j] * model.y[i, j] * (-1) for (i, j) in model.edges) + sum(model.alf[customer,p] * model.demand[customer] * model.price[p] for custmer in model.customer for p in model.p) - sum(0.3 * model.alf[customer,p] * model.price[p]**2 for custmer in model.customer for p in model.p) - sum(model.demand[customer] * model.unitcost[i,j] * model.x[i,j,customer] for (i, j) in model.edges for custmer in model.customer) + sum(0.3 * model.unitcost[i,j] * model.z[i, j, customer] for (i, j) in model.edges for custmer in model.customer) - sum(2000000 * model.g[customer] for custmer in model.customer)
model.profit = pe.Objective(sense=pe.maximize, rule=profit)
当我编写 objective 函数时收到此错误
ERROR: Rule failed when generating expression for Objective profit with index
None: DeveloperError: Internal Pyomo implementation error:
'Unknown problem encountered when trying to retrieve index for component
alf' Please report this to the Pyomo Developers.
ERROR: Constructing component 'profit' from data=None failed: DeveloperError:
Internal Pyomo implementation error:
'Unknown problem encountered when trying to retrieve index for component
alf' Please report this to the Pyomo Developers.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\anaconda3\lib\site-packages\pyomo\core\base\indexed_component.py in __getitem__(self, index)
522 try:
--> 523 obj = self._data.get(index, _NotFound)
524 except TypeError:
TypeError: unhashable type: 'list'
During handling of the above exception, another exception occurred:
DeveloperError Traceback (most recent call last)
DeveloperError: Internal Pyomo implementation error:
'Unknown problem encountered when trying to retrieve index for component alf'
Please report this to the Pyomo Developers.
我只带错误的开头和结尾。
欢迎访问本网站。你在这里被几件事咬伤了。但在此之前,如果您有一段代码产生错误,通常会包含一个 minimum reproducible example 以便有人可以 copy/paste 它并得到您正在处理的错误。如果您这样做,您获得满意回复的几率会高 很多!
那么您的代码发生了什么...您在 objective 函数中多次将“客户”一词拼错为 custmer
。因此,当 python 尝试 运行 求和时,它找不到一个名为 customer
的局部变量来对事物进行索引,因此它会返回整个程序的范围和你的原始数据列表 customer
到索引和伪造的 custmer
变量不做你想让它做的任何事情。但是,您的代码中没有语法错误,只是一个不幸的拼写错误,因此您可能没有看到任何静态错误。
那你能做什么?
- 修正拼写错误
- 将长得离谱的 objective 函数分解成单独的表达式(见下文我的)。这将使它们更容易进行故障排除,并且您可以通过执行诸如
pe.value(z2)
.
- 更仔细地选择数据元素的名称。如果您坚持将事物集合命名为 复数名称 的惯例,您会更早发现这一点……您有
nodes
、edges
...为什么不 customers
作为数据源?
以下是建议的方法,构建没有错误。为了清楚起见,我没有更改客户 --> 客户。
import pyomo.environ as pe
model = pe.ConcreteModel()
nodes = [1, 2]
edges = [(1, 2)]
customer = ['Bob',]
demand = {'Bob': 4}
origin = {'Bob': 2}
destination = {'Bob': 3}
fixcost = { (1,2): 0.5}
unitcost = fixcost
capacity = fixcost
p = [2,]
price = {2:1}
model.nodes = pe.Set(initialize=nodes)
model.edges = pe.Set(within=model.nodes*model.nodes, initialize=edges)
model.customer = pe.Set(initialize=customer)
model.p = pe.Set(initialize=p)
#model.delta_neg = pe.Param(model.nodes, initialize=delta_neg, within=pe.Any, default=set())
#model.delta_pos = pe.Param(model.nodes, initialize=delta_pos, within=pe.Any, default=set())
model.demand = pe.Param(model.customer, initialize=demand)
model.origin = pe.Param(model.customer, initialize=origin)
model.destination = pe.Param(model.customer, initialize=destination)
model.fixcost = pe.Param(model.edges, initialize=fixcost)
model.unitcost = pe.Param(model.edges, initialize=unitcost)
model.capacity = pe.Param(model.edges, initialize=capacity)
model.price = pe.Param(model.p, initialize=price)
model.x = pe.Var(model.edges, model.customer, within=pe.Binary)
model.y = pe.Var(model.edges, within=pe.Binary)
model.g = pe.Var(model.customer, within=pe.Binary)
model.alf= pe.Var(model.customer, model.p, within=pe.Binary)
model.z= pe.Var(model.edges, model.customer, within=pe.PositiveReals)
model.ua= pe.Var(model.nodes, model.customer, within=pe.PositiveReals)
#OBJ components
z1 = sum(model.fixcost[i, j] * model.y[i, j] * (-1) for (i, j) in model.edges)
z2 = sum(model.alf[customer,p] * model.demand[customer] * model.price[p] for customer in model.customer for p in model.p)
z3 = - sum(0.3 * model.alf[customer,p] * model.price[p]**2 for customer in model.customer for p in model.p)
z4 = - sum(model.demand[customer] * model.unitcost[i,j] * model.x[i,j,customer] for (i, j) in model.edges for customer in model.customer)
z5 = sum(0.3 * model.unitcost[i,j] * model.z[i, j, customer] for (i, j) in model.edges for customer in model.customer)
z6 = - sum(2000000 * model.g[customer] for customer in model.customer)
# make OBJ...
z = z1 + z2 + z3 + z4 + z5 + z6
model.profit = pe.Objective(sense=pe.maximize, expr=z)
model.pprint()
我是新手,我正在尝试通过 Jupiter notebook 中的 pyomo 编写优化问题(固定 cost 多商品网络设计)。我导入os, from collections import defaultdict, import networkx as nx, import pandas as pd, import pyomo.environ as pe, import pyomo.opt as po.
model.nodes = pe.Set(initialize=nodes)
model.edges = pe.Set(within=model.nodes*model.nodes, initialize=edges)
model.customer = pe.Set(initialize=customer)
model.p = pe.Set(initialize=p)
model.delta_neg = pe.Param(model.nodes, initialize=delta_neg, within=pe.Any, default=set())
model.delta_pos = pe.Param(model.nodes, initialize=delta_pos, within=pe.Any, default=set())
model.demand = pe.Param(model.customer, initialize=demand)
model.origin = pe.Param(model.customer, initialize=origin)
model.destination = pe.Param(model.customer, initialize=destination)
model.fixcost = pe.Param(model.edges, initialize=fixcost)
model.unitcost = pe.Param(model.edges, initialize=unitcost)
model.capacity = pe.Param(model.edges, initialize=capacity)
model.price = pe.Param(model.p, initialize=price)
model.x = pe.Var(model.edges, model.customer, within=pe.Binary)
model.y = pe.Var(model.edges, within=pe.Binary)
model.g = pe.Var(model.customer, within=pe.Binary)
model.alf= pe.Var(model.customer, model.p, within=pe.Binary)
model.z= pe.Var(model.edges, model.customer, within=pe.PositiveReals)
model.ua= pe.Var(model.nodes, model.customer, within=pe.PositiveReals)
objective函数:
def profit(model):
return sum(model.fixcost[i, j] * model.y[i, j] * (-1) for (i, j) in model.edges) + sum(model.alf[customer,p] * model.demand[customer] * model.price[p] for custmer in model.customer for p in model.p) - sum(0.3 * model.alf[customer,p] * model.price[p]**2 for custmer in model.customer for p in model.p) - sum(model.demand[customer] * model.unitcost[i,j] * model.x[i,j,customer] for (i, j) in model.edges for custmer in model.customer) + sum(0.3 * model.unitcost[i,j] * model.z[i, j, customer] for (i, j) in model.edges for custmer in model.customer) - sum(2000000 * model.g[customer] for custmer in model.customer)
model.profit = pe.Objective(sense=pe.maximize, rule=profit)
当我编写 objective 函数时收到此错误
ERROR: Rule failed when generating expression for Objective profit with index
None: DeveloperError: Internal Pyomo implementation error:
'Unknown problem encountered when trying to retrieve index for component
alf' Please report this to the Pyomo Developers.
ERROR: Constructing component 'profit' from data=None failed: DeveloperError:
Internal Pyomo implementation error:
'Unknown problem encountered when trying to retrieve index for component
alf' Please report this to the Pyomo Developers.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\anaconda3\lib\site-packages\pyomo\core\base\indexed_component.py in __getitem__(self, index)
522 try:
--> 523 obj = self._data.get(index, _NotFound)
524 except TypeError:
TypeError: unhashable type: 'list'
During handling of the above exception, another exception occurred:
DeveloperError Traceback (most recent call last)
DeveloperError: Internal Pyomo implementation error:
'Unknown problem encountered when trying to retrieve index for component alf'
Please report this to the Pyomo Developers.
我只带错误的开头和结尾。
欢迎访问本网站。你在这里被几件事咬伤了。但在此之前,如果您有一段代码产生错误,通常会包含一个 minimum reproducible example 以便有人可以 copy/paste 它并得到您正在处理的错误。如果您这样做,您获得满意回复的几率会高 很多!
那么您的代码发生了什么...您在 objective 函数中多次将“客户”一词拼错为 custmer
。因此,当 python 尝试 运行 求和时,它找不到一个名为 customer
的局部变量来对事物进行索引,因此它会返回整个程序的范围和你的原始数据列表 customer
到索引和伪造的 custmer
变量不做你想让它做的任何事情。但是,您的代码中没有语法错误,只是一个不幸的拼写错误,因此您可能没有看到任何静态错误。
那你能做什么?
- 修正拼写错误
- 将长得离谱的 objective 函数分解成单独的表达式(见下文我的)。这将使它们更容易进行故障排除,并且您可以通过执行诸如
pe.value(z2)
. - 更仔细地选择数据元素的名称。如果您坚持将事物集合命名为 复数名称 的惯例,您会更早发现这一点……您有
nodes
、edges
...为什么不customers
作为数据源?
以下是建议的方法,构建没有错误。为了清楚起见,我没有更改客户 --> 客户。
import pyomo.environ as pe
model = pe.ConcreteModel()
nodes = [1, 2]
edges = [(1, 2)]
customer = ['Bob',]
demand = {'Bob': 4}
origin = {'Bob': 2}
destination = {'Bob': 3}
fixcost = { (1,2): 0.5}
unitcost = fixcost
capacity = fixcost
p = [2,]
price = {2:1}
model.nodes = pe.Set(initialize=nodes)
model.edges = pe.Set(within=model.nodes*model.nodes, initialize=edges)
model.customer = pe.Set(initialize=customer)
model.p = pe.Set(initialize=p)
#model.delta_neg = pe.Param(model.nodes, initialize=delta_neg, within=pe.Any, default=set())
#model.delta_pos = pe.Param(model.nodes, initialize=delta_pos, within=pe.Any, default=set())
model.demand = pe.Param(model.customer, initialize=demand)
model.origin = pe.Param(model.customer, initialize=origin)
model.destination = pe.Param(model.customer, initialize=destination)
model.fixcost = pe.Param(model.edges, initialize=fixcost)
model.unitcost = pe.Param(model.edges, initialize=unitcost)
model.capacity = pe.Param(model.edges, initialize=capacity)
model.price = pe.Param(model.p, initialize=price)
model.x = pe.Var(model.edges, model.customer, within=pe.Binary)
model.y = pe.Var(model.edges, within=pe.Binary)
model.g = pe.Var(model.customer, within=pe.Binary)
model.alf= pe.Var(model.customer, model.p, within=pe.Binary)
model.z= pe.Var(model.edges, model.customer, within=pe.PositiveReals)
model.ua= pe.Var(model.nodes, model.customer, within=pe.PositiveReals)
#OBJ components
z1 = sum(model.fixcost[i, j] * model.y[i, j] * (-1) for (i, j) in model.edges)
z2 = sum(model.alf[customer,p] * model.demand[customer] * model.price[p] for customer in model.customer for p in model.p)
z3 = - sum(0.3 * model.alf[customer,p] * model.price[p]**2 for customer in model.customer for p in model.p)
z4 = - sum(model.demand[customer] * model.unitcost[i,j] * model.x[i,j,customer] for (i, j) in model.edges for customer in model.customer)
z5 = sum(0.3 * model.unitcost[i,j] * model.z[i, j, customer] for (i, j) in model.edges for customer in model.customer)
z6 = - sum(2000000 * model.g[customer] for customer in model.customer)
# make OBJ...
z = z1 + z2 + z3 + z4 + z5 + z6
model.profit = pe.Objective(sense=pe.maximize, expr=z)
model.pprint()