Consider the following constraint in the Timefold documentation (see this page): def too_many_visits(factory: ConstraintFactory) -> Constraint: return (factory.for_each(Visit) .group_by(lambda visit: visit.vehicle, ConstraintCollectors.count()) .filter(lambda vehicle, visit_count: visit_count > 10) .penalize(HardSoftScore.ONE_HARD, lambda vehicle, visit_count: visit_count - 10) .as_constraint("too_many_visits"); ) After the group_by, a filter is applied to vehicle, visit_count pairs. I wonder how to set this up so that I can apply a filter to visit, vehicle, visit_count triples, where the vehicle is visit.vehicle and visit_count is the amount of vehicle visits? This sounds like it should be pretty easy to do, but I haven't figured out a way to do it yet. Continue reading...