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.TimetableModel;
009import org.cpsolver.ifs.algorithms.neighbourhoods.RandomMove;
010import org.cpsolver.ifs.assignment.Assignment;
011import org.cpsolver.ifs.model.Neighbour;
012import org.cpsolver.ifs.model.SimpleNeighbour;
013import org.cpsolver.ifs.solution.Solution;
014import org.cpsolver.ifs.util.DataProperties;
015import org.cpsolver.ifs.util.ToolBox;
016
017
018/**
019 * Try to assign a class with a new room. A class is selected randomly, a
020 * different (available) room is randomly selected for the class -- the class is
021 * assigned into the new room if there is no conflict.
022 * <br>
023 * 
024 * @author  Tomáš Müller
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 RoomChange extends RandomMove<Lecture, Placement> {
045
046    public RoomChange(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 || old.getNrRooms() != 1) continue;
059
060            List<RoomLocation> values = lecture.roomLocations();
061            if (values.isEmpty()) continue;
062
063            int valIdx = ToolBox.random(values.size());
064            for (int j = 0; j < values.size(); j++) {
065                RoomLocation room = values.get((j + valIdx) % values.size());
066                if (room.getPreference() > 50) continue;
067                if (room.equals(old.getRoomLocation())) continue;
068                
069                Placement placement = new Placement(lecture, old.getTimeLocation(), room);
070                if (placement.isValid() && !model.inConflict(assignment, placement)) {
071                    SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
072                    if (!iHC || n.value(assignment) <= 0) return n;
073                }
074            }
075        }
076        return null;
077    }
078}