    001package net.sf.cpsolver.coursett.neighbourhoods;
    002
    003import java.util.List;
    004
    005import net.sf.cpsolver.coursett.model.Lecture;
    006import net.sf.cpsolver.coursett.model.Placement;
    007import net.sf.cpsolver.coursett.model.RoomLocation;
    008import net.sf.cpsolver.coursett.model.TimeLocation;
    009import net.sf.cpsolver.coursett.model.TimetableModel;
    010import net.sf.cpsolver.ifs.algorithms.neighbourhoods.RandomMove;
    011import net.sf.cpsolver.ifs.model.Neighbour;
    012import net.sf.cpsolver.ifs.model.SimpleNeighbour;
    013import net.sf.cpsolver.ifs.solution.Solution;
    014import net.sf.cpsolver.ifs.util.DataProperties;
    015import net.sf.cpsolver.ifs.util.ToolBox;
    016
    017/**
    018 * Try to assign a class with a new time. A class is selected randomly, a
    019 * different (available) time is randomly selected for the class -- the class is
    020 * assigned into the new time if there is no conflict.
    021 * <br>
    022 *
    023 * @version IFS 1.2 (Iterative Forward Search)<br>
    024 *          Copyright (C) 2014 Tomáš Müller<br>
    025 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    026 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    027 * <br>
    028 *          This library is free software; you can redistribute it and/or modify
    029 *          it under the terms of the GNU Lesser General Public License as
    030 *          published by the Free Software Foundation; either version 3 of the
    031 *          License, or (at your option) any later version. <br>
    032 * <br>
    033 *          This library is distributed in the hope that it will be useful, but
    034 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    035 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    036 *          Lesser General Public License for more details. <br>
    037 * <br>
    038 *          You should have received a copy of the GNU Lesser General Public
    039 *          License along with this library; if not see
    040 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    041 */
    042public class TimeChange extends RandomMove<Lecture, Placement> {
    043
    044    public TimeChange(DataProperties config) {
    045        super(config);
    046    }
    047
    048    @Override
    049    public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
    050        TimetableModel model = (TimetableModel)solution.getModel();
    051        int varIdx = ToolBox.random(model.variables().size());
    052        for (int i = 0; i < model.variables().size(); i++) {
    053            Lecture lecture = model.variables().get((i + varIdx) % model.variables().size());
    054            Placement old = lecture.getAssignment();
    055            if (old == null) continue;
    056            List<TimeLocation> values = lecture.timeLocations();
    057            if (values.isEmpty()) continue;
    058            int valIdx = ToolBox.random(values.size());
    059            for (int j = 0; j < values.size(); j++) {
    060                TimeLocation time = values.get((j + valIdx) % values.size());
    061                if (time.getPreference() > 50) continue;
    062
    063                Placement placement = null;
    064                if (lecture.getNrRooms() == 0)
    065                    placement = new Placement(lecture, time, (RoomLocation) null);
    066                else if (lecture.getNrRooms() == 1)
    067                    placement = new Placement(lecture, time, old.getRoomLocation());
    068                else
    069                    placement = new Placement(lecture, time, old.getRoomLocations());
    070
    071                if (placement.isValid() && !model.inConflict(placement)) {
    072                    SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
    073                    if (!iHC || n.value() <= 0) return n;
    074                }
    075            }
    076        }
    077        return null;
    078    }
    079
    080
    081}