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