001package org.cpsolver.exam.neighbours; 002 003import java.util.Set; 004 005import org.cpsolver.exam.model.Exam; 006import org.cpsolver.exam.model.ExamModel; 007import org.cpsolver.exam.model.ExamPeriodPlacement; 008import org.cpsolver.exam.model.ExamPlacement; 009import org.cpsolver.exam.model.ExamRoomPlacement; 010import org.cpsolver.ifs.assignment.Assignment; 011import org.cpsolver.ifs.heuristics.NeighbourSelection; 012import org.cpsolver.ifs.model.Neighbour; 013import org.cpsolver.ifs.solution.Solution; 014import org.cpsolver.ifs.solver.Solver; 015import org.cpsolver.ifs.util.DataProperties; 016import org.cpsolver.ifs.util.ToolBox; 017 018 019/** 020 * A new period is selected for a randomly selected exam. It tries to use the 021 * current set of rooms, if it is possible (exam is assigned, rooms are 022 * available and not used during the new period). Otherwise, rooms are selected 023 * using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)}. <br> 024 * <br> 025 * 026 * @version ExamTT 1.3 (Examination Timetabling)<br> 027 * Copyright (C) 2008 - 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 ExamTimeMove implements NeighbourSelection<Exam,ExamPlacement> { 046 private boolean iCheckStudentConflicts = false; 047 private boolean iCheckDistributionConstraints = true; 048 049 /** 050 * Constructor 051 * @param properties problem properties 052 */ 053 public ExamTimeMove(DataProperties properties) { 054 iCheckStudentConflicts = properties.getPropertyBoolean("ExamTimeMove.CheckStudentConflicts", iCheckStudentConflicts); 055 iCheckDistributionConstraints = properties.getPropertyBoolean("ExamTimeMove.CheckDistributionConstraints", iCheckDistributionConstraints); 056 } 057 058 /** 059 * Initialization 060 */ 061 @Override 062 public void init(Solver<Exam,ExamPlacement> solver) {} 063 064 /** 065 * Select an exam randomly, 066 * select an available period randomly (if it is not assigned), 067 * use rooms if possible, select rooms using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)} if not (exam is unassigned, a room is not available or used). 068 */ 069 @Override 070 public Neighbour<Exam,ExamPlacement> selectNeighbour(Solution<Exam,ExamPlacement> solution) { 071 ExamModel model = (ExamModel)solution.getModel(); 072 Assignment<Exam, ExamPlacement> assignment = solution.getAssignment(); 073 Exam exam = ToolBox.random(model.variables()); 074 ExamPlacement placement = assignment.getValue(exam); 075 int px = ToolBox.random(exam.getPeriodPlacements().size()); 076 for (int p=0;p<exam.getPeriodPlacements().size();p++) { 077 ExamPeriodPlacement period = exam.getPeriodPlacements().get((p+px)%exam.getPeriodPlacements().size()); 078 if (placement!=null && placement.getPeriod().equals(period.getPeriod())) continue; 079 if (iCheckStudentConflicts && exam.countStudentConflicts(assignment, period)>0) continue; 080 if (iCheckDistributionConstraints && !exam.checkDistributionConstraints(assignment, period)) continue; 081 if (placement!=null) { 082 boolean ok = true; 083 for (ExamRoomPlacement room: placement.getRoomPlacements()) 084 if (!room.isAvailable(period.getPeriod()) || room.getRoom().inConflict(assignment, exam, period.getPeriod())) { 085 ok = false; break; 086 } 087 if (ok) 088 return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, placement.getRoomPlacements())); 089 } 090 Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period); 091 if (rooms==null) continue; 092 return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms)); 093 } 094 return null; 095 } 096}