001 package net.sf.cpsolver.exam.criteria.additional;
002
003 import java.util.Map;
004 import java.util.Set;
005
006 import net.sf.cpsolver.exam.criteria.ExamCriterion;
007 import net.sf.cpsolver.exam.model.ExamPlacement;
008 import net.sf.cpsolver.exam.model.ExamRoomPlacement;
009 import net.sf.cpsolver.ifs.util.DataProperties;
010
011 /**
012 * Experimental criterion measuring average distance (in meters) to the
013 * strongly preferred room (or rooms) of the examination. The idea is to
014 * prefer rooms that are close to the strongly preference room (if there is
015 * a strongly preferred room but it is not available).
016 * <br><br>
017 * A weight of the average distance between the assigned room(s) and the
018 * strongly preferred room or rooms can be set using
019 * Exams.DistanceToStronglyPreferredRoomWeight property.
020 * <br><br>
021 * To enable this criterion add this class name to Exams.AdditionalCriteria
022 * parameter. For instance:<br>
023 * Exams.AdditionalCriteria=net.sf.cpsolver.exam.criteria.additional.DistanceToStronglyPreferredRoom
024 *
025 * <br>
026 *
027 * @version ExamTT 1.2 (Examination Timetabling)<br>
028 * Copyright (C) 2008 - 2012 Tomas Muller<br>
029 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
030 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
031 * <br>
032 * This library is free software; you can redistribute it and/or modify
033 * it under the terms of the GNU Lesser General Public License as
034 * published by the Free Software Foundation; either version 3 of the
035 * License, or (at your option) any later version. <br>
036 * <br>
037 * This library is distributed in the hope that it will be useful, but
038 * WITHOUT ANY WARRANTY; without even the implied warranty of
039 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
040 * Lesser General Public License for more details. <br>
041 * <br>
042 * You should have received a copy of the GNU Lesser General Public
043 * License along with this library; if not see
044 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
045 */
046 public class DistanceToStronglyPreferredRoom extends ExamCriterion {
047
048 @Override
049 public String getWeightName() {
050 return "Exams.DistanceToStronglyPreferredRoomWeight";
051 }
052
053 @Override
054 public String getXmlWeightName() {
055 return "distanceToStronglyPreferredRoomWeight";
056 }
057
058 @Override
059 public double getWeightDefault(DataProperties config) {
060 return 0.001;
061 }
062
063 @Override
064 public double getValue(ExamPlacement value, Set<ExamPlacement> conflicts) {
065 Average ret = new Average();
066 for (ExamRoomPlacement assigned: value.getRoomPlacements()) {
067 for (ExamRoomPlacement preferred: value.variable().getRoomPlacements()) {
068 if (preferred.getPenalty() < -2) // strongly preferred
069 ret.add(assigned.getDistanceInMeters(preferred));
070 }
071 }
072 return ret.average();
073 }
074
075 @Override
076 public String toString() {
077 return "@D:" + sDoubleFormat.format(getValue() / getModel().assignedVariables().size());
078 }
079
080 @Override
081 public double[] getBounds() {
082 return new double[] { 0.0, 0.0 };
083 }
084
085 @Override
086 public void getInfo(Map<String, String> info) {
087 if (getValue() > 0.0)
088 info.put(getName(), sDoubleFormat.format(getValue() / getModel().assignedVariables().size()) + " m");
089 }
090
091 private static class Average {
092 double iValue = 0.0;
093 int iCount = 0;
094
095 private void add(double value) {
096 iValue += value; iCount ++;
097 }
098
099 public double average() {
100 return (iCount == 0 ? 0.0 : iValue / iCount);
101 }
102 }
103
104 }