0

I am trying to build a scheduling optimization for employees working in different roles, days and shifts. I want to optimize the preference score given a schedule (the assignment matrix). However, I am having trouble with defining my objective function. I am using PuLP to setup my code, here is my setup:

from __future__ import print_function
import numpy as np
import pandas as pd
import pulp
from random import randint
from itertools import product

employees = range(11)
roles = range(5)
days = range(6)
shifts = range(3)

Variable X is the Assignment matrix which consists of 0s and 1s

X = pulp.LpVariable.dicts("X", product(employees, roles, days, shifts),
                          cat=pulp.LpBinary)

P is the preference score

for k in employees:
    for j in roles:
        for i in days: 
            for h in nr_shifts:
                P[(k, j, i,h)] = np.random.rand() - 0.5

The problem, maximizing the preference score:

scheduling_problem = pulp.LpProblem("Employee Scheduling", pulp.LpMaximize)

And the objective function (which is causing an error)

scheduling_problem += (pulp.lpSum(X[(k, j, i, h)] * P[(k, j, i, h)])
        for k in employees for j in roles for i in days for h in shifts
)

the error after running the objective function:

TypeError: Can only add LpConstraintVar, LpConstraint, LpAffineExpression or True objects

My objective is to sum the preference scores over the employees, roles, days and shifts and I thought that multiplying my preference scores with the assignments (simply 0s and 1s) would do the trick.

Any thoughts on this?

Thnx

Schmidt88
  • 1
  • 3

1 Answers1

0

That last statement is not valid Python: check the parentheses. You probably mean:

scheduling_problem += pulp.lpSum(X[(k, j, i, h)] * P[(k, j, i, h)]
        for k in employees for j in roles for i in days for h in shifts)
Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39