# optimize days between work shift

For a work shift optimization problem, I've defined a binary variable in PuLP as follows:

pulp.LpVariable.dicts('VAR', (range(D), range(N)), 0, 1, 'Binary')


where

1. $$D =$$ # days in each schedule we create (= 28, or 4 weeks)
2. $$N =$$ # of workers

and the variable $$VAR$$ represents whether worker $$J$$ works in Day $$I$$ or not.

I want to ensure that work is evenly distributed over the 28 days (a.k.a $$VAR$$ for worker $$J$$ should look more like [1,0,0,0,1,0,0,0,0,1,...,var[i=27][J]] rather than [1,1,1,0,0,0,0,0,...,var[i=27][J]] where work is clustered in the beginning). Since I can't add if statements/access the values of variables before they are defined, an approach where we minimize the range in days between work shifts doesn't seem feasible (since the work shifts themselves haven't been allocated yet). What could be a way to model this optimization problem?

• you could write something like $x_{i,t} \le 1-x_{i,t+k}$ for appropriate values of $k$. Jan 6, 2022 at 15:21

You can enforce "no more than $$w$$ workdays in any consecutive $$d$$-day period" via linear constraints $$\sum_{i=t}^{t+d-1} x_i \le w \quad \text{for all t}$$
If you cannot enforce a specific maximum days policy as Rob Pratt suggests, another possibility is to penalize the lumpiness of the work distribution. Pick a window size $$d$$ (Rob's "$$d$$-day period"), and add two new variables $$y$$ and $$z$$ along with the constraints $$y \ge \sum_{i=t}^{t+d-1} x_i \quad \forall t \in \lbrace1,\dots,D-d+1\rbrace$$and $$z \le \sum_{i=t}^{t+d-1} x_i \quad \forall t \in \lbrace1,\dots,D-d+1\rbrace.$$Now penalize $$y-z$$ in your objective function.