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.solver.Solver;
012import org.cpsolver.ifs.util.DataProperties;
013
014
015/**
016 * Front load penalty. I.e., large exam is discouraged to be placed on or after a
017 * certain period.
018 * <br><br>
019 * <b>largeSize</b>: An exam is considered large, if its size is greater or equal to 
020 * this number. Value -1 means all exams are small. It can be set by problem
021 * property Exams.LargeSize, or in the input xml file, property largeSize.
022 * <br><br>
023 * <b>largePeriod</b>: Period index (number of periods multiplied by this number) for front load
024 * criteria for large exams. Can be set by problem property
025 * Exams.LargePeriod, or in the input xml file, property largePeriod.
026 * <br><br>
027 * Weight of the front load criterion, i.e., a weight for assigning a large exam
028 * after large period can be set by problem property Exams.LargeWeight, or
029 * in the input xml file, property largeWeight.
030 * 
031 * <br>
032 * 
033 * @version ExamTT 1.3 (Examination Timetabling)<br>
034 *          Copyright (C) 2008 - 2014 Tomas Muller<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 boolean init(Solver<Exam, ExamPlacement> solver) {
093        boolean ret = super.init(solver);
094        iLargeSize = solver.getProperties().getPropertyInt("Exams.LargeSize", iLargeSize);
095        iLargePeriod = solver.getProperties().getPropertyDouble("Exams.LargePeriod", iLargePeriod);
096        return ret;
097    }
098    
099    /**
100     * An exam is considered large, if its size is greater or equal to this
101     * large size. Value -1 means all exams are small. Can be set by problem
102     * property Exams.LargeSize, or in the input xml file, property largeSize)
103     * @return large size
104     **/
105    public int getLargeSize() {
106        return iLargeSize;
107    }
108    
109    /**
110     * An exam is considered large, if its size is greater or equal to this
111     * large size. Value -1 means all exams are small. Can be set by problem
112     * property Exams.LargeSize, or in the input xml file, property largeSize)
113     * @param largeSize large size
114     **/
115    public void setLargeSize(int largeSize) {
116        iLargeSize = largeSize;
117    }
118    
119    /**
120     * Period index (number of periods multiplied by this number) for front load
121     * criteria for large exams. Can be set by problem property
122     * Exams.LargePeriod, or in the input xml file, property largePeriod)
123     * @return large period
124     **/
125    public double getLargePeriod() {
126        return iLargePeriod;
127    }
128    
129    /**
130     * Period index (number of periods multiplied by this number) for front load
131     * criteria for large exams. Can be set by problem property
132     * Exams.LargePeriod, or in the input xml file, property largePeriod)
133     * @param largePeriod large period
134     **/
135    public void setLargePeriod(double largePeriod) {
136        iLargePeriod = largePeriod;
137    }
138
139    
140    public int getLargePeriodIndex() {
141        return (int) Math.round(((ExamModel)getModel()).getPeriods().size() * iLargePeriod);
142    }
143
144    @Override
145    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
146        Exam exam = value.variable();
147        if (getLargeSize() < 0 || exam.getSize() < getLargeSize()) return 0;
148        return (value.getPeriod().getIndex() < getLargePeriodIndex() ? 0 : 1);
149    }
150
151    @Override
152    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
153        double[] bounds = new double[] { 0.0, 0.0 };
154        for (Exam exam : variables) {
155            if (getLargeSize() >= 0 && exam.getSize() >= getLargeSize())
156                bounds[1] += 1.0;
157        }
158        return bounds;
159    }
160
161    @Override
162    public String toString(Assignment<Exam, ExamPlacement> assignment) {
163        return (getValue(assignment) <= 0.0 ? "" : "LP:" + sDoubleFormat.format(getValue(assignment)));
164    }
165}