Skip to content

Commit 122bce7

Browse files
committed
Added categorical data example
1 parent 3b4eb05 commit 122bce7

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
### Added
5+
- Added categorical data example
56
- Added printProblem to print problem to stdout
67
- Added stage checks to presolve, freereoptsolve, freetransform
78
- Added primal_dual_evolution recipe and a plot recipe
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from pyscipopt import Model
2+
3+
# 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+
}
13+
14+
# Employee availability
15+
availability = {
16+
"Alice": ["Morning", "Afternoon"],
17+
"Bob": ["Afternoon", "Night"],
18+
"Charlie": ["Morning", "Night"]
19+
}
20+
21+
# 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()
25+
}
26+
27+
# Create the model
28+
model = Model("Shift Assignment")
29+
30+
# x[e, s] = 1 if employee e is assigned to shift s
31+
x = {}
32+
for e in employees:
33+
for s in shifts.values():
34+
x[e, s] = model.addVar(vtype="B", name=f"x({e},{s})")
35+
36+
# Each shift must be assigned to exactly one employee
37+
for s in shifts.values():
38+
model.addCons(sum(x[e, s] for e in employees) == 1)
39+
40+
# Employees can only work shifts they are available for
41+
for e in employees:
42+
for s in shifts.values():
43+
if s not in availability_int[e]:
44+
model.addCons(x[e, s] == 0)
45+
46+
# Minimize shift assignment cost
47+
model.setObjective(
48+
sum(cost[e][s]*x[e, s] for e in employees for s in shifts.values()), "minimize"
49+
)
50+
51+
# Solve the problem
52+
model.optimize()
53+
54+
# Display the results
55+
print("\nOptimal Shift Assignment:")
56+
for e in employees:
57+
for s, s_id in shifts.items():
58+
if model.getVal(x[e, s_id]) > 0.5:
59+
print("%s is assigned to %s" % (e, s))

0 commit comments

Comments
 (0)