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