001    package net.sf.cpsolver.exam.criteria;
002    
003    import java.util.Collection;
004    import java.util.HashSet;
005    import java.util.Map;
006    import java.util.Set;
007    
008    import net.sf.cpsolver.exam.model.Exam;
009    import net.sf.cpsolver.exam.model.ExamPeriodPlacement;
010    import net.sf.cpsolver.exam.model.ExamPlacement;
011    import net.sf.cpsolver.exam.model.ExamRoomPlacement;
012    import net.sf.cpsolver.ifs.criteria.AbstractCriterion;
013    
014    /**
015     * Abstract examination criterion. All examination criteria are inherited from this criterion.
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 abstract class ExamCriterion extends AbstractCriterion<Exam, ExamPlacement> {
039        
040        public ExamCriterion() {
041            super();
042        }
043        
044        public void setWeight(double weight) { iWeight = weight; }
045        
046        @Override
047        public String getWeightName() {
048            return "Exams." + getClass().getName().substring(1 + getClass().getName().lastIndexOf('.')) + "Weight";
049        }
050        
051        @Override
052        public double[] getBounds(Collection<Exam> exams) {
053            double[] bounds = new double[] { 0.0, 0.0 };
054            for (Exam exam: exams) {
055                Double min = null, max = null;
056                for (ExamPeriodPlacement period: exam.getPeriodPlacements()) {
057                    if (exam.getMaxRooms() == 0) {
058                        double value = getValue(new ExamPlacement(exam, period, null), null);
059                        if (min == null) { min = value; max = value; continue; }
060                        min = Math.min(min, value);
061                        max = Math.max(max, value);
062                    } else {
063                        for (ExamRoomPlacement room: exam.getRoomPlacements()) {
064                            Set<ExamRoomPlacement> rooms = new HashSet<ExamRoomPlacement>();
065                            rooms.add(room);
066                            double value = getValue(new ExamPlacement(exam, period, rooms), null);
067                            if (min == null) { min = value; max = value; continue; }
068                            min = Math.min(min, value);
069                            max = Math.max(max, value);
070                        }
071                    }
072                }
073                if (min != null) {
074                    bounds[0] += min;
075                    bounds[1] += max;
076                }
077            }
078            return bounds;
079        }
080        
081        @Override
082        public void getInfo(Map<String, String> info) {
083            double val = getValue();
084            double[] bounds = getBounds();
085            if (bounds[0] <= val && val <= bounds[1] && bounds[0] < bounds[1])
086                info.put(getName(), getPerc(val, bounds[0], bounds[1]) + "% (" + sDoubleFormat.format(val) + ")");
087            else if (bounds[1] <= val && val <= bounds[0] && bounds[1] < bounds[0])
088                info.put(getName(), getPercRev(val, bounds[1], bounds[0]) + "% (" + sDoubleFormat.format(val) + ")");
089            else if (bounds[0] != val || val != bounds[1])
090                info.put(getName(), sDoubleFormat.format(val));
091        }
092        
093        /**
094         * True if this criterion is based on period assignment. Used by {@link ExamPlacement#getTimeCost()}.
095         **/
096        public boolean isPeriodCriterion() { return true; }
097        
098        /**
099         * Return impact of this criterion on period assignment (if this criterion is based on period assignment). Used by {@link ExamPlacement#getTimeCost()}.
100         */
101        public double getPeriodValue(ExamPlacement value) { return isPeriodCriterion() ? getValue(value, null) : 0.0; }
102         
103        /**
104         * True if this criterion is based on room assignment. Used by {@link ExamPlacement#getRoomCost()}.
105         **/
106        public boolean isRoomCriterion() { return !isPeriodCriterion(); }
107        
108        /**
109         * Return impact of this criterion on room assignment (if this criterion is based on room assignment). Used by {@link ExamPlacement#getRoomCost()}.
110         */
111        public double getRoomValue(ExamPlacement value) { return isRoomCriterion() ? getValue(value, null) : 0.0; }
112        
113        /**
114         * Name of the weight parameter in the parameters section of the examination XML file.
115         */
116        public String getXmlWeightName() {
117            String name = getClass().getName().substring(1 + getClass().getName().lastIndexOf('.'));
118            return Character.toString(name.charAt(0)) + name.substring(1);
119        }
120        
121        /**
122         * Put all the parameters of this criterion into a map that is used to write parameters section of the examination XML file.
123         */
124        public void getXmlParameters(Map<String, String> params) {
125            params.put(getXmlWeightName(), String.valueOf(getWeight()));
126        }
127        
128        /**
129         * Set all the parameters of this criterion from a map that is read from the parameters section the examination XML file.
130         */
131        public void setXmlParameters(Map<String, String> params) {
132            try {
133                setWeight(Double.valueOf(params.get(getXmlWeightName())));
134            } catch (NumberFormatException e) {
135            } catch (NullPointerException e) {}
136        }
137    }