pyomo 中的维度错误变量声明
Dimension error variable declaration in pyomo
我是 pyomo 的初学者,我正在努力声明我的决策变量并收到错误:ValueError: The value=('6', (2, 3, 4)) has dimension 4 and is not valid for Set y_index which has dimen=3
。我的数据如下所示:
员工名单:
N = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
员工周末集(键:员工 ID,值:周末集)
EmployeeWE = {'0':[(2, 3),(9, 10),(16, 17),(23, 24)],
'1':[(2, 3),(9, 10),(16, 17),(23, 24)],
'2':[(2, 3),(9, 10),(16, 17),(23, 24)],
'3':[(2, 3),(9, 10),(16, 17),(23, 24)],
'4':[(2, 3),(9, 10),(16, 17),(23, 24)],
'5':[(2, 3),(9, 10),(16, 17),(23, 24)],
'6':[(2, 3,4),(9, 10,11),(16, 17,18),(23, 24,25)],
'7':[(2, 3,4),(9, 10,11),(16, 17,18),(23, 24,25)],
'8':[(2, 3),(9, 10),(16, 17),(23, 24)],
'9':[(2, 3),(9, 10),(16, 17),(23, 24)]}
如您所见,对于员工 3 和 4,他们的周末定义是休息 3 天。所以我所做的是:
from pyomo.environ import *
model = ConcreteModel()
#Set declaration:
model.n = Set(initialize = N)
#model.WE = ??? -> no clue of how to declare a dictionary with a list of tupples
#Var
model.y = Var(((employees, weekends) for employees in model.n for weekends in EmployeeWE[employees]), within=Binary, initialize=0)
所以我猜这个错误来自 2 名周末不同的员工,但我该如何解决这个问题?对于变量声明,我需要知道员工 n 在周末 i 是否工作。周末我指的是字典中元组列表的索引(+1,从 1 开始)。例如,一组周末员工 ID 1:
1:(2,3), 2:(9,10), 3:(16,17), 4:(23,24) 等等。谢谢!
这是我用来解决问题的代码
unique_WE = sorted(set(itertools.chain.from_iterable(EmployeeWE .values())))
map_I_WE = {k: v for k, v in enumerate(unique_WE)}
map_WE_I = {map_I_WE[k]: k for k in map_I_WE}
I = list(map_I_WE.keys())
map_N_I = defaultdict(list)
for n in EmployeeWE :
for w in EmployeeWE [n]:
map_N_I[n].append(map_WE_I[w])
model.I = Set(initialize = I)
model.N_I = Set(within=model.N * model.I,
initialize=[(n, i) for n in map_N_I for i in map_N_I[n]])
我是 pyomo 的初学者,我正在努力声明我的决策变量并收到错误:ValueError: The value=('6', (2, 3, 4)) has dimension 4 and is not valid for Set y_index which has dimen=3
。我的数据如下所示:
员工名单:
N = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
员工周末集(键:员工 ID,值:周末集)
EmployeeWE = {'0':[(2, 3),(9, 10),(16, 17),(23, 24)],
'1':[(2, 3),(9, 10),(16, 17),(23, 24)],
'2':[(2, 3),(9, 10),(16, 17),(23, 24)],
'3':[(2, 3),(9, 10),(16, 17),(23, 24)],
'4':[(2, 3),(9, 10),(16, 17),(23, 24)],
'5':[(2, 3),(9, 10),(16, 17),(23, 24)],
'6':[(2, 3,4),(9, 10,11),(16, 17,18),(23, 24,25)],
'7':[(2, 3,4),(9, 10,11),(16, 17,18),(23, 24,25)],
'8':[(2, 3),(9, 10),(16, 17),(23, 24)],
'9':[(2, 3),(9, 10),(16, 17),(23, 24)]}
如您所见,对于员工 3 和 4,他们的周末定义是休息 3 天。所以我所做的是:
from pyomo.environ import *
model = ConcreteModel()
#Set declaration:
model.n = Set(initialize = N)
#model.WE = ??? -> no clue of how to declare a dictionary with a list of tupples
#Var
model.y = Var(((employees, weekends) for employees in model.n for weekends in EmployeeWE[employees]), within=Binary, initialize=0)
所以我猜这个错误来自 2 名周末不同的员工,但我该如何解决这个问题?对于变量声明,我需要知道员工 n 在周末 i 是否工作。周末我指的是字典中元组列表的索引(+1,从 1 开始)。例如,一组周末员工 ID 1:
1:(2,3), 2:(9,10), 3:(16,17), 4:(23,24) 等等。谢谢!
这是我用来解决问题的代码
unique_WE = sorted(set(itertools.chain.from_iterable(EmployeeWE .values())))
map_I_WE = {k: v for k, v in enumerate(unique_WE)}
map_WE_I = {map_I_WE[k]: k for k in map_I_WE}
I = list(map_I_WE.keys())
map_N_I = defaultdict(list)
for n in EmployeeWE :
for w in EmployeeWE [n]:
map_N_I[n].append(map_WE_I[w])
model.I = Set(initialize = I)
model.N_I = Set(within=model.N * model.I,
initialize=[(n, i) for n in map_N_I for i in map_N_I[n]])