    001package org.cpsolver.coursett.neighbourhoods;
    002
    003import java.util.List;
    004
    005import org.cpsolver.coursett.model.Lecture;
    006import org.cpsolver.coursett.model.Placement;
    007import org.cpsolver.coursett.model.RoomLocation;
    008import org.cpsolver.coursett.model.TimeLocation;
    009import org.cpsolver.coursett.model.TimetableModel;
    010import org.cpsolver.ifs.algorithms.neighbourhoods.RandomMove;
    011import org.cpsolver.ifs.assignment.Assignment;
    012import org.cpsolver.ifs.model.Neighbour;
    013import org.cpsolver.ifs.model.SimpleNeighbour;
    014import org.cpsolver.ifs.solution.Solution;
    015import org.cpsolver.ifs.util.DataProperties;
    016import org.cpsolver.ifs.util.ToolBox;
    017
    018
    019/**
    020 * Try to assign a class with a new time. A class is selected randomly, a
    021 * different (available) time is randomly selected for the class -- the class is
    022 * assigned into the new time if there is no conflict.
    023 * <br>
    024 *
    025 * @version IFS 1.3 (Iterative Forward Search)<br>
    026 *          Copyright (C) 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 TimeChange extends RandomMove<Lecture, Placement> {
    045
    046    public TimeChange(DataProperties config) {
    047        super(config);
    048    }
    049
    050    @Override
    051    public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
    052        TimetableModel model = (TimetableModel)solution.getModel();
    053        Assignment<Lecture, Placement> assignment = solution.getAssignment();
    054        int varIdx = ToolBox.random(model.variables().size());
    055        for (int i = 0; i < model.variables().size(); i++) {
    056            Lecture lecture = model.variables().get((i + varIdx) % model.variables().size());
    057            Placement old = assignment.getValue(lecture);
    058            if (old == null) continue;
    059            List<TimeLocation> values = lecture.timeLocations();
    060            if (values.isEmpty()) continue;
    061            int valIdx = ToolBox.random(values.size());
    062            for (int j = 0; j < values.size(); j++) {
    063                TimeLocation time = values.get((j + valIdx) % values.size());
    064                if (time.getPreference() > 50) continue;
    065
    066                Placement placement = null;
    067                if (lecture.getNrRooms() == 0)
    068                    placement = new Placement(lecture, time, (RoomLocation) null);
    069                else if (lecture.getNrRooms() == 1)
    070                    placement = new Placement(lecture, time, old.getRoomLocation());
    071                else
    072                    placement = new Placement(lecture, time, old.getRoomLocations());
    073
    074                if (placement.isValid() && !model.inConflict(assignment, placement)) {
    075                    SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
    076                    if (!iHC || n.value(assignment) <= 0) return n;
    077                }
    078            }
    079        }
    080        return null;
    081    }
    082
    083
    084}