Skip to content
Merged
Changes from all 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
16 changes: 5 additions & 11 deletions src/cp/objectives/maximize_wishes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import cast

from ortools.sat.python.cp_model import CpModel, IntVar, LinearExpr
from ortools.sat.python.cp_model import CpModel, LinearExpr

from ..variables import EmployeeWorksOnDayVariables, ShiftAssignmentVariables
from ..variables import EmployeeWorksOnDayVariables, ShiftAssignmentVariables, Variable
from .objective import Objective


Expand All @@ -17,18 +17,15 @@ def create(
shift_assignment_variables: ShiftAssignmentVariables,
employee_works_on_day_variables: EmployeeWorksOnDayVariables,
) -> LinearExpr:
penalties: list[IntVar] = []
penalties: list[Variable] = []

for employee in self._employees:
# Wish to have specific days off
for wish_day in employee.get_wish_days:
for day in self._days:
if day.day == wish_day:
var = employee_works_on_day_variables[employee][day]
penalty = model.NewBoolVar(f"penalty_on_assigned_wish_day_off_{employee.get_key()}_{day}")
model.Add(penalty == 1).OnlyEnforceIf(var)
model.Add(penalty == 0).OnlyEnforceIf(var.Not())
penalties.append(penalty)
penalties.append(var)

# Wish to have specific shifts off
for _, abbr in employee.get_wish_shifts:
Expand All @@ -38,9 +35,6 @@ def create(
continue

var = shift_assignment_variables[employee][day][shift]
penalty = model.NewBoolVar(f"penalty_on_assigned_wish_shift_off_{employee.get_key()}_{day}_{abbr}")
model.Add(penalty == 1).OnlyEnforceIf(var)
model.Add(penalty == 0).OnlyEnforceIf(var.Not())
penalties.append(penalty)
penalties.append(var)

return cast(LinearExpr, sum(penalties)) * self.weight
Loading