|
| 1 | +""" |
| 2 | +This example shows how one can optimize a model with categorical data by converting it into integers. |
| 3 | +
|
| 4 | +There are three employees (Alice, Bob, Charlie) and three shifts. Each shift is assigned an integer: |
| 5 | +
|
| 6 | +Morning - 0 |
| 7 | +Afternoon - 1 |
| 8 | +Night - 2 |
| 9 | +
|
| 10 | +The employees have availabilities (e.g. Alice can only work in the Morning and Afternoon), and different |
| 11 | +salary demands. These constraints, and an additional one stipulating that every shift must be covered, |
| 12 | +allows us to model a MIP with the objective of minimizing the money spent on salary. |
| 13 | +""" |
| 14 | + |
1 | 15 | from pyscipopt import Model |
2 | 16 |
|
3 | 17 | # Define categorical data |
4 | | -shifts = {"Morning": 0, "Afternoon": 1, "Night": 2} |
5 | | -employees = ["Alice", "Bob", "Charlie"] |
6 | | - |
7 | | -# Employees have different salary demands |
8 | | -cost = { |
9 | | - "Alice": [2,4,1], |
10 | | - "Bob": [3,2,7], |
11 | | - "Charlie": [3,3,3] |
12 | | -} |
| 18 | +shift_to_int = {"Morning": 0, "Afternoon": 1, "Night": 2} |
| 19 | +employees = ["Alice", "Bob", "Charlie"] |
13 | 20 |
|
14 | 21 | # Employee availability |
15 | 22 | availability = { |
|
19 | 26 | } |
20 | 27 |
|
21 | 28 | # Transform availability into integer values |
22 | | -availability_int = { |
23 | | - emp: [shifts[shift] for shift in available_shifts] |
24 | | - for emp, available_shifts in availability.items() |
| 29 | +availability_int = {} |
| 30 | +for emp, available_shifts in availability.items(): |
| 31 | + availability_int[emp] = [shift_to_int[shift] for shift in available_shifts] |
| 32 | + |
| 33 | + |
| 34 | +# Employees have different salary demands |
| 35 | +cost = { |
| 36 | + "Alice": [2,4,1], |
| 37 | + "Bob": [3,2,7], |
| 38 | + "Charlie": [3,3,3] |
25 | 39 | } |
26 | 40 |
|
27 | 41 | # Create the model |
|
30 | 44 | # x[e, s] = 1 if employee e is assigned to shift s |
31 | 45 | x = {} |
32 | 46 | for e in employees: |
33 | | - for s in shifts.values(): |
| 47 | + for s in shift_to_int.values(): |
34 | 48 | x[e, s] = model.addVar(vtype="B", name=f"x({e},{s})") |
35 | 49 |
|
36 | 50 | # Each shift must be assigned to exactly one employee |
37 | | -for s in shifts.values(): |
| 51 | +for s in shift_to_int.values(): |
38 | 52 | model.addCons(sum(x[e, s] for e in employees) == 1) |
39 | 53 |
|
40 | 54 | # Employees can only work shifts they are available for |
41 | 55 | for e in employees: |
42 | | - for s in shifts.values(): |
| 56 | + for s in shift_to_int.values(): |
43 | 57 | if s not in availability_int[e]: |
44 | 58 | model.addCons(x[e, s] == 0) |
45 | 59 |
|
46 | 60 | # Minimize shift assignment cost |
47 | 61 | model.setObjective( |
48 | | - sum(cost[e][s]*x[e, s] for e in employees for s in shifts.values()), "minimize" |
| 62 | + sum(cost[e][s]*x[e, s] for e in employees for s in shift_to_int.values()), "minimize" |
49 | 63 | ) |
50 | 64 |
|
51 | 65 | # Solve the problem |
|
54 | 68 | # Display the results |
55 | 69 | print("\nOptimal Shift Assignment:") |
56 | 70 | for e in employees: |
57 | | - for s, s_id in shifts.items(): |
| 71 | + for s, s_id in shift_to_int.items(): |
58 | 72 | if model.getVal(x[e, s_id]) > 0.5: |
59 | 73 | print("%s is assigned to %s" % (e, s)) |
0 commit comments