Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added categorical data example #932

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
### Added
- Added categorical data example
- Added printProblem to print problem to stdout
- Added stage checks to presolve, freereoptsolve, freetransform
- Added primal_dual_evolution recipe and a plot recipe
Expand Down
59 changes: 59 additions & 0 deletions examples/finished/categorical_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from pyscipopt import Model

Joao-Dionisio marked this conversation as resolved.
Show resolved Hide resolved
# Define categorical data
shifts = {"Morning": 0, "Afternoon": 1, "Night": 2}
employees = ["Alice", "Bob", "Charlie"]

# Employees have different salary demands
cost = {
"Alice": [2,4,1],
"Bob": [3,2,7],
"Charlie": [3,3,3]
}

# Employee availability
availability = {
"Alice": ["Morning", "Afternoon"],
"Bob": ["Afternoon", "Night"],
"Charlie": ["Morning", "Night"]
}

# Transform availability into integer values
availability_int = {
emp: [shifts[shift] for shift in available_shifts]
for emp, available_shifts in availability.items()
}

# Create the model
model = Model("Shift Assignment")

# x[e, s] = 1 if employee e is assigned to shift s
x = {}
for e in employees:
for s in shifts.values():
x[e, s] = model.addVar(vtype="B", name=f"x({e},{s})")

# Each shift must be assigned to exactly one employee
for s in shifts.values():
model.addCons(sum(x[e, s] for e in employees) == 1)

# Employees can only work shifts they are available for
for e in employees:
for s in shifts.values():
if s not in availability_int[e]:
model.addCons(x[e, s] == 0)

# Minimize shift assignment cost
model.setObjective(
sum(cost[e][s]*x[e, s] for e in employees for s in shifts.values()), "minimize"
)

# Solve the problem
model.optimize()

# Display the results
print("\nOptimal Shift Assignment:")
for e in employees:
for s, s_id in shifts.items():
if model.getVal(x[e, s_id]) > 0.5:
print("%s is assigned to %s" % (e, s))
Loading