001package net.sf.cpsolver.exam.criteria;
002
003import java.util.Collection;
004import java.util.Map;
005import java.util.Set;
006
007import net.sf.cpsolver.exam.model.Exam;
008import net.sf.cpsolver.exam.model.ExamPeriodPlacement;
009import net.sf.cpsolver.exam.model.ExamPlacement;
010import net.sf.cpsolver.ifs.util.DataProperties;
011
012/**
013 * A weight for period penalty (used in
014 * {@link ExamPeriodPlacement#getPenalty()} multiplied by examination size
015 * {@link Exam#getSize()}. Can be set by problem property
016 * Exams.PeriodSizeWeight, or in the input xml file, property periodSizeWeight).
017 * 
018 * <br>
019 * 
020 * @version ExamTT 1.2 (Examination Timetabling)<br>
021 *          Copyright (C) 2008 - 2012 Tomáš Müller<br>
022 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
023 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
024 * <br>
025 *          This library is free software; you can redistribute it and/or modify
026 *          it under the terms of the GNU Lesser General Public License as
027 *          published by the Free Software Foundation; either version 3 of the
028 *          License, or (at your option) any later version. <br>
029 * <br>
030 *          This library is distributed in the hope that it will be useful, but
031 *          WITHOUT ANY WARRANTY; without even the implied warranty of
032 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
033 *          Lesser General Public License for more details. <br>
034 * <br>
035 *          You should have received a copy of the GNU Lesser General Public
036 *          License along with this library; if not see
037 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
038 */
039public class PeriodSizePenalty extends ExamCriterion {
040    
041    @Override
042    public String getWeightName() {
043        return "Exams.PeriodSizeWeight";
044    }
045    
046    @Override
047    public String getXmlWeightName() {
048        return "periodSizeWeight";
049    }
050    
051    @Override
052    public double getWeightDefault(DataProperties config) {
053        return 1.0;
054    }
055    
056    @Override
057    public double getValue(ExamPlacement value, Set<ExamPlacement> conflicts) {
058        return value.getPeriodPlacement().getPenalty() * (value.variable().getSize() + 1);
059    }
060    
061    @Override
062    public String getName() {
063        return "Period&times;Size Penalty";
064    }
065    
066    @Override
067    public double[] getBounds(Collection<Exam> variables) {
068        double[] bounds = new double[] { 0.0, 0.0 };
069        for (Exam exam : variables) {
070            if (!exam.getPeriodPlacements().isEmpty()) {
071                int minSizePenalty = Integer.MAX_VALUE, maxSizePenalty = Integer.MIN_VALUE;
072                for (ExamPeriodPlacement periodPlacement : exam.getPeriodPlacements()) {
073                    minSizePenalty = Math.min(minSizePenalty, periodPlacement.getPenalty() * (exam.getSize() + 1));
074                    maxSizePenalty = Math.max(maxSizePenalty, periodPlacement.getPenalty() * (exam.getSize() + 1));
075                }
076                bounds[0] += minSizePenalty;
077                bounds[1] += maxSizePenalty;
078            }
079        }
080        return bounds;
081    }
082    
083    @Override
084    public void getInfo(Map<String, String> info) {
085        if (getValue() != 0.0) {
086            info.put(getName(), sDoubleFormat.format(getValue() / getModel().nrAssignedVariables()));
087        }
088    }
089
090    @Override
091    public String toString() {
092        return "PS:" + sDoubleFormat.format(getValue() / getModel().nrAssignedVariables());
093    }
094}