    001package org.cpsolver.exam.criteria;
    002
    003import java.util.Collection;
    004import java.util.Map;
    005import java.util.Set;
    006
    007import org.cpsolver.exam.model.Exam;
    008import org.cpsolver.exam.model.ExamModel;
    009import org.cpsolver.exam.model.ExamPlacement;
    010import org.cpsolver.ifs.assignment.Assignment;
    011import org.cpsolver.ifs.util.DataProperties;
    012
    013
    014/**
    015 * Front load penalty. I.e., large exam is discouraged to be placed on or after a
    016 * certain period.
    017 * <br><br>
    018 * <b>largeSize</b>: An exam is considered large, if its size is greater or equal to
    019 * this number. Value -1 means all exams are small. It can be set by problem
    020 * property Exams.LargeSize, or in the input xml file, property largeSize.
    021 * <br><br>
    022 * <b>largePeriod</b>: Period index (number of periods multiplied by this number) for front load
    023 * criteria for large exams. Can be set by problem property
    024 * Exams.LargePeriod, or in the input xml file, property largePeriod.
    025 * <br><br>
    026 * Weight of the front load criterion, i.e., a weight for assigning a large exam
    027 * after large period can be set by problem property Exams.LargeWeight, or
    028 * in the input xml file, property largeWeight.
    029 *
    030 * <br>
    031 *
    032 * @version ExamTT 1.3 (Examination Timetabling)<br>
    033 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    034 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    035 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    036 * <br>
    037 *          This library is free software; you can redistribute it and/or modify
    038 *          it under the terms of the GNU Lesser General Public License as
    039 *          published by the Free Software Foundation; either version 3 of the
    040 *          License, or (at your option) any later version. <br>
    041 * <br>
    042 *          This library is distributed in the hope that it will be useful, but
    043 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    044 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    045 *          Lesser General Public License for more details. <br>
    046 * <br>
    047 *          You should have received a copy of the GNU Lesser General Public
    048 *          License along with this library; if not see
    049 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    050 */
    051public class LargeExamsPenalty extends ExamCriterion {
    052    private int iLargeSize = -1;
    053    private double iLargePeriod = 0.67;
    054
    055    @Override
    056    public String getWeightName() {
    057        return "Exams.LargeWeight";
    058    }
    059
    060    @Override
    061    public String getXmlWeightName() {
    062        return "largeWeight";
    063    }
    064
    065    @Override
    066    public void getXmlParameters(Map<String, String> params) {
    067        params.put(getXmlWeightName(), String.valueOf(getWeight()));
    068        params.put("largeSize", String.valueOf(getLargeSize()));
    069        params.put("largePeriod", String.valueOf(getLargePeriod()));
    070    }
    071
    072    @Override
    073    public void setXmlParameters(Map<String, String> params) {
    074        try {
    075            setWeight(Double.valueOf(params.get(getXmlWeightName())));
    076        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
    077        try {
    078            setLargeSize(Integer.valueOf(params.get("largeSize")));
    079        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
    080        try {
    081            setLargePeriod(Double.valueOf(params.get("largePeriod")));
    082        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
    083    }
    084
    085    @Override
    086    public double getWeightDefault(DataProperties config) {
    087        return 1.0;
    088    }
    089
    090    @Override
    091    public void configure(DataProperties properties) {
    092        super.configure(properties);
    093        iLargeSize = properties.getPropertyInt("Exams.LargeSize", iLargeSize);
    094        iLargePeriod = properties.getPropertyDouble("Exams.LargePeriod", iLargePeriod);
    095    }
    096
    097    /**
    098     * An exam is considered large, if its size is greater or equal to this
    099     * large size. Value -1 means all exams are small. Can be set by problem
    100     * property Exams.LargeSize, or in the input xml file, property largeSize)
    101     * @return large size
    102     **/
    103    public int getLargeSize() {
    104        return iLargeSize;
    105    }
    106
    107    /**
    108     * An exam is considered large, if its size is greater or equal to this
    109     * large size. Value -1 means all exams are small. Can be set by problem
    110     * property Exams.LargeSize, or in the input xml file, property largeSize)
    111     * @param largeSize large size
    112     **/
    113    public void setLargeSize(int largeSize) {
    114        iLargeSize = largeSize;
    115    }
    116
    117    /**
    118     * Period index (number of periods multiplied by this number) for front load
    119     * criteria for large exams. Can be set by problem property
    120     * Exams.LargePeriod, or in the input xml file, property largePeriod)
    121     * @return large period
    122     **/
    123    public double getLargePeriod() {
    124        return iLargePeriod;
    125    }
    126
    127    /**
    128     * Period index (number of periods multiplied by this number) for front load
    129     * criteria for large exams. Can be set by problem property
    130     * Exams.LargePeriod, or in the input xml file, property largePeriod)
    131     * @param largePeriod large period
    132     **/
    133    public void setLargePeriod(double largePeriod) {
    134        iLargePeriod = largePeriod;
    135    }
    136
    137
    138    public int getLargePeriodIndex() {
    139        return (int) Math.round(((ExamModel)getModel()).getPeriods().size() * iLargePeriod);
    140    }
    141
    142    @Override
    143    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    144        Exam exam = value.variable();
    145        if (getLargeSize() < 0 || exam.getSize() < getLargeSize()) return 0;
    146        return (value.getPeriod().getIndex() < getLargePeriodIndex() ? 0 : 1);
    147    }
    148
    149    @Override
    150    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
    151        double[] bounds = new double[] { 0.0, 0.0 };
    152        for (Exam exam : variables) {
    153            if (getLargeSize() >= 0 && exam.getSize() >= getLargeSize())
    154                bounds[1] += 1.0;
    155        }
    156        return bounds;
    157    }
    158
    159    @Override
    160    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    161        return (getValue(assignment) <= 0.0 ? "" : "LP:" + sDoubleFormat.format(getValue(assignment)));
    162    }
    163}