-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library.fs
240 lines (183 loc) · 8.96 KB
/
Library.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
namespace SchedulingLib
open Flips
open Flips.Types
open Flips.SliceMap
open Flips.UnitsOfMeasure
open System.Diagnostics
[<Measure>]
type Euro
[<Measure>]
type Hour
[<Measure>]
type Strain
[<Measure>]
type Worker
[<Measure>]
type Shift
type Options =
{
StrainMinimizing: bool
EnsureQualifiedPersonnelConstraint: bool
NoDoubleShiftConstraint: bool
MaximumWorkingHoursConstraint: bool
MinimumWorkingHoursConstraint: bool }
//! Domain Model
// Every day has a certain amount of time slots (default: 3) which can each contrain 0, 1 or more shifts which need to be staffed
type Employee =
{ Name: string
Occupations: string list
Wage: float<Euro / Hour> }
type RequiredPersonnel =
{ Count: int<Worker / Shift>
RequiredQualifications: string list }
type ShiftInfo =
{ Name: string
Length: float<Hour / Shift>
RequiredPersonnel: RequiredPersonnel list
Strain: float<Strain / Shift> }
type TimeSlot = { Shifts: ShiftInfo list }
type Day = { TimeSlots: TimeSlot list }
type Week = { Days: Day list }
type Schedule = { Weeks: Week list }
type Problem =
{ Workers: Employee list
Schedule: Schedule
MaxHoursPerWeek: float<Hour>
MinHoursPerWeek: float<Hour>
Options: Options }
exception ModelError of string
type Solution = {
Result:string list list list list list
ObjectiveCost:float<Euro>
ObjectiveStrain: (string * float<Strain>) list
}
module Scheduler =
let solve (problem: Problem) =
// Helper variables to make the code more readable
let workers = problem.Workers
let Schedule = problem.Schedule
let maxHoursPerWeek = problem.MaxHoursPerWeek
let minHoursPerWeek = problem.MinHoursPerWeek
let workersWage = [ for record in workers -> record, record.Wage ] |> SMap.ofList
// Here are the shifts helpers defined
let shiftLength =
[ for x = 0 to Schedule.Weeks.Length - 1 do
for y = 0 to Schedule.Weeks.[x].Days.Length - 1 do
for z = 0 to Schedule.Weeks.[x].Days.[y].TimeSlots.Length - 1 do
for shift in Schedule.Weeks.[x].Days.[y].TimeSlots.[z].Shifts -> (x, y, z, shift), shift.Length ]
|> SMap4.ofList
let strainOfShifts =
[ for x = 0 to Schedule.Weeks.Length - 1 do
for y = 0 to Schedule.Weeks.[x].Days.Length - 1 do
for z = 0 to Schedule.Weeks.[x].Days.[y].TimeSlots.Length - 1 do
for shift in Schedule.Weeks.[x].Days.[y].TimeSlots.[z].Shifts -> (x, y, z, shift), shift.Strain ]
|> SMap4.ofList
// Builds a binary matrix per worker of 3 shifts (as columns) and 7 Days (as Rows) for every employee
//! Decision
let shouldWork =
DecisionBuilder<Shift> "Has to work" {
for employee in workers do
for x = 0 to Schedule.Weeks.Length - 1 do
for y = 0 to Schedule.Weeks.[x].Days.Length - 1 do
for z = 0 to Schedule.Weeks.[x].Days.[y].TimeSlots.Length - 1 do
for shift in Schedule.Weeks.[x].Days.[y].TimeSlots.[z].Shifts -> Boolean
}
|> SMap5.ofSeq
let containsAllElements list1 list2 =
List.forall (fun elem -> List.contains elem list1) list2
//! Constraints
// Ensures sufficient, qualified staffing
let qualifiedConstraints =
ConstraintBuilder "Ensure qualified personell and enough of workers of in shift" {
for x = 0 to Schedule.Weeks.Length - 1 do
for y = 0 to Schedule.Weeks.[x].Days.Length - 1 do
for z = 0 to Schedule.Weeks.[x].Days.[y].TimeSlots.Length - 1 do
for shift in Schedule.Weeks.[x].Days.[y].TimeSlots.[z].Shifts do
for reqPersonnel in shift.RequiredPersonnel ->
sum (
shouldWork.[Where(fun employee ->
containsAllElements
employee.Occupations
reqPersonnel.RequiredQualifications),
x,
y,
z,
shift]
)
>== float (reqPersonnel.Count) * 1.0<Shift>
}
// Maximum worktime per week
let maxHoursConstraints =
ConstraintBuilder "Maximum Constraint" {
for employee in workers do
for week = 0 to Schedule.Weeks.Length - 1 do
yield
sum (shouldWork.[employee, week, All, All, All] .* shiftLength.[week, All, All, All])
<== maxHoursPerWeek
}
let minimumHoursConstraint =
ConstraintBuilder "Minimum hours constraint" {
for employee in workers do
for week = 0 to Schedule.Weeks.Length - 1 do
yield
sum (shouldWork.[employee, week, All, All, All] .* shiftLength.[week, All, All, All])
>== minHoursPerWeek
}
// No double shift on one day can be worked
let noDoubleShiftConstraint =
ConstraintBuilder "No Double Shift Constraint" {
for employee in workers do
for x = 0 to Schedule.Weeks.Length - 1 do
for y = 0 to Schedule.Weeks.[x].Days.Length - 1 do
yield sum (shouldWork.[employee, x, y, All, All]) <== 1.0<Shift>
}
let minimizeStrain =
[ for employee in workers ->
sum (shouldWork[employee, All,All,All,All] .* strainOfShifts.[All,All,All,All])
|> Objective.create (sprintf ("Minimize Strain Target %s") (employee.Name)) Minimize
|> fun x -> (employee.Name,x)
]
let minimizeCosts =
[ for employee in workers ->
sum (shouldWork.[employee, All,All,All,All] .* shiftLength.[All,All,All,All]) * workersWage.[employee]
]
|> List.sum
|> Objective.create "Minimize Cost Target" Minimize
let retrieveSolutionValues (result: SolveResult) =
match result with
| Optimal solution ->
let values = Solution.getValues solution shouldWork |> SMap5.ofMap
let resultMatrix =
[
for week = 0 to Schedule.Weeks.Length - 1 do
[
for day = 0 to Schedule.Weeks.[week].Days.Length - 1 do
[
for timeslot = 0 to Schedule.Weeks.[week].Days.[day].TimeSlots.Length - 1 do
[ for shift in Schedule.Weeks.[week].Days.[day].TimeSlots.[timeslot].Shifts do
[
let x = values.[All, week, day, timeslot, shift]
for employee in workers do
if x.[employee] = 1.0<Shift> then
yield employee.Name
] ] ] ] ]
{ Result = resultMatrix
ObjectiveCost = Objective.evaluate solution minimizeCosts
ObjectiveStrain = minimizeStrain |> List.map(fun (name,strain) -> (name, Objective.evaluate solution strain)) }
| _ ->
raise (ModelError "Model infeasible. Try again")
let solved =
let options = problem.Options
let mutable model = Model.create minimizeCosts
if options.StrainMinimizing then
minimizeStrain |> List.iter(fun (name,objec) -> model <- (Model.addObjective objec model))
if options.EnsureQualifiedPersonnelConstraint then
model <- Model.addConstraints qualifiedConstraints model
if options.NoDoubleShiftConstraint then
model <- Model.addConstraints noDoubleShiftConstraint model
if options.MaximumWorkingHoursConstraint then
model <- Model.addConstraints maxHoursConstraints model
if options.MinimumWorkingHoursConstraint then
model <- Model.addConstraints minimumHoursConstraint model
model |> Solver.solve Settings.basic
retrieveSolutionValues solved