[英]Optaplanner - see if the same item appears in two lists
我目前正在嘗試制定考試時間表計划。 我知道了,所以考試被分配到一個時間段,但是學生沖突部分不起作用。 我目前將我的學生作為主題對象的屬性列在列表中。 每個考試還有一個 subject 屬性,它是 subject 類的一個實例。 我遇到的問題是,在比較學生名單時,據說沒有沖突,因為名單不相同。 但是我需要它來搜索列表並查看兩者中是否出現相同的字符串。 我對此很陌生,所以如果有人能找到更好的方法來構建我的問題或解決字符串中的問題,我將不勝感激。
private Constraint studentConflict(ConstraintFactory constraintFactory){
// students can only sit one exam at the same time
// selects a lesson and pairs it with another one
return constraintFactory
.forEach(Exam.class)
.join(Exam.class,
//...in the same timeslot ...
Joiners.equal(Exam::getTimeslot),
//...has the same students ...
Joiners.equal(Exam::getStudentstoString))
.penalize(HardSoftScore.ONE_HARD)
.asConstraint("Student conflict");
}
輸出部分:
您需要添加一個過濾器。
private Constraint studentConflict(ConstraintFactory constraintFactory){
// students can only sit one exam at the same time
// selects a lesson and pairs it with another one
return constraintFactory.forEach(Exam.class)
.join(Exam.class,
Joiners.equal(Exam::getTimeslot))
.filter((exam1, exam2) -> {
// call any code that compares the two lists and returns true on overlap
})
.penalize(HardSoftScore.ONE_HARD)
.asConstraint("Student conflict");
}
這是您可以使用當前模型做的最好的事情。 但是,它可能不是很有效。 每次評估Exam
時,都會執行過濾器,並且將對列表進行整體比較。
我們可以做得更好。 假設Student
是一個問題事實或一個規划實體,您可以這樣做,這將更加增量:
private Constraint studentConflict(ConstraintFactory constraintFactory){
// students can only sit one exam at the same time
// selects a lesson and pairs it with another one
return constraintFactory.forEachUniquePair(Exam.class,
Joiners.equal(Exam::getTimeslot))
.ifExists(Student.class,
Joiners.filtering((exam1, exam2, student) ->
exam1.getStudents().contains(student) &&
exam2.getStudents().contains(student))
.penalize(HardSoftScore.ONE_HARD)
.asConstraint("Student conflict");
}
不用說,為了盡可能高效, getStudents()
應該返回一個Set
,而不是一個List
。 (或者列表應該非常小。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.