001package org.cpsolver.exam.neighbours; 002 003import java.util.Iterator; 004import java.util.Set; 005 006import org.cpsolver.exam.model.Exam; 007import org.cpsolver.exam.model.ExamModel; 008import org.cpsolver.exam.model.ExamPeriodPlacement; 009import org.cpsolver.exam.model.ExamPlacement; 010import org.cpsolver.exam.model.ExamRoomPlacement; 011import org.cpsolver.exam.model.ExamRoomSharing; 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 * A new period is selected for a randomly selected exam. It tries to use the 023 * current set of rooms, if it is possible (exam is assigned, rooms are 024 * available and not used during the new period). Otherwise, rooms are selected 025 * using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)}. <br> 026 * <br> 027 * 028 * @version ExamTT 1.3 (Examination Timetabling)<br> 029 * Copyright (C) 2008 - 2014 Tomáš Müller<br> 030 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 031 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 032 * <br> 033 * This library is free software; you can redistribute it and/or modify 034 * it under the terms of the GNU Lesser General Public License as 035 * published by the Free Software Foundation; either version 3 of the 036 * License, or (at your option) any later version. <br> 037 * <br> 038 * This library is distributed in the hope that it will be useful, but 039 * WITHOUT ANY WARRANTY; without even the implied warranty of 040 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 041 * Lesser General Public License for more details. <br> 042 * <br> 043 * You should have received a copy of the GNU Lesser General Public 044 * License along with this library; if not see 045 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 046 */ 047public class ExamTimeMove implements NeighbourSelection<Exam,ExamPlacement> { 048 private boolean iCheckStudentConflicts = false; 049 private boolean iCheckDistributionConstraints = true; 050 051 /** 052 * Constructor 053 * @param properties problem properties 054 */ 055 public ExamTimeMove(DataProperties properties) { 056 iCheckStudentConflicts = properties.getPropertyBoolean("ExamTimeMove.CheckStudentConflicts", iCheckStudentConflicts); 057 iCheckDistributionConstraints = properties.getPropertyBoolean("ExamTimeMove.CheckDistributionConstraints", iCheckDistributionConstraints); 058 } 059 060 /** 061 * Initialization 062 */ 063 @Override 064 public void init(Solver<Exam,ExamPlacement> solver) {} 065 066 /** 067 * Select an exam randomly, 068 * select an available period randomly (if it is not assigned), 069 * use rooms if possible, select rooms using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)} if not (exam is unassigned, a room is not available or used). 070 */ 071 @Override 072 public Neighbour<Exam,ExamPlacement> selectNeighbour(Solution<Exam,ExamPlacement> solution) { 073 ExamModel model = (ExamModel)solution.getModel(); 074 Assignment<Exam, ExamPlacement> assignment = solution.getAssignment(); 075 ExamRoomSharing sharing = model.getRoomSharing(); 076 Exam exam = ToolBox.random(model.variables()); 077 ExamPlacement placement = assignment.getValue(exam); 078 int px = ToolBox.random(exam.getPeriodPlacements().size()); 079 for (int p=0;p<exam.getPeriodPlacements().size();p++) { 080 ExamPeriodPlacement period = exam.getPeriodPlacements().get((p+px)%exam.getPeriodPlacements().size()); 081 if (placement!=null && placement.getPeriod().equals(period)) continue; 082 if (iCheckStudentConflicts && exam.countStudentConflicts(assignment, period)>0) continue; 083 if (iCheckDistributionConstraints && !exam.checkDistributionConstraints(assignment, period)) continue; 084 if (placement!=null) { 085 boolean ok = true; 086 if (sharing != null && placement.getRoomPlacements().size() == 1) { 087 ExamRoomPlacement room = placement.getRoomPlacements().iterator().next(); 088 ok = room.isAvailable(period.getPeriod()) && !sharing.inConflict(exam, room.getRoom().getPlacements(assignment, period.getPeriod()), room.getRoom()); 089 } else { 090 for (Iterator<ExamRoomPlacement> i=placement.getRoomPlacements().iterator();i.hasNext();) { 091 ExamRoomPlacement room = i.next(); 092 if (!room.isAvailable(period.getPeriod()) || !room.getRoom().getPlacements(assignment, period.getPeriod()).isEmpty()) { 093 ok = false; break; 094 } 095 } 096 } 097 if (ok) 098 return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, placement.getRoomPlacements())); 099 } 100 Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period); 101 if (rooms==null) continue; 102 return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms)); 103 } 104 return null; 105 } 106}