问题
it's my first post here and I'm posting for my team of learning Java programmer for an school project. So we aren't good programmer. how can I add a constraint to make the Optaplanner move the lectures into a specific room type. For example I want to move the lecture "Programming" into a computerroom and the lecture "Math" in a normal room. But with my constraint it declares the hard constraints but doesn't move the lectures in their room. This should be a negative constraint. So the negative score for this hard constraint is shown, but it still won't be resolved/moved.
Here's the constraint:
rule "computerroom"
when
$room : Room($computerroom : computerroom)
$course : Course(computerroom == $computerroom)
then
scoreHolder.addHardConstraintMatch(kcontext, -1);
end
We added a new variable to the class Room, that look like:
package org.optaplanner.examples.curriculumcourse.domain;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.optaplanner.examples.common.domain.AbstractPersistable;
@XStreamAlias("Room")
public class Room extends AbstractPersistable {
private String code;
private int capacity;
private boolean computerroom;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public boolean getComputerroom() {
return computerroom;
}
public void setComputerroom(boolean computerroom) {
this.computerroom = computerroom;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public String getLabel() {
return code;
}
@Override
public String toString() {
return code + " {C" + capacity + "}";
}
}
So have u got an advise for us?
Another little thing: the Optaplanner gets a lot of data to work with: How can we increase the maximun seconds spend for resolving? Because we tried other values over 1152 but it does't work. It's only a little question, the constraint is more important, but we would be happy to get the program running!
Greets SEP2014
回答1:
That score rule isn't checking anything that is "planning variable" (= a variable that changes during solving). So it's static, it can never have a different score than with what it starts out with.
Consider this instead:
rule "computerroom"
when
// If there's a room which has no computer
$r : Room(hasComputer == false)
// And we're putting a course which needs a computer in that room
$course : Course(needsComputer == true, room == $r)
then
scoreHolder.addHardConstraintMatch(kcontext, -1);
end
The important thing here is Course(..., room == $r)
. That room
is a planning variable: it changes during planning.
Note: this one doesn't punish courses that don't need a computer if they use a computer room (but that's easily changed).
Same rule, but written shorter and more efficiently:
rule "computerroom"
when
$course : Course(needsComputer == true, room != null, room.hasComputer == false)
then
scoreHolder.addHardConstraintMatch(kcontext, -1);
end
来源:https://stackoverflow.com/questions/25787865/optaplanner-add-computerroom-restriction