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.ifs.util.DataProperties;
008    
009    /**
010     * Cost for using more than one room (nrSplits^2).
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     * roomSplitWeight.
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 RoomSplitPenalty extends ExamCriterion {
038        private int iRoomSplits[] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
039        
040        @Override
041        public String getWeightName() {
042            return "Exams.RoomSplitWeight";
043        }
044        
045        @Override
046        public String getXmlWeightName() {
047            return "roomSplitWeight";
048        }
049        
050        @Override
051        public double getWeightDefault(DataProperties config) {
052            return 10.0;
053        }
054    
055        @Override
056        public double getValue(ExamPlacement value, Set<ExamPlacement> conflicts) {
057            return (value.getRoomPlacements() == null || value.getRoomPlacements().size() <= 1 ? 0 : (value.getRoomPlacements().size() - 1) * (value.getRoomPlacements().size() - 1));
058        }
059    
060        @Override
061        public void beforeUnassigned(long iteration, ExamPlacement value) {
062            super.beforeUnassigned(iteration, value);
063            if (value.getRoomPlacements() == null || value.getRoomPlacements().size() > 1)
064                iRoomSplits[value.getRoomPlacements().size() - 2] --;
065        }
066        
067        @Override
068        public void afterAssigned(long iteration, ExamPlacement value) {
069            super.afterAssigned(iteration, value);
070            if (value.getRoomPlacements() == null || value.getRoomPlacements().size() > 1)
071                iRoomSplits[value.getRoomPlacements().size() - 2] ++;
072        }
073        
074        @Override
075        public void getInfo(Map<String, String> info) {
076            if (getValue() != 0.0) {
077                String split = "";
078                for (int i = 0; i < iRoomSplits.length; i++) {
079                    if (iRoomSplits[i] > 0) {
080                        if (split.length() > 0)
081                            split += ", ";
082                        split += iRoomSplits[i] + "&times;" + (i + 2);
083                    }
084                }
085                info.put(getName(), sDoubleFormat.format(getValue()) + " (" + split + ")");            
086            }
087        }
088    
089        @Override
090        public String toString() {
091            return "RSp:" + sDoubleFormat.format(getValue());
092        }
093    
094        @Override
095        public boolean isPeriodCriterion() { return false; }
096    }