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