001package org.cpsolver.exam.neighbours; 002 003import java.util.ArrayList; 004import java.util.List; 005import java.util.Set; 006 007import org.cpsolver.exam.model.Exam; 008import org.cpsolver.exam.model.ExamModel; 009import org.cpsolver.exam.model.ExamPeriodPlacement; 010import org.cpsolver.exam.model.ExamPlacement; 011import org.cpsolver.exam.model.ExamRoomPlacement; 012import org.cpsolver.ifs.assignment.Assignment; 013import org.cpsolver.ifs.heuristics.NeighbourSelection; 014import org.cpsolver.ifs.model.Neighbour; 015import org.cpsolver.ifs.solution.Solution; 016import org.cpsolver.ifs.solver.Solver; 017import org.cpsolver.ifs.util.DataProperties; 018import org.cpsolver.ifs.util.ToolBox; 019 020 021/** 022 * Try to swap a room between two exams. An exam is selected randomly, a 023 * different (available) room is randomly selected for the exam -- the exam is 024 * assigned into the new room (if the room is used, it tries to swap the rooms 025 * between the selected exam and the one that is using it). If an exam is 026 * assigned into two or more rooms, only one room is swapped at a time. <br> 027 * <br> 028 * 029 * @author Tomáš Müller 030 * @version ExamTT 1.3 (Examination Timetabling)<br> 031 * Copyright (C) 2008 - 2014 Tomáš Müller<br> 032 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 033 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 034 * <br> 035 * This library is free software; you can redistribute it and/or modify 036 * it under the terms of the GNU Lesser General Public License as 037 * published by the Free Software Foundation; either version 3 of the 038 * License, or (at your option) any later version. <br> 039 * <br> 040 * This library is distributed in the hope that it will be useful, but 041 * WITHOUT ANY WARRANTY; without even the implied warranty of 042 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 043 * Lesser General Public License for more details. <br> 044 * <br> 045 * You should have received a copy of the GNU Lesser General Public 046 * License along with this library; if not see 047 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 048 */ 049public class ExamRoomMove implements NeighbourSelection<Exam, ExamPlacement> { 050 private boolean iCheckStudentConflicts = false; 051 private boolean iCheckDistributionConstraints = true; 052 053 /** 054 * Constructor 055 * 056 * @param properties 057 * problem properties 058 */ 059 public ExamRoomMove(DataProperties properties) { 060 iCheckStudentConflicts = properties.getPropertyBoolean("ExamRoomMove.CheckStudentConflicts", iCheckStudentConflicts); 061 iCheckDistributionConstraints = properties.getPropertyBoolean("ExamRoomMove.CheckDistributionConstraints", iCheckDistributionConstraints); 062 } 063 064 /** 065 * Initialization 066 */ 067 @Override 068 public void init(Solver<Exam, ExamPlacement> solver) { 069 } 070 071 /** 072 * Select an exam randomly, select an available period randomly (if it is 073 * not assigned, from {@link Exam#getPeriodPlacements()}), select rooms 074 * using {@link Exam#findRoomsRandom(Assignment, ExamPeriodPlacement)} 075 */ 076 @Override 077 public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) { 078 ExamModel model = (ExamModel) solution.getModel(); 079 Assignment<Exam, ExamPlacement> assignment = solution.getAssignment(); 080 Exam exam = ToolBox.random(model.variables()); 081 if (exam.getMaxRooms() <= 0) 082 return null; 083 ExamPlacement placement = assignment.getValue(exam); 084 ExamPeriodPlacement period = (placement != null ? placement.getPeriodPlacement() 085 : (ExamPeriodPlacement) ToolBox.random(exam.getPeriodPlacements())); 086 if (iCheckStudentConflicts && placement == null && exam.countStudentConflicts(assignment, period) > 0) 087 return null; 088 if (iCheckDistributionConstraints && placement == null && !exam.checkDistributionConstraints(assignment, period)) 089 return null; 090 Set<ExamRoomPlacement> rooms = (placement != null ? placement.getRoomPlacements() : exam.findBestAvailableRooms(assignment, period)); 091 if (rooms == null || rooms.isEmpty()) 092 return null; 093 if (placement == null) 094 placement = new ExamPlacement(exam, period, rooms); 095 List<ExamRoomPlacement> roomVect = new ArrayList<ExamRoomPlacement>(rooms); 096 int rx = ToolBox.random(roomVect.size()); 097 for (int r = 0; r < roomVect.size(); r++) { 098 ExamRoomPlacement current = roomVect.get((r + rx) % roomVect.size()); 099 int mx = ToolBox.random(exam.getRoomPlacements().size()); 100 for (int m = 0; m < exam.getRoomPlacements().size(); m++) { 101 ExamRoomPlacement swap = exam.getRoomPlacements().get((m + mx) % exam.getRoomPlacements().size()); 102 ExamRoomSwapNeighbour n = new ExamRoomSwapNeighbour(assignment, placement, current, swap); 103 if (n.canDo()) 104 return n; 105 } 106 } 107 rooms = exam.findRoomsRandom(assignment, period); 108 if (rooms == null) 109 return null; 110 return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms)); 111 } 112}