1

The Modeling and Solving Linear Programming with R book has a nice example on planning shifts in Sec 3.7. I am unable to solve it with R. Also, I am not clear with the solution provided in the book.

Problem

A company has a emergency center which is working 24 hours a day. In the table below, is detailed the minimal needs of employees for each of the six shifts of four hours in which the day is divided.

     Shift    Employees
00:00 - 04:00    5
04:00 - 08:00    7
08:00 - 12:00   18
12:00 - 16:00   12
16:00 - 20:00   15
20:00 - 00:00   10

R solution

I used the following to solve the above.

library(lpSolve)
obj.fun <- c(1,1,1,1,1,1)
constr <- c(1,1,0,0,0,0,
            0,1,1,0,0,0,
            0,0,1,1,0,0,
            0,0,0,1,1,0,
            0,0,0,0,1,1,
            1,0,0,0,0,1)
constr.dir <- rep(">=",6)
constr.val <-c (12,25,30,27,25,15)
day.shift <- lp("min",obj.fun,constr,constr.dir,constr.val,compute.sens = TRUE)

And, I get the following result.

> day.shift$objval
[1] 1.666667
> day.shift$solution
[1] 0.000000 1.666667 0.000000 0.000000 0.000000 0.000000

This is nowhere close to the numerical solution mentioned in the book.

Numerical solution

The total number of solutions required as per the numerical solution is 38. However, since the problem stated that, there is a defined minimum number of employees in every period, how can this solution be valid?

s1 5 s2 6 s3 12 s4 0 s5 15 s6 0

cogitoergosum
  • 2,309
  • 4
  • 38
  • 62

1 Answers1

3

Your mistake is at the point where you initialize the variable constr, because you don't define it as a matrix. Second fault is your matrix itself. Just look at my example.

I was wondering why you didn't stick to the example in the book because I wanted to check my solution. Mine is based on that.

library(lpSolve)
obj.fun <- c(1,1,1,1,1,1)
constr <- matrix(c(1,0,0,0,0,1,
        1,1,0,0,0,0,
        0,1,1,0,0,0,
        0,0,1,1,0,0,
        0,0,0,1,1,0,
        0,0,0,0,1,1), ncol = 6, byrow = TRUE)
constr.dir <- rep(">=",6)
constr.val <-c (5,7,18,12,15,10)
day.shift <- lp("min",obj.fun,constr,constr.dir,constr.val,compute.sens = TRUE)

day.shift$objval
# [1] 38
day.shift$solution
# [1]  5 11  7  5 10  0

EDIT based on your question in the comments:

This is the distribution of the shifts on the periods:

shift | 0-4 | 4-8 | 8-12 | 12-16 | 16-20 | 20-24
---------------------------------------------------
20-4  | 5   | 5   |      |       |       |
0-8   |     | 11  | 11   |       |       |
4-12  |     |     | 7    | 7     |       |
8-16  |     |     |      | 5     | 5     |
12-20 |     |     |      |       | 10    | 10
18-24 |     |     |      |       |       |
----------------------------------------------------
sum   | 5   | 16  | 18   | 12    | 15    | 10
----------------------------------------------------
need  | 5   | 7   | 18   | 12    | 15    | 10
---------------------------------------------------
Felix Grossmann
  • 1,224
  • 1
  • 11
  • 30
  • oh man! What a face-palm moment! I got it working with matrix. Thanks! However, I would still like to know, how is it a valid solution when, every shift has a non-zero minimum requirement. – cogitoergosum Apr 01 '17 at 07:15
  • The `constr.val` that I used sets constraint to sum of the minima of 8hr shift. For example, first shift minimum is 5 and the second is 7. So, the minimum of both put together is 12. If the constraint was set to 5, then a theoretically, a value of 6 will satisfy the constraint; but, not the limit of individual shifts. This is the second question I have. – cogitoergosum Apr 01 '17 at 07:20
  • To your second question. Your linear program should solve the question, how many shifts of each type there should be. You can fill the 00:00-04:00 hour shift also with the 20:00-04:00 shift, not just with the 00:00-08:00 hour shift. So fixing your shifts is fixing your decision variables. – Felix Grossmann Apr 01 '17 at 07:28
  • Thanks! The numerical solution in the book is `5,6,12,0,15,0` for si (i=1 to 6). – cogitoergosum Apr 01 '17 at 07:56
  • I know but I cannot see any fault (the solution is viable as you see in the table) and the objective function has the same value. So there are simply multiple solutions :) Btw I changed my answer a bit, because your matrix was not correct too. I would really appreciat it if you would mark the question as answered :) – Felix Grossmann Apr 01 '17 at 07:58
  • I have marked as answered; would it be possible for you to remove the down-vote to this question, please? Thanks! – cogitoergosum Apr 01 '17 at 08:04
  • I got you*, enjoyed this. Downloaded the pdf that was linked and am teaching myself this now. Upvoted. – Evan Friedland Apr 01 '17 at 15:56
  • Can you please elaborate on how exactly you got this "distribution of the shifts" table in R? – Serzhan Akhmetov Nov 30 '18 at 06:14
  • @FelixGrossmann I am working on a similar problem. An additional constraint that I would like to LP is i want to control the number of shifts. Meaning I have provided 6 shift timings but in the output I want only 4 shifts with min of 10 ppl in each shift. Is this doable? can you please help me with the code – Lalitha Sundar Iyer S Mar 04 '20 at 13:41