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