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