@@ -104,24 +104,28 @@ You define your constraints by using the ConstraintFactory:
104
104
105
105
``` python
106
106
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)
108
109
109
110
@constraint_provider
110
- def define_constraints (constraint_factory ) :
111
+ def define_constraints (constraint_factory : ConstraintFactory) -> list[Constraint] :
111
112
return [
112
113
# Hard constraints
113
114
room_conflict(constraint_factory),
114
115
# Other constraints here...
115
116
]
116
117
117
- def room_conflict (constraint_factory ) :
118
+ def room_conflict (constraint_factory : ConstraintFactory) -> Constraint :
118
119
# 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,
120
122
# ... in the same timeslot ...
121
123
Joiners.equal(lambda lesson : lesson.timeslot),
122
124
# ... 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
+ )
125
129
```
126
130
for more details on Constraint Streams,
127
131
see https://timefold.ai/docs/timefold-solver/latest/constraints-and-score/score-calculation .
0 commit comments