    001package org.cpsolver.exam.neighbours;
    002
    003import java.util.Set;
    004
    005import org.cpsolver.exam.model.Exam;
    006import org.cpsolver.exam.model.ExamModel;
    007import org.cpsolver.exam.model.ExamPeriodPlacement;
    008import org.cpsolver.exam.model.ExamPlacement;
    009import org.cpsolver.exam.model.ExamRoomPlacement;
    010import org.cpsolver.ifs.assignment.Assignment;
    011import org.cpsolver.ifs.heuristics.NeighbourSelection;
    012import org.cpsolver.ifs.model.Neighbour;
    013import org.cpsolver.ifs.solution.Solution;
    014import org.cpsolver.ifs.solver.Solver;
    015import org.cpsolver.ifs.util.DataProperties;
    016import org.cpsolver.ifs.util.ToolBox;
    017
    018
    019/**
    020 * A new period is selected for a randomly selected exam. It tries to use the
    021 * current set of rooms, if it is possible (exam is assigned, rooms are
    022 * available and not used during the new period). Otherwise, rooms are selected
    023 * using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)}. <br>
    024 * <br>
    025 *
    026 * @author  Tomáš Müller
    027 * @version ExamTT 1.3 (Examination Timetabling)<br>
    028 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    029 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    030 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    031 * <br>
    032 *          This library is free software; you can redistribute it and/or modify
    033 *          it under the terms of the GNU Lesser General Public License as
    034 *          published by the Free Software Foundation; either version 3 of the
    035 *          License, or (at your option) any later version. <br>
    036 * <br>
    037 *          This library is distributed in the hope that it will be useful, but
    038 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    039 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    040 *          Lesser General Public License for more details. <br>
    041 * <br>
    042 *          You should have received a copy of the GNU Lesser General Public
    043 *          License along with this library; if not see
    044 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    045 */
    046public class ExamTimeMove implements NeighbourSelection<Exam,ExamPlacement> {
    047    private boolean iCheckStudentConflicts = false;
    048    private boolean iCheckDistributionConstraints = true;
    049
    050    /**
    051     * Constructor
    052     * @param properties problem properties
    053     */
    054    public ExamTimeMove(DataProperties properties) {
    055        iCheckStudentConflicts = properties.getPropertyBoolean("ExamTimeMove.CheckStudentConflicts", iCheckStudentConflicts);
    056        iCheckDistributionConstraints = properties.getPropertyBoolean("ExamTimeMove.CheckDistributionConstraints", iCheckDistributionConstraints);
    057    }
    058
    059    /**
    060     * Initialization
    061     */
    062    @Override
    063    public void init(Solver<Exam,ExamPlacement> solver) {}
    064
    065    /**
    066     * Select an exam randomly,
    067     * select an available period randomly (if it is not assigned),
    068     * use rooms if possible, select rooms using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)} if not (exam is unassigned, a room is not available or used).
    069     */
    070    @Override
    071    public Neighbour<Exam,ExamPlacement> selectNeighbour(Solution<Exam,ExamPlacement> solution) {
    072        ExamModel model = (ExamModel)solution.getModel();
    073        Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
    074        Exam exam = ToolBox.random(model.variables());
    075        ExamPlacement placement = assignment.getValue(exam);
    076        int px = ToolBox.random(exam.getPeriodPlacements().size());
    077        for (int p=0;p<exam.getPeriodPlacements().size();p++) {
    078            ExamPeriodPlacement period = exam.getPeriodPlacements().get((p+px)%exam.getPeriodPlacements().size());
    079            if (placement!=null && placement.getPeriod().equals(period.getPeriod())) continue;
    080            if (iCheckStudentConflicts && exam.countStudentConflicts(assignment, period)>0) continue;
    081            if (iCheckDistributionConstraints && !exam.checkDistributionConstraints(assignment, period)) continue;
    082            if (placement!=null) {
    083                boolean ok = true;
    084                for (ExamRoomPlacement room: placement.getRoomPlacements())
    085                    if (!room.isAvailable(period.getPeriod()) || room.getRoom().inConflict(assignment, exam, period.getPeriod())) {
    086                        ok = false; break;
    087                    }
    088                if (ok)
    089                    return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, placement.getRoomPlacements()));
    090            }
    091            Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period);
    092            if (rooms==null) continue;
    093            return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms));
    094        }
    095        return null;
    096    }
    097}