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.ExamPeriodPlacement;
009import org.cpsolver.exam.model.ExamPlacement;
010import org.cpsolver.ifs.assignment.Assignment;
011import org.cpsolver.ifs.util.DataProperties;
012
013
014/**
015 * A weight for period penalty (used in
016 * {@link ExamPeriodPlacement#getPenalty()} multiplied by examination size
017 * {@link Exam#getSize()}. Can be set by problem property
018 * Exams.PeriodSizeWeight, or in the input xml file, property periodSizeWeight).
019 * 
020 * <br>
021 * 
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 class PeriodSizePenalty extends ExamCriterion {
042    
043    @Override
044    public String getWeightName() {
045        return "Exams.PeriodSizeWeight";
046    }
047    
048    @Override
049    public String getXmlWeightName() {
050        return "periodSizeWeight";
051    }
052    
053    @Override
054    public double getWeightDefault(DataProperties config) {
055        return 1.0;
056    }
057    
058    @Override
059    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
060        return value.getPeriodPlacement().getPenalty() * (value.variable().getSize() + 1);
061    }
062    
063    @Override
064    public String getName() {
065        return "Period&times;Size Penalty";
066    }
067    
068    @Override
069    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
070        double[] bounds = new double[] { 0.0, 0.0 };
071        for (Exam exam : variables) {
072            if (!exam.getPeriodPlacements().isEmpty()) {
073                int minSizePenalty = Integer.MAX_VALUE, maxSizePenalty = Integer.MIN_VALUE;
074                for (ExamPeriodPlacement periodPlacement : exam.getPeriodPlacements()) {
075                    minSizePenalty = Math.min(minSizePenalty, periodPlacement.getPenalty() * (exam.getSize() + 1));
076                    maxSizePenalty = Math.max(maxSizePenalty, periodPlacement.getPenalty() * (exam.getSize() + 1));
077                }
078                bounds[0] += minSizePenalty;
079                bounds[1] += maxSizePenalty;
080            }
081        }
082        return bounds;
083    }
084    
085    @Override
086    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
087        if (getValue(assignment) != 0.0) {
088            info.put(getName(), sDoubleFormat.format(getValue(assignment) / assignment.nrAssignedVariables()));
089        }
090    }
091
092    @Override
093    public String toString(Assignment<Exam, ExamPlacement> assignment) {
094        return "PS:" + sDoubleFormat.format(getValue(assignment) / assignment.nrAssignedVariables());
095    }
096}