001 package net.sf.cpsolver.exam.criteria;
002
003 import java.util.Map;
004 import java.util.Set;
005
006 import net.sf.cpsolver.exam.model.ExamPlacement;
007 import net.sf.cpsolver.exam.model.ExamRoomPlacement;
008 import net.sf.cpsolver.ifs.util.DataProperties;
009
010 /**
011 * Room split distance penalty. I.e., average distance between two rooms of a placement.
012 * <br><br>
013 * A weight for room split penalty can be set by problem
014 * property Exams.RoomSplitWeight, or in the input xml file, property
015 * roomSplitDistanceWeight).
016 *
017 * <br>
018 *
019 * @version ExamTT 1.2 (Examination Timetabling)<br>
020 * Copyright (C) 2008 - 2012 Tomas Muller<br>
021 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
022 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
023 * <br>
024 * This library is free software; you can redistribute it and/or modify
025 * it under the terms of the GNU Lesser General Public License as
026 * published by the Free Software Foundation; either version 3 of the
027 * License, or (at your option) any later version. <br>
028 * <br>
029 * This library is distributed in the hope that it will be useful, but
030 * WITHOUT ANY WARRANTY; without even the implied warranty of
031 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
032 * Lesser General Public License for more details. <br>
033 * <br>
034 * You should have received a copy of the GNU Lesser General Public
035 * License along with this library; if not see
036 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
037 */
038 public class RoomSplitDistancePenalty extends ExamCriterion {
039 private int iRoomSplits = 0;
040
041 @Override
042 public String getWeightName() {
043 return "Exams.RoomSplitDistanceWeight";
044 }
045
046 @Override
047 public String getXmlWeightName() {
048 return "roomSplitDistanceWeight";
049 }
050
051 @Override
052 public double getWeightDefault(DataProperties config) {
053 return 0.01;
054 }
055
056 @Override
057 public double getValue(ExamPlacement value, Set<ExamPlacement> conflicts) {
058 if (value.getRoomPlacements() == null || value.getRoomPlacements().size() <= 1) return 0.0;
059 double distance = 0.0;
060 for (ExamRoomPlacement r : value.getRoomPlacements()) {
061 for (ExamRoomPlacement w : value.getRoomPlacements()) {
062 if (r.getRoom().getId() < w.getRoom().getId())
063 distance += r.getRoom().getDistanceInMeters(w.getRoom());
064 }
065 }
066 int pairs = value.getRoomPlacements().size() * (value.getRoomPlacements().size() - 1) / 2;
067 return distance / pairs;
068 }
069
070 @Override
071 public void beforeUnassigned(long iteration, ExamPlacement value) {
072 super.beforeUnassigned(iteration, value);
073 if (value.getRoomPlacements() == null || value.getRoomPlacements().size() > 1)
074 iRoomSplits --;
075 }
076
077 @Override
078 public void afterAssigned(long iteration, ExamPlacement value) {
079 super.afterAssigned(iteration, value);
080 if (value.getRoomPlacements() == null || value.getRoomPlacements().size() > 1)
081 iRoomSplits ++;
082 }
083
084 @Override
085 public void getInfo(Map<String, String> info) {
086 if (getValue() != 0.0) {
087 info.put(getName(), sDoubleFormat.format(getValue() / iRoomSplits) + " m");
088 }
089 }
090
091 public int nrRoomSplits() {
092 return iRoomSplits;
093 }
094
095 @Override
096 public String toString() {
097 return "RSd:" + sDoubleFormat.format(getValue() / iRoomSplits);
098 }
099
100 @Override
101 public boolean isPeriodCriterion() { return false; }
102 }