Search in sources :

Example 1 with LazySwap

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;
}
Also used : HashMap(java.util.HashMap) ExamModel(org.cpsolver.exam.model.ExamModel) ExamDistributionConstraint(org.cpsolver.exam.model.ExamDistributionConstraint) LazySwap(org.cpsolver.ifs.model.LazySwap) ExamRoomPlacement(org.cpsolver.exam.model.ExamRoomPlacement) ExamPlacement(org.cpsolver.exam.model.ExamPlacement) ExamPeriodPlacement(org.cpsolver.exam.model.ExamPeriodPlacement) Exam(org.cpsolver.exam.model.Exam) HashSet(java.util.HashSet)

Aggregations

HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Exam (org.cpsolver.exam.model.Exam)1 ExamDistributionConstraint (org.cpsolver.exam.model.ExamDistributionConstraint)1 ExamModel (org.cpsolver.exam.model.ExamModel)1 ExamPeriodPlacement (org.cpsolver.exam.model.ExamPeriodPlacement)1 ExamPlacement (org.cpsolver.exam.model.ExamPlacement)1 ExamRoomPlacement (org.cpsolver.exam.model.ExamRoomPlacement)1 LazySwap (org.cpsolver.ifs.model.LazySwap)1