use of org.cpsolver.ifs.model.LazySwap in project cpsolver by UniTime.
the class ExamPeriodSwapMove method selectNeighbour.
/**
* Select an exam randomly,
* select an available period randomly (if it is not assigned),
* use rooms if possible, select rooms using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)} if not (exam is unassigned, a room is not available or used).
*/
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
ExamModel model = (ExamModel) solution.getModel();
Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
Exam x1 = ToolBox.random(model.variables());
ExamPlacement v1 = assignment.getValue(x1);
if (v1 == null)
return null;
int x = ToolBox.random(model.variables().size());
for (int v = 0; v < model.variables().size(); v++) {
Exam x2 = model.variables().get((v + x) % (model.variables().size()));
ExamPlacement v2 = assignment.getValue(x2);
if (x1.equals(x2) || v2 == null)
continue;
ExamPeriodPlacement p1 = x1.getPeriodPlacement(v2.getPeriod());
ExamPeriodPlacement p2 = x2.getPeriodPlacement(v1.getPeriod());
if (p1 == null || p2 == null)
continue;
if (iCheckStudentConflicts && (x1.countStudentConflicts(assignment, p1) > 0 || x2.countStudentConflicts(assignment, p2) > 0))
continue;
if (iCheckDistributionConstraints) {
Map<Exam, ExamPlacement> placements = new HashMap<Exam, ExamPlacement>();
placements.put(x1, new ExamPlacement(x1, p1, new HashSet<ExamRoomPlacement>()));
placements.put(x2, new ExamPlacement(x2, p2, new HashSet<ExamRoomPlacement>()));
if (!checkDistributionConstraints(assignment, x1, p1, placements) || !checkDistributionConstraints(assignment, x2, p2, placements))
continue;
}
Set<ExamPlacement> conflicts = new HashSet<ExamPlacement>();
conflicts.add(v1);
conflicts.add(v2);
Map<Exam, ExamPlacement> placements = new HashMap<Exam, ExamPlacement>();
Set<ExamRoomPlacement> r1 = findBestAvailableRooms(assignment, x1, p1, conflicts, placements);
if (r1 == null)
continue;
placements.put(x1, new ExamPlacement(x1, p1, r1));
Set<ExamRoomPlacement> r2 = findBestAvailableRooms(assignment, x2, p2, conflicts, placements);
if (r2 == null)
continue;
return new LazySwap<Exam, ExamPlacement>(new ExamPlacement(x1, p1, r1), new ExamPlacement(x2, p2, r2));
}
return null;
}
Aggregations