use of org.cpsolver.ifs.model.LazyNeighbour in project cpsolver by UniTime.
the class ExamSimulatedAnnealing method accept.
/**
* True if the given neighboir is to be be accepted
*
* @param solution
* current solution
* @param neighbour
* proposed move
* @return true if generated random number is below
* {@link ExamSimulatedAnnealing.Context#prob(double)}
*/
protected boolean accept(Solution<Exam, ExamPlacement> solution, Neighbour<Exam, ExamPlacement> neighbour) {
if (neighbour instanceof LazyNeighbour) {
((LazyNeighbour<Exam, ExamPlacement>) neighbour).setAcceptanceCriterion(this);
return true;
}
Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
double value = (iRelativeAcceptance ? neighbour.value(assignment) : solution.getModel().getTotalValue(assignment) + neighbour.value(assignment) - solution.getBestValue());
Context context = getContext(solution.getAssignment());
double prob = context.prob(value);
if (prob >= 1.0 || ToolBox.random() < prob) {
context.accepted(neighbour.value(assignment));
return true;
}
return false;
}
use of org.cpsolver.ifs.model.LazyNeighbour in project cpsolver by UniTime.
the class ExamHillClimbing method selectNeighbour.
/**
* Select one of the given neighbourhoods randomly, select neighbour, return
* it if its value is below or equal to zero (continue with the next
* selection otherwise). Return null when the given number of idle
* iterations is reached.
*/
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
Context context = getContext(solution.getAssignment());
context.activateIfNeeded();
while (true) {
if (context.incIter(solution))
break;
NeighbourSelection<Exam, ExamPlacement> ns = iNeighbours.get(ToolBox.random(iNeighbours.size()));
Neighbour<Exam, ExamPlacement> n = ns.selectNeighbour(solution);
if (n != null) {
if (n instanceof LazyNeighbour) {
((LazyNeighbour<Exam, ExamPlacement>) n).setAcceptanceCriterion(this);
return n;
} else if (n.value(solution.getAssignment()) <= 0.0)
return n;
}
}
context.reset();
return null;
}
Aggregations