fix the weekend count for multishits

This commit is contained in:
Ross
2025-06-22 22:34:05 +01:00
parent 2443ac285b
commit 48d6b748d1
2 changed files with 37 additions and 23 deletions
+35 -21
View File
@@ -2145,6 +2145,20 @@ class RotaBuilder(object):
) )
for week in self.weeks: for week in self.weeks:
weekend_days = self.days[5:] # ["Sat", "Sun"]
total_assigned = sum(
self.model.works[worker.id, week, day, shiftname]
for w, day, shiftname in self.get_all_shiftname_combinations(week=week)
if day in weekend_days
)
# If any shift is assigned on Sat or Sun, works_weekend is 1
self.model.constraints.add(total_assigned >= self.model.works_weekend[worker.id, week])
# If works_weekend is 1, at least one shift must be assigned
self.model.constraints.add(total_assigned <= 1000 * self.model.works_weekend[worker.id, week])
for constraint_shift in self.get_shifts_with_constraints( for constraint_shift in self.get_shifts_with_constraints(
"max_shifts_per_week" "max_shifts_per_week"
): ):
@@ -2301,27 +2315,27 @@ class RotaBuilder(object):
# Try disabling tihs to allow more than 2 shifts ot be assigned on a weekend # Try disabling tihs to allow more than 2 shifts ot be assigned on a weekend
self.model.constraints.add( #self.model.constraints.add(
self.model.works_weekend[worker.id, week] # self.model.works_weekend[worker.id, week]
>= sum( # >= sum(
self.model.works[worker.id, week, day, shiftname] # self.model.works[worker.id, week, day, shiftname]
for w, day, shiftname in self.get_all_shiftname_combinations( # for w, day, shiftname in self.get_all_shiftname_combinations(
week=week # week=week
) # )
if day in self.days[5:] # if day in self.days[5:]
) # )
/ 2 # / 2
) #)
self.model.constraints.add( #self.model.constraints.add(
self.model.works_weekend[worker.id, week] # self.model.works_weekend[worker.id, week]
<= sum( # <= sum(
self.model.works[worker.id, week, day, shiftname] # self.model.works[worker.id, week, day, shiftname]
for w, day, shiftname in self.get_all_shiftname_combinations( # for w, day, shiftname in self.get_all_shiftname_combinations(
week=week # week=week
) # )
if day in self.days[5:] # if day in self.days[5:]
) # )
) #)
# self.model.constraints.add( # self.model.constraints.add(
# self.model.works_saturday[worker.id, week] == sum( # self.model.works_saturday[worker.id, week] == sum(
# self.model.works[worker.id, week, "Sat", shift.name] # self.model.works[worker.id, week, "Sat", shift.name]
+2 -2
View File
@@ -365,7 +365,7 @@ def test_pre_post_clear_force_assign(demo_rota_clear):
#], #],
), ),
) )
Rota.build_and_solve(options={"ratio": 0.0}) Rota.build_and_solve(options={"ratio": 0.01})
Rota.export_rota_to_html("test_pre_post_clear", folder="tests") Rota.export_rota_to_html("test_pre_post_clear", folder="tests")
assert Rota.results.solver.status == "ok" assert Rota.results.solver.status == "ok"
@@ -441,7 +441,7 @@ def test_pre_post_clear_force_assign2(demo_rota_clear):
], ],
), ),
) )
Rota.build_and_solve(options={"ratio": 0.0}) Rota.build_and_solve(options={"ratio": 0.01})
Rota.export_rota_to_html("test_pre_post_clear", folder="tests") Rota.export_rota_to_html("test_pre_post_clear", folder="tests")
assert Rota.results.solver.status == "ok" assert Rota.results.solver.status == "ok"