private Constraint roomConflict(ConstraintFactory constraintFactory) {
// A room can accommodate at most one lesson at the same time.
// Select a lesson ...
return constraintFactory.from(Lesson.class)
// ... and pair it with another lesson ...
.join(Lesson.class,
// ... in the same timeslot ...
Joiners.equal(Lesson::getTimeslot),
// ... in the same room ...
Joiners.equal(Lesson::getRoom),
// ... and the pair is unique (different id, no reverse pairs)
Joiners.lessThan(Lesson::getId))
// then penalize each pair with a hard weight.
.penalize("Room conflict", HardSoftScore.ONE_HARD);
}
Lesson.class in java can be translated to classOf[Lesson], but scala does not support Lesson::getTimeslot, what to do?
Thanks
Stephen
def roomConflict(constraintFactory: ConstraintFactory): Constraint = {
constraintFactory.from(classOf[Lesson])
// ... and pair it with another lesson ...
.join[Lesson](classOf[Lesson],
// ... in the same timeslot ...
Joiners.equal((l: Lesson) => l.getTimeslot),
// ... in the same room ...
Joiners.equal((l: Lesson) => l.getRoom),
// ... and the pair is unique (different id, no reverse pairs)
Joiners.lessThan((l: Lesson) => l.getId))
// then penalize each pair with a hard weight.
.penalize("Room conflict", HardSoftScore.ONE_HARD)
}
[error] Joiners.lessThan((l: Lesson) => l.id))
def roomConflict(constraintFactory: ConstraintFactory): Constraint = {
constraintFactory.from(classOf[Lesson])
// ... and pair it with another lesson ...
.join[Lesson](classOf[Lesson],
// ... in the same timeslot ...
Joiners.equal((l: Lesson) => l.timeslot),
// ... in the same room ...
Joiners.equal((l: Lesson) => l.room),
// ... and the pair is unique (different id, no reverse pairs)
Joiners.lessThan((l: Lesson) => l.id.toString))
// then penalize each pair with a hard weight.
.penalize("Room conflict", HardSoftScore.ONE_HARD)
}
There are users using ConstraintStreams in Kotlin, for example:
https://twitter.com/fabOnTw/status/1271424534084816903
I'd love to see some scala examples too.
The trick is to understand the method signatures indeed.
inferred type arguments [mytimetable.Lesson,Long] do not conform to method lessThan's type parameter bounds [A,Property_ <: Comparable[Property_]]
It seems to say that Scala's Long doesn't implement Comparable ?!?
In any case, don't do id.toString(): no need to do that.
With kind regards,
Geoffrey De Smet
--
You received this message because you are subscribed to the Google Groups "OptaPlanner development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to optaplanner-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/optaplanner-dev/11aaf0d2-056b-40e2-af0d-5f13b0746d61o%40googlegroups.com.
def skaterSkateAtMostThreeSessionsADay(constraintFactory: ConstraintFactory): Constraint = {
import java.time.DayOfWeek
constraintFactory.from(classOf[Session])
.groupBy[Tuple2[Skater,DayOfWeek]]((l:Session) => (l.skaternth.skater,l.timeslot.dayOfWeek),count[Tuple2[Skater,DayOfWeek]]())
.filter((skater:Tuple2[Skater,DayOfWeek],count:Int) => count>3)
.penalize("skaterSkateAtMostThriceADay", HardSoftScore.ONE_HARD)
}
To unsubscribe from this group and stop receiving emails from it, send an email to optapla...@googlegroups.com.
def skaterSkateAtMostThreeSessionsADay(constraintFactory: ConstraintFactory): Constraint = {
import java.time.DayOfWeek
constraintFactory.from(classOf[Session])
.groupBy[Tuple2[Skater,DayOfWeek]]((l:Session) => (l.skaternth.skater,l.timeslot.dayOfWeek),count[Tuple2[Skater,DayOfWeek]]())
.filter((skater:Tuple2[Skater,DayOfWeek],count:Int) => count>3)
Lukáš Petrovický
He/Him/His
Principal Software Engineer, Business Automation
lukas.pe...@redhat.com IM: triceo/lpetrovi
val x: UniConstraintCollector[(Skater, DayOfWeek), _, Integer] = count[(Skater, DayOfWeek)]()
constraintFactory.from(classOf[Session])
.groupBy[Tuple2[Skater,DayOfWeek]]((l:Session) => (l.skaternth.skater,l.timeslot.dayOfWeek),x)
The groupBy is exactly where I am having trouble, compiler does not accept the second argument I pass to groupBy:
val x: UniConstraintCollector[Session, _, Integer] = count[Session]()
constraintFactory.from(classOf[Session])
.groupBy[Tuple2[Skater,DayOfWeek]]((l:Session) => (l.skaternth.skater,l.timeslot.dayOfWeek),x)
constraintFactory.from(classOf[Session])
.groupBy((l:Session) => (l.skaternth.skater,l.timeslot.dayOfWeek),count[Session]())
.filter((_,count) => count>3)
constraintFactory.from(classOf[Lesson]).join[Lesson](classOf[Lesson],
// ... and pair it with another lesson ...// ... in the same timeslot ...Joiners.equal((l: Lesson) => l.getTimeslot),// ... in the same room ...Joiners.equal((l: Lesson) => l.getRoom),// ... and the pair is unique (different id, no reverse pairs)Joiners.lessThan((l: Lesson) => l.getId))
But why would I need to pass type parameter in join below but can not pass type parameter in groupBy?