Skip to content

Commit

Permalink
chore: Use HardSoftScore.parse, make Employee attributes optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher-Chianelli authored and triceo committed Jun 19, 2024
1 parent 6238ea1 commit 77ca693
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
13 changes: 5 additions & 8 deletions python/employee-scheduling/src/employee_scheduling/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ def validate_score(v: Any, info: ValidationInfo) -> Any:
if isinstance(v, HardSoftScore) or v is None:
return v
if isinstance(v, str):
hard_part, soft_part = v.split('/')
hard = int(hard_part.rstrip('hard'))
soft = int(soft_part.rstrip('soft'))
return HardSoftScore.of(hard, soft)
return HardSoftScore.parse(v)
raise ValueError('"score" should be a string')


Expand All @@ -34,10 +31,10 @@ class BaseSchema(BaseModel):

class Employee(BaseSchema):
name: Annotated[str, PlanningId]
skills: set[str]
unavailable_dates: set[date]
undesired_dates: set[date]
desired_dates: set[date]
skills: Annotated[set[str], Field(default_factory=set)]
unavailable_dates: Annotated[set[date], Field(default_factory=set)]
undesired_dates: Annotated[set[date], Field(default_factory=set)]
desired_dates: Annotated[set[date], Field(default_factory=set)]

This comment has been minimized.

Copy link
@ge0ffrey

ge0ffrey Jun 19, 2024

Contributor

@Christopher-Chianelli without this, it does run for me in the UI when I click the solve button. Do we need this?

This comment has been minimized.

Copy link
@Christopher-Chianelli

Christopher-Chianelli Jun 19, 2024

Author Contributor

It makes it so skills, unavailable_dates, undesired_dates, and desired_dates are all optional. It removes boilerplate when users create their own dataset or tests.



@planning_entity
Expand Down
28 changes: 14 additions & 14 deletions python/employee-scheduling/tests/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@


def test_required_skill():
employee = Employee(name="Amy", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee = Employee(name="Amy")
(constraint_verifier.verify_that(required_skill)
.given(employee,
Shift(id="1", start=DAY_START_TIME, end=DAY_END_TIME, location="Location", required_skill="Skill", employee=employee))
.penalizes(1))

employee = Employee(name="Beth", skills={"Skill"}, unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee = Employee(name="Beth", skills={"Skill"})
(constraint_verifier.verify_that(required_skill)
.given(employee,
Shift(id="2", start=DAY_START_TIME, end=DAY_END_TIME, location="Location", required_skill="Skill", employee=employee))
.penalizes(0))


def test_overlapping_shifts():
employee1 = Employee(name="Amy", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee2 = Employee(name="Beth", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee1 = Employee(name="Amy")
employee2 = Employee(name="Beth")
(constraint_verifier.verify_that(no_overlapping_shifts)
.given(employee1, employee2,
Shift(id="1", start=DAY_START_TIME, end=DAY_END_TIME, location="Location", required_skill="Skill", employee=employee1),
Expand All @@ -51,8 +51,8 @@ def test_overlapping_shifts():


def test_one_shift_per_day():
employee1 = Employee(name="Amy", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee2 = Employee(name="Beth", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee1 = Employee(name="Amy")
employee2 = Employee(name="Beth")
(constraint_verifier.verify_that(no_overlapping_shifts)
.given(employee1, employee2,
Shift(id="1", start=DAY_START_TIME, end=DAY_END_TIME, location="Location", required_skill="Skill", employee=employee1),
Expand All @@ -79,8 +79,8 @@ def test_one_shift_per_day():


def test_at_least_10_hours_between_shifts():
employee1 = Employee(name="Amy", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee2 = Employee(name="Beth", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee1 = Employee(name="Amy")
employee2 = Employee(name="Beth")

(constraint_verifier.verify_that(at_least_10_hours_between_two_shifts)
.given(employee1, employee2,
Expand Down Expand Up @@ -114,8 +114,8 @@ def test_at_least_10_hours_between_shifts():


def test_unavailable_employee():
employee1 = Employee(name="Amy", skills=set(), unavailable_dates={DAY_1}, undesired_dates=set(), desired_dates=set())
employee2 = Employee(name="Beth", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee1 = Employee(name="Amy", unavailable_dates={DAY_1})
employee2 = Employee(name="Beth")

(constraint_verifier.verify_that(unavailable_employee)
.given(employee1, employee2,
Expand All @@ -139,8 +139,8 @@ def test_unavailable_employee():


def test_undesired_day_for_employee():
employee1 = Employee(name="Amy", skills=set(), unavailable_dates=set(), undesired_dates={DAY_1}, desired_dates=set())
employee2 = Employee(name="Beth", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee1 = Employee(name="Amy", undesired_dates={DAY_1})
employee2 = Employee(name="Beth")

(constraint_verifier.verify_that(undesired_day_for_employee)
.given(employee1, employee2,
Expand All @@ -164,8 +164,8 @@ def test_undesired_day_for_employee():


def test_desired_day_for_employee():
employee1 = Employee(name="Amy", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates={DAY_1})
employee2 = Employee(name="Beth", skills=set(), unavailable_dates=set(), undesired_dates=set(), desired_dates=set())
employee1 = Employee(name="Amy", desired_dates={DAY_1})
employee2 = Employee(name="Beth")

(constraint_verifier.verify_that(desired_day_for_employee)
.given(employee1, employee2,
Expand Down
5 changes: 1 addition & 4 deletions python/school-timetabling/src/school_timetabling/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ def validate_score(v: Any, info: ValidationInfo) -> Any:
if isinstance(v, HardSoftScore) or v is None:
return v
if isinstance(v, str):
hard_part, soft_part = v.split('/')
hard = int(hard_part.rstrip('hard'))
soft = int(soft_part.rstrip('soft'))
return HardSoftScore.of(hard, soft)
return HardSoftScore.parse(v)
raise ValueError('"score" should be a string')


Expand Down

0 comments on commit 77ca693

Please sign in to comment.