Search code examples
pythonmodelpyomo

Dimension error variable declaration in pyomo


I'm kinda a begginer in pyomo and I'm struggeling to declare my decision variables and getting the error:ValueError: The value=('6', (2, 3, 4)) has dimension 4 and is not valid for Set y_index which has dimen=3. My data look like this:

List of Employees:

N = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

Set of weekends by employees (keys: employee ID, values: set of weekends)

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)]}

As you can see for the employees 3 & 4, the weekends definition for them is off 3 days. So what I did was:

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)

So I guess the error come from the 2 employees with a different set of weekends but how can I fix this ? For the variable declaration I need to know if employee n works or not at weekend i. By weekend i I mean the index (+1, strating from 1) of the list of tupples in the dictionary. For e.g., set of weekends employee ID 1:

1:(2,3), 2:(9,10), 3:(16,17), 4:(23,24) and so on for each employees. Thank you!


Solution

  • Here's the code I used to solve my problem

    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]])