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        protected void computeBounds() {
053            iBounds = getBounds(getModel().variables());
054        }
055        
056        @Override
057        public double[] getBounds(Collection<Exam> exams) {
058            double[] bounds = new double[] { 0.0, 0.0 };
059            for (Exam exam: exams) {
060                Double min = null, max = null;
061                for (ExamPeriodPlacement period: exam.getPeriodPlacements()) {
062                    if (exam.getMaxRooms() == 0) {
063                        double value = getValue(new ExamPlacement(exam, period, null), null);
064                        if (min == null) { min = value; max = value; continue; }
065                        min = Math.min(min, value);
066                        max = Math.max(max, value);
067                    } else {
068                        for (ExamRoomPlacement room: exam.getRoomPlacements()) {
069                            Set<ExamRoomPlacement> rooms = new HashSet<ExamRoomPlacement>();
070                            rooms.add(room);
071                            double value = getValue(new ExamPlacement(exam, period, rooms), null);
072                            if (min == null) { min = value; max = value; continue; }
073                            min = Math.min(min, value);
074                            max = Math.max(max, value);
075                        }
076                    }
077                }
078                if (min != null) {
079                    bounds[0] += min;
080                    bounds[1] += max;
081                }
082            }
083            return bounds;
084        }
085        
086        @Override
087        public void getInfo(Map<String, String> info) {
088            double val = getValue();
089            double[] bounds = getBounds();
090            if (bounds[0] <= val && val <= bounds[1] && bounds[0] < bounds[1])
091                info.put(getName(), getPerc(val, bounds[0], bounds[1]) + "% (" + sDoubleFormat.format(val) + ")");
092            else if (bounds[1] <= val && val <= bounds[0] && bounds[1] < bounds[0])
093                info.put(getName(), getPercRev(val, bounds[1], bounds[0]) + "% (" + sDoubleFormat.format(val) + ")");
094            else if (bounds[0] != val || val != bounds[1])
095                info.put(getName(), sDoubleFormat.format(val));
096        }
097        
098        /**
099         * True if this criterion is based on period assignment. Used by {@link ExamPlacement#getTimeCost()}.
100         **/
101        public boolean isPeriodCriterion() { return true; }
102        
103        /**
104         * Return impact of this criterion on period assignment (if this criterion is based on period assignment). Used by {@link ExamPlacement#getTimeCost()}.
105         */
106        public double getPeriodValue(ExamPlacement value) { return isPeriodCriterion() ? getValue(value, null) : 0.0; }
107         
108        /**
109         * True if this criterion is based on room assignment. Used by {@link ExamPlacement#getRoomCost()}.
110         **/
111        public boolean isRoomCriterion() { return !isPeriodCriterion(); }
112        
113        /**
114         * Return impact of this criterion on room assignment (if this criterion is based on room assignment). Used by {@link ExamPlacement#getRoomCost()}.
115         */
116        public double getRoomValue(ExamPlacement value) { return isRoomCriterion() ? getValue(value, null) : 0.0; }
117        
118        /**
119         * Name of the weight parameter in the parameters section of the examination XML file.
120         */
121        public String getXmlWeightName() {
122            String name = getClass().getName().substring(1 + getClass().getName().lastIndexOf('.'));
123            return Character.toString(name.charAt(0)) + name.substring(1);
124        }
125        
126        /**
127         * Put all the parameters of this criterion into a map that is used to write parameters section of the examination XML file.
128         */
129        public void getXmlParameters(Map<String, String> params) {
130            params.put(getXmlWeightName(), String.valueOf(getWeight()));
131        }
132        
133        /**
134         * Set all the parameters of this criterion from a map that is read from the parameters section the examination XML file.
135         */
136        public void setXmlParameters(Map<String, String> params) {
137            try {
138                setWeight(Double.valueOf(params.get(getXmlWeightName())));
139            } catch (NumberFormatException e) {
140            } catch (NullPointerException e) {}
141        }
142    }