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