001 package net.sf.cpsolver.exam.criteria; 002 003 import java.util.Set; 004 005 import net.sf.cpsolver.exam.model.Exam; 006 import net.sf.cpsolver.exam.model.ExamModel; 007 import net.sf.cpsolver.exam.model.ExamPeriod; 008 import net.sf.cpsolver.exam.model.ExamPlacement; 009 import net.sf.cpsolver.exam.model.ExamRoom; 010 import net.sf.cpsolver.exam.model.ExamRoomPlacement; 011 import net.sf.cpsolver.ifs.util.DataProperties; 012 013 /** 014 * Room penalty (penalty for using given rooms). I.e., sum of 015 * {@link ExamRoomPlacement#getPenalty(ExamPeriod)} of assigned rooms. 016 * <br><br> 017 * A weight for room penalty can be set by problem property 018 * Exams.RoomPreferenceWeight, or in the input xml file, property 019 * roomWeight). 020 * 021 * <br> 022 * 023 * @version ExamTT 1.2 (Examination Timetabling)<br> 024 * Copyright (C) 2008 - 2012 Tomas Muller<br> 025 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 026 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 027 * <br> 028 * This library is free software; you can redistribute it and/or modify 029 * it under the terms of the GNU Lesser General Public License as 030 * published by the Free Software Foundation; either version 3 of the 031 * License, or (at your option) any later version. <br> 032 * <br> 033 * This library is distributed in the hope that it will be useful, but 034 * WITHOUT ANY WARRANTY; without even the implied warranty of 035 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 036 * Lesser General Public License for more details. <br> 037 * <br> 038 * You should have received a copy of the GNU Lesser General Public 039 * License along with this library; if not see 040 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 041 */ 042 public class RoomPenalty extends ExamCriterion { 043 044 @Override 045 public String getWeightName() { 046 return "Exams.RoomWeight"; 047 } 048 049 @Override 050 public String getXmlWeightName() { 051 return "roomWeight"; 052 } 053 054 @Override 055 public double getWeightDefault(DataProperties config) { 056 return 0.1; 057 } 058 059 @Override 060 public double getValue(ExamPlacement value, Set<ExamPlacement> conflicts) { 061 double penalty = 0.0; 062 if (value.getRoomPlacements() != null) 063 for (ExamRoomPlacement r : value.getRoomPlacements()) { 064 penalty += r.getPenalty(value.getPeriod()); 065 } 066 return penalty; 067 } 068 069 private int getMinPenalty(ExamRoom r) { 070 int min = Integer.MAX_VALUE; 071 for (ExamPeriod p : ((ExamModel)getModel()).getPeriods()) { 072 if (r.isAvailable(p)) { 073 min = Math.min(min, r.getPenalty(p)); 074 } 075 } 076 return min; 077 } 078 079 private int getMaxPenalty(ExamRoom r) { 080 int max = Integer.MIN_VALUE; 081 for (ExamPeriod p : ((ExamModel)getModel()).getPeriods()) { 082 if (r.isAvailable(p)) { 083 max = Math.max(max, r.getPenalty(p)); 084 } 085 } 086 return max; 087 } 088 089 @Override 090 protected void computeBounds() { 091 iBounds = new double[] { 0.0, 0.0 }; 092 for (Exam exam : getModel().variables()) { 093 if (!exam.getRoomPlacements().isEmpty()) { 094 int minPenalty = Integer.MAX_VALUE, maxPenalty = Integer.MIN_VALUE; 095 for (ExamRoomPlacement roomPlacement : exam.getRoomPlacements()) { 096 minPenalty = Math.min(minPenalty, 2 * roomPlacement.getPenalty() + getMinPenalty(roomPlacement.getRoom())); 097 maxPenalty = Math.max(maxPenalty, 2 * roomPlacement.getPenalty() + getMaxPenalty(roomPlacement.getRoom())); 098 } 099 iBounds[0] += minPenalty; 100 iBounds[1] += maxPenalty; 101 } 102 } 103 } 104 105 @Override 106 public String toString() { 107 return "RP:" + sDoubleFormat.format(getWeightedValue()); 108 } 109 110 @Override 111 public boolean isPeriodCriterion() { return false; } 112 }