Skip to content

Commit cbcaac5

Browse files
Christopher-Chianellitriceo
authored andcommitted
docs: Fix constraint example in the README
1 parent b14b306 commit cbcaac5

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,28 @@ You define your constraints by using the ConstraintFactory:
104104

105105
```python
106106
from domain import Lesson
107-
from timefold.solver.score import Joiners, HardSoftScore, constraint_provider
107+
from timefold.solver.score import (Joiners, HardSoftScore, ConstraintFactory,
108+
Constraint, constraint_provider)
108109

109110
@constraint_provider
110-
def define_constraints(constraint_factory):
111+
def define_constraints(constraint_factory: ConstraintFactory) -> list[Constraint]:
111112
return [
112113
# Hard constraints
113114
room_conflict(constraint_factory),
114115
# Other constraints here...
115116
]
116117

117-
def room_conflict(constraint_factory):
118+
def room_conflict(constraint_factory: ConstraintFactory) -> Constraint:
118119
# A room can accommodate at most one lesson at the same time.
119-
return constraint_factory.for_each_unique_pair(Lesson,
120+
return (
121+
constraint_factory.for_each_unique_pair(Lesson,
120122
# ... in the same timeslot ...
121123
Joiners.equal(lambda lesson: lesson.timeslot),
122124
# ... in the same room ...
123-
Joiners.equal(lambda lesson: lesson.room)) \
124-
.penalize("Room conflict", HardSoftScore.ONE_HARD)
125+
Joiners.equal(lambda lesson: lesson.room))
126+
.penalize(HardSoftScore.ONE_HARD)
127+
.as_constraint("Room conflict")
128+
)
125129
```
126130
for more details on Constraint Streams,
127131
see https://timefold.ai/docs/timefold-solver/latest/constraints-and-score/score-calculation.

0 commit comments

Comments
 (0)