    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 period is selected randomly for a randomly selected exam. Rooms are
    021 * computed using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)}. <br>
    022 * <br>
    023 *
    024 * @author  Tomáš Müller
    025 * @version ExamTT 1.3 (Examination Timetabling)<br>
    026 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    027 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    028 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    029 * <br>
    030 *          This library is free software; you can redistribute it and/or modify
    031 *          it under the terms of the GNU Lesser General Public License as
    032 *          published by the Free Software Foundation; either version 3 of the
    033 *          License, or (at your option) any later version. <br>
    034 * <br>
    035 *          This library is distributed in the hope that it will be useful, but
    036 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    037 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    038 *          Lesser General Public License for more details. <br>
    039 * <br>
    040 *          You should have received a copy of the GNU Lesser General Public
    041 *          License along with this library; if not see
    042 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    043 */
    044public class ExamRandomMove implements NeighbourSelection<Exam, ExamPlacement> {
    045    private boolean iCheckStudentConflicts = false;
    046    private boolean iCheckDistributionConstraints = true;
    047
    048    /**
    049     * Constructor
    050     *
    051     * @param properties
    052     *            problem properties
    053     */
    054    public ExamRandomMove(DataProperties properties) {
    055        iCheckStudentConflicts = properties.getPropertyBoolean("ExamRandomMove.CheckStudentConflicts", iCheckStudentConflicts);
    056        iCheckDistributionConstraints = properties.getPropertyBoolean("ExamRandomMove.CheckDistributionConstraints", iCheckDistributionConstraints);
    057    }
    058
    059    /**
    060     * Initialization
    061     */
    062    @Override
    063    public void init(Solver<Exam, ExamPlacement> solver) {
    064    }
    065
    066    /**
    067     * Select an exam randomly, select an available period randomly (from
    068     * {@link Exam#getPeriodPlacements()}), select rooms using
    069     * {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)}.
    070     */
    071    @Override
    072    public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
    073        ExamModel model = (ExamModel) solution.getModel();
    074        Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
    075        Exam exam = ToolBox.random(model.variables());
    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 (iCheckStudentConflicts && exam.countStudentConflicts(assignment, period) > 0)
    080                continue;
    081            if (iCheckDistributionConstraints && !exam.checkDistributionConstraints(assignment, period))
    082                continue;
    083            Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period);
    084            if (rooms == null)
    085                continue;
    086            return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms));
    087        }
    088        return null;
    089    }
    090}