File size: 22,440 Bytes
e7cf451 e510416 e7cf451 e510416 e7cf451 e510416 e7cf451 e510416 e7cf451 e510416 e7cf451 e510416 e7cf451 e510416 e7cf451 e510416 e7cf451 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
"""
Constraint tests for the employee scheduling quickstart.
Each constraint is tested with both penalizing and non-penalizing scenarios.
"""
from solverforge_legacy.solver.test import ConstraintVerifier
from employee_scheduling.domain import Employee, Shift, EmployeeSchedule
from employee_scheduling.constraints import (
define_constraints,
required_skill,
no_overlapping_shifts,
at_least_10_hours_between_two_shifts,
one_shift_per_day,
unavailable_employee,
undesired_day_for_employee,
desired_day_for_employee,
balance_employee_shift_assignments,
)
from datetime import date, datetime, time, timedelta
import pytest
# Test constants
DAY_1 = date(2021, 2, 1)
DAY_2 = date(2021, 2, 2)
DAY_3 = date(2021, 2, 3)
DAY_START_TIME = datetime.combine(DAY_1, time(9, 0))
DAY_END_TIME = datetime.combine(DAY_1, time(17, 0))
AFTERNOON_START_TIME = datetime.combine(DAY_1, time(13, 0))
AFTERNOON_END_TIME = datetime.combine(DAY_1, time(21, 0))
constraint_verifier = ConstraintVerifier.build(
define_constraints, EmployeeSchedule, Shift
)
# ========================================
# Required Skill Tests
# ========================================
class TestRequiredSkill:
"""Tests for the required_skill constraint."""
def test_penalized_when_employee_lacks_skill(self):
"""Employee without required skill should be penalized."""
employee = Employee(name="Amy") # No skills
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Driving",
employee=employee,
)
constraint_verifier.verify_that(required_skill).given(
employee, shift
).penalizes(1)
def test_not_penalized_when_employee_has_skill(self):
"""Employee with required skill should not be penalized."""
employee = Employee(name="Amy", skills={"Driving"})
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Driving",
employee=employee,
)
constraint_verifier.verify_that(required_skill).given(
employee, shift
).penalizes(0)
def test_not_penalized_when_employee_has_multiple_skills(self):
"""Employee with multiple skills including required should not be penalized."""
employee = Employee(name="Amy", skills={"Driving", "First Aid", "Cooking"})
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="First Aid",
employee=employee,
)
constraint_verifier.verify_that(required_skill).given(
employee, shift
).penalizes(0)
def test_penalized_when_employee_has_different_skills(self):
"""Employee with skills but not the required one should be penalized."""
employee = Employee(name="Amy", skills={"Cooking", "Cleaning"})
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Driving",
employee=employee,
)
constraint_verifier.verify_that(required_skill).given(
employee, shift
).penalizes(1)
# ========================================
# Overlapping Shifts Tests
# ========================================
class TestNoOverlappingShifts:
"""Tests for the no_overlapping_shifts constraint."""
def test_penalized_when_shifts_fully_overlap(self):
"""Same employee with fully overlapping shifts should be penalized."""
employee = Employee(name="Amy")
shift1 = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location A",
required_skill="Skill",
employee=employee,
)
shift2 = Shift(
id="2",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location B",
required_skill="Skill",
employee=employee,
)
# 8 hours overlap = 480 minutes
constraint_verifier.verify_that(no_overlapping_shifts).given(
employee, shift1, shift2
).penalizes_by(480)
def test_penalized_when_shifts_partially_overlap(self):
"""Same employee with partially overlapping shifts should be penalized by overlap duration."""
employee = Employee(name="Amy")
shift1 = Shift(
id="1",
start=DAY_START_TIME, # 9:00
end=DAY_END_TIME, # 17:00
location="Location A",
required_skill="Skill",
employee=employee,
)
shift2 = Shift(
id="2",
start=AFTERNOON_START_TIME, # 13:00
end=AFTERNOON_END_TIME, # 21:00
location="Location B",
required_skill="Skill",
employee=employee,
)
# Overlap from 13:00 to 17:00 = 4 hours = 240 minutes
constraint_verifier.verify_that(no_overlapping_shifts).given(
employee, shift1, shift2
).penalizes_by(240)
def test_not_penalized_when_different_employees(self):
"""Different employees with overlapping shifts should not be penalized."""
employee1 = Employee(name="Amy")
employee2 = Employee(name="Beth")
shift1 = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location A",
required_skill="Skill",
employee=employee1,
)
shift2 = Shift(
id="2",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location B",
required_skill="Skill",
employee=employee2,
)
constraint_verifier.verify_that(no_overlapping_shifts).given(
employee1, employee2, shift1, shift2
).penalizes(0)
def test_not_penalized_when_shifts_dont_overlap(self):
"""Same employee with non-overlapping shifts should not be penalized."""
employee = Employee(name="Amy")
shift1 = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location A",
required_skill="Skill",
employee=employee,
)
shift2 = Shift(
id="2",
start=DAY_START_TIME + timedelta(days=1),
end=DAY_END_TIME + timedelta(days=1),
location="Location B",
required_skill="Skill",
employee=employee,
)
constraint_verifier.verify_that(no_overlapping_shifts).given(
employee, shift1, shift2
).penalizes(0)
# ========================================
# One Shift Per Day Tests
# ========================================
class TestOneShiftPerDay:
"""Tests for the one_shift_per_day constraint."""
def test_penalized_when_two_shifts_same_day(self):
"""Employee with two shifts on same day should be penalized."""
employee = Employee(name="Amy")
shift1 = Shift(
id="1",
start=datetime.combine(DAY_1, time(6, 0)),
end=datetime.combine(DAY_1, time(10, 0)),
location="Location A",
required_skill="Skill",
employee=employee,
)
shift2 = Shift(
id="2",
start=datetime.combine(DAY_1, time(18, 0)),
end=datetime.combine(DAY_1, time(22, 0)),
location="Location B",
required_skill="Skill",
employee=employee,
)
constraint_verifier.verify_that(one_shift_per_day).given(
employee, shift1, shift2
).penalizes(1)
def test_not_penalized_when_shifts_different_days(self):
"""Employee with shifts on different days should not be penalized."""
employee = Employee(name="Amy")
shift1 = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location A",
required_skill="Skill",
employee=employee,
)
shift2 = Shift(
id="2",
start=DAY_START_TIME + timedelta(days=1),
end=DAY_END_TIME + timedelta(days=1),
location="Location B",
required_skill="Skill",
employee=employee,
)
constraint_verifier.verify_that(one_shift_per_day).given(
employee, shift1, shift2
).penalizes(0)
def test_not_penalized_when_different_employees_same_day(self):
"""Different employees with shifts on same day should not be penalized."""
employee1 = Employee(name="Amy")
employee2 = Employee(name="Beth")
shift1 = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location A",
required_skill="Skill",
employee=employee1,
)
shift2 = Shift(
id="2",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location B",
required_skill="Skill",
employee=employee2,
)
constraint_verifier.verify_that(one_shift_per_day).given(
employee1, employee2, shift1, shift2
).penalizes(0)
# ========================================
# 10 Hours Between Shifts Tests
# ========================================
class TestAtLeast10HoursBetweenShifts:
"""Tests for the at_least_10_hours_between_two_shifts constraint."""
def test_penalized_when_less_than_10_hours_gap(self):
"""Employee with less than 10 hours between shifts should be penalized."""
employee = Employee(name="Amy")
shift1 = Shift(
id="1",
start=DAY_START_TIME, # 9:00
end=DAY_END_TIME, # 17:00
location="Location A",
required_skill="Skill",
employee=employee,
)
shift2 = Shift(
id="2",
start=AFTERNOON_END_TIME, # 21:00 (4 hours after shift1 ends)
end=DAY_START_TIME + timedelta(days=1),
location="Location B",
required_skill="Skill",
employee=employee,
)
# Gap is 4 hours, need 10 hours, so 6 hours short = 360 minutes penalty
constraint_verifier.verify_that(at_least_10_hours_between_two_shifts).given(
employee, shift1, shift2
).penalizes_by(360)
def test_penalized_when_no_gap(self):
"""Back-to-back shifts should be penalized by full 600 minutes."""
employee = Employee(name="Amy")
shift1 = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location A",
required_skill="Skill",
employee=employee,
)
shift2 = Shift(
id="2",
start=DAY_END_TIME, # Starts exactly when shift1 ends
end=DAY_START_TIME + timedelta(days=1),
location="Location B",
required_skill="Skill",
employee=employee,
)
constraint_verifier.verify_that(at_least_10_hours_between_two_shifts).given(
employee, shift1, shift2
).penalizes_by(600)
def test_not_penalized_when_exactly_10_hours_gap(self):
"""Employee with exactly 10 hours between shifts should not be penalized."""
employee = Employee(name="Amy")
shift1 = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME, # 17:00
location="Location A",
required_skill="Skill",
employee=employee,
)
shift2 = Shift(
id="2",
start=DAY_END_TIME + timedelta(hours=10), # 03:00 next day
end=DAY_START_TIME + timedelta(days=1),
location="Location B",
required_skill="Skill",
employee=employee,
)
constraint_verifier.verify_that(at_least_10_hours_between_two_shifts).given(
employee, shift1, shift2
).penalizes(0)
def test_not_penalized_when_different_employees(self):
"""Different employees with back-to-back shifts should not be penalized."""
employee1 = Employee(name="Amy")
employee2 = Employee(name="Beth")
shift1 = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location A",
required_skill="Skill",
employee=employee1,
)
shift2 = Shift(
id="2",
start=AFTERNOON_END_TIME,
end=DAY_START_TIME + timedelta(days=1),
location="Location B",
required_skill="Skill",
employee=employee2,
)
constraint_verifier.verify_that(at_least_10_hours_between_two_shifts).given(
employee1, employee2, shift1, shift2
).penalizes(0)
# ========================================
# Unavailable Employee Tests
# ========================================
class TestUnavailableEmployee:
"""Tests for the unavailable_employee constraint."""
def test_penalized_when_shift_on_unavailable_day(self):
"""Employee scheduled on unavailable day should be penalized by shift duration."""
employee = Employee(name="Amy", unavailable_dates={DAY_1})
shift = Shift(
id="1",
start=DAY_START_TIME, # DAY_1 at 9:00
end=DAY_END_TIME, # DAY_1 at 17:00
location="Location",
required_skill="Skill",
employee=employee,
)
# 8 hours = 480 minutes
constraint_verifier.verify_that(unavailable_employee).given(
employee, shift
).penalizes_by(480)
def test_penalized_proportionally_for_multi_day_shift(self):
"""Multi-day shift crossing unavailable day should be penalized proportionally."""
employee = Employee(name="Amy", unavailable_dates={DAY_1})
shift = Shift(
id="1",
start=DAY_START_TIME - timedelta(days=1), # Starts day before
end=DAY_END_TIME, # Ends on DAY_1
location="Location",
required_skill="Skill",
employee=employee,
)
# Overlap with DAY_1 is from midnight to 17:00 = 17 hours = 1020 minutes
constraint_verifier.verify_that(unavailable_employee).given(
employee, shift
).penalizes_by(1020)
def test_not_penalized_when_shift_on_different_day(self):
"""Employee scheduled on available day should not be penalized."""
employee = Employee(name="Amy", unavailable_dates={DAY_1})
shift = Shift(
id="1",
start=DAY_START_TIME + timedelta(days=1), # DAY_2
end=DAY_END_TIME + timedelta(days=1),
location="Location",
required_skill="Skill",
employee=employee,
)
constraint_verifier.verify_that(unavailable_employee).given(
employee, shift
).penalizes(0)
def test_not_penalized_when_different_employee(self):
"""Different employee (without unavailable dates) should not be penalized."""
employee1 = Employee(name="Amy", unavailable_dates={DAY_1})
employee2 = Employee(name="Beth") # No unavailable dates
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Skill",
employee=employee2,
)
constraint_verifier.verify_that(unavailable_employee).given(
employee1, employee2, shift
).penalizes(0)
def test_penalized_for_multiple_unavailable_days(self):
"""Shift crossing multiple unavailable days should be penalized for both."""
employee = Employee(name="Amy", unavailable_dates={DAY_1, DAY_3})
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Skill",
employee=employee,
)
# Only DAY_1 overlaps (DAY_3 is 2 days later)
constraint_verifier.verify_that(unavailable_employee).given(
employee, shift
).penalizes_by(480)
# ========================================
# Undesired Day Tests
# ========================================
class TestUndesiredDayForEmployee:
"""Tests for the undesired_day_for_employee constraint (soft)."""
def test_penalized_when_shift_on_undesired_day(self):
"""Employee scheduled on undesired day should be penalized."""
employee = Employee(name="Amy", undesired_dates={DAY_1})
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Skill",
employee=employee,
)
constraint_verifier.verify_that(undesired_day_for_employee).given(
employee, shift
).penalizes_by(480)
def test_not_penalized_when_shift_on_different_day(self):
"""Employee scheduled on non-undesired day should not be penalized."""
employee = Employee(name="Amy", undesired_dates={DAY_1})
shift = Shift(
id="1",
start=DAY_START_TIME + timedelta(days=1),
end=DAY_END_TIME + timedelta(days=1),
location="Location",
required_skill="Skill",
employee=employee,
)
constraint_verifier.verify_that(undesired_day_for_employee).given(
employee, shift
).penalizes(0)
def test_not_penalized_when_different_employee(self):
"""Different employee without undesired dates should not be penalized."""
employee1 = Employee(name="Amy", undesired_dates={DAY_1})
employee2 = Employee(name="Beth")
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Skill",
employee=employee2,
)
constraint_verifier.verify_that(undesired_day_for_employee).given(
employee1, employee2, shift
).penalizes(0)
# ========================================
# Desired Day Tests
# ========================================
class TestDesiredDayForEmployee:
"""Tests for the desired_day_for_employee constraint (soft reward)."""
def test_rewarded_when_shift_on_desired_day(self):
"""Employee scheduled on desired day should be rewarded."""
employee = Employee(name="Amy", desired_dates={DAY_1})
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Skill",
employee=employee,
)
constraint_verifier.verify_that(desired_day_for_employee).given(
employee, shift
).rewards_with(480)
def test_not_rewarded_when_shift_on_different_day(self):
"""Employee scheduled on non-desired day should not be rewarded."""
employee = Employee(name="Amy", desired_dates={DAY_1})
shift = Shift(
id="1",
start=DAY_START_TIME + timedelta(days=1),
end=DAY_END_TIME + timedelta(days=1),
location="Location",
required_skill="Skill",
employee=employee,
)
constraint_verifier.verify_that(desired_day_for_employee).given(
employee, shift
).rewards(0)
def test_not_rewarded_when_different_employee(self):
"""Different employee without desired dates should not be rewarded."""
employee1 = Employee(name="Amy", desired_dates={DAY_1})
employee2 = Employee(name="Beth")
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Skill",
employee=employee2,
)
constraint_verifier.verify_that(desired_day_for_employee).given(
employee1, employee2, shift
).rewards(0)
# ========================================
# Balance Employee Shift Assignments Tests
# ========================================
class TestBalanceEmployeeShiftAssignments:
"""Tests for the balance_employee_shift_assignments constraint."""
def test_no_penalty_when_no_shifts(self):
"""No shifts assigned should have zero imbalance."""
employee1 = Employee(name="Amy")
employee2 = Employee(name="Beth")
constraint_verifier.verify_that(balance_employee_shift_assignments).given(
employee1, employee2
).penalizes_by(0)
def test_penalized_when_unbalanced(self):
"""Only one employee with shifts should be penalized (imbalanced)."""
employee1 = Employee(name="Amy")
employee2 = Employee(name="Beth")
shift = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Skill",
employee=employee1,
)
constraint_verifier.verify_that(balance_employee_shift_assignments).given(
employee1, employee2, shift
).penalizes_by_more_than(0)
def test_no_penalty_when_balanced(self):
"""Equal shifts per employee should have zero imbalance."""
employee1 = Employee(name="Amy")
employee2 = Employee(name="Beth")
shift1 = Shift(
id="1",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Skill",
employee=employee1,
)
shift2 = Shift(
id="2",
start=DAY_START_TIME,
end=DAY_END_TIME,
location="Location",
required_skill="Skill",
employee=employee2,
)
constraint_verifier.verify_that(balance_employee_shift_assignments).given(
employee1, employee2, shift1, shift2
).penalizes_by(0)
|