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