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