001package org.cpsolver.exam.criteria;
002
003import java.util.Collection;
004import java.util.HashSet;
005import java.util.Map;
006import java.util.Set;
007
008import org.cpsolver.exam.model.Exam;
009import org.cpsolver.exam.model.ExamPeriodPlacement;
010import org.cpsolver.exam.model.ExamPlacement;
011import org.cpsolver.exam.model.ExamRoomPlacement;
012import org.cpsolver.ifs.assignment.Assignment;
013import org.cpsolver.ifs.criteria.AbstractCriterion;
014
015
016/**
017 * Abstract examination criterion. All examination criteria are inherited from this criterion.
018 * 
019 * <br>
020 * 
021 * @version ExamTT 1.3 (Examination Timetabling)<br>
022 *          Copyright (C) 2008 - 2014 Tomas Muller<br>
023 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
024 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
025 * <br>
026 *          This library is free software; you can redistribute it and/or modify
027 *          it under the terms of the GNU Lesser General Public License as
028 *          published by the Free Software Foundation; either version 3 of the
029 *          License, or (at your option) any later version. <br>
030 * <br>
031 *          This library is distributed in the hope that it will be useful, but
032 *          WITHOUT ANY WARRANTY; without even the implied warranty of
033 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
034 *          Lesser General Public License for more details. <br>
035 * <br>
036 *          You should have received a copy of the GNU Lesser General Public
037 *          License along with this library; if not see
038 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
039 */
040public abstract class ExamCriterion extends AbstractCriterion<Exam, ExamPlacement> {
041    
042    public ExamCriterion() {
043        super();
044    }
045    
046    public void setWeight(double weight) { iWeight = weight; }
047    
048    @Override
049    public String getWeightName() {
050        return "Exams." + getClass().getName().substring(1 + getClass().getName().lastIndexOf('.')) + "Weight";
051    }
052    
053    @Override
054    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> exams) {
055        double[] bounds = new double[] { 0.0, 0.0 };
056        for (Exam exam: exams) {
057            Double min = null, max = null;
058            for (ExamPeriodPlacement period: exam.getPeriodPlacements()) {
059                if (exam.getMaxRooms() == 0) {
060                    double value = getValue(assignment, new ExamPlacement(exam, period, null), null);
061                    if (min == null) { min = value; max = value; continue; }
062                    min = Math.min(min, value);
063                    max = Math.max(max, value);
064                } else {
065                    for (ExamRoomPlacement room: exam.getRoomPlacements()) {
066                        Set<ExamRoomPlacement> rooms = new HashSet<ExamRoomPlacement>();
067                        rooms.add(room);
068                        double value = getValue(assignment, new ExamPlacement(exam, period, rooms), null);
069                        if (min == null) { min = value; max = value; continue; }
070                        min = Math.min(min, value);
071                        max = Math.max(max, value);
072                    }
073                }
074            }
075            if (min != null) {
076                bounds[0] += min;
077                bounds[1] += max;
078            }
079        }
080        return bounds;
081    }
082    
083    @Override
084    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
085        double val = getValue(assignment);
086        double[] bounds = getBounds(assignment);
087        if (bounds[0] <= val && val <= bounds[1] && bounds[0] < bounds[1])
088            info.put(getName(), getPerc(val, bounds[0], bounds[1]) + "% (" + sDoubleFormat.format(val) + ")");
089        else if (bounds[1] <= val && val <= bounds[0] && bounds[1] < bounds[0])
090            info.put(getName(), getPercRev(val, bounds[1], bounds[0]) + "% (" + sDoubleFormat.format(val) + ")");
091        else if (bounds[0] != val || val != bounds[1])
092            info.put(getName(), sDoubleFormat.format(val));
093    }
094    
095    /**
096     * True if this criterion is based on period assignment. Used by {@link ExamPlacement#getTimeCost(Assignment)}.
097     * @return true if this criterion is based on period assignment
098     **/
099    public boolean isPeriodCriterion() { return true; }
100    
101    /**
102     * Return impact of this criterion on period assignment (if this criterion is based on period assignment). Used by {@link ExamPlacement#getTimeCost(Assignment)}.
103     * @param assignment current assignment
104     * @param value new assignment in question
105     * @return change in the period preference value
106     */
107    public double getPeriodValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value) { return isPeriodCriterion() ? getValue(assignment, value, null) : 0.0; }
108     
109    /**
110     * True if this criterion is based on room assignment. Used by {@link ExamPlacement#getRoomCost(Assignment)}.
111     * @return true if this criterion is based on room assignment
112     **/
113    public boolean isRoomCriterion() { return !isPeriodCriterion(); }
114    
115    /**
116     * Return impact of this criterion on room assignment (if this criterion is based on room assignment). Used by {@link ExamPlacement#getRoomCost(Assignment)}.
117     * @param assignment current assignment
118     * @param value new assignment in question
119     * @return change in the room preference value
120     */
121    public double getRoomValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value) { return isRoomCriterion() ? getValue(assignment, value, null) : 0.0; }
122    
123    /**
124     * Name of the weight parameter in the parameters section of the examination XML file.
125     * @return name of the weight parameter in the XML
126     */
127    public String getXmlWeightName() {
128        String name = getClass().getName().substring(1 + getClass().getName().lastIndexOf('.'));
129        return Character.toString(name.charAt(0)) + name.substring(1);
130    }
131    
132    /**
133     * Put all the parameters of this criterion into a map that is used to write parameters section of the examination XML file.
134     * @param params map of parameters (parameter = value) to be populated
135     */
136    public void getXmlParameters(Map<String, String> params) {
137        params.put(getXmlWeightName(), String.valueOf(getWeight()));
138    }
139    
140    /**
141     * Set all the parameters of this criterion from a map that is read from the parameters section the examination XML file.
142     * @param params map of parameters (parameter = value) loaded from XML
143     */
144    public void setXmlParameters(Map<String, String> params) {
145        try {
146            setWeight(Double.valueOf(params.get(getXmlWeightName())));
147        } catch (NumberFormatException e) {
148        } catch (NullPointerException e) {}
149    }
150}