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 * @author  Tomáš Müller
023 * @version ExamTT 1.3 (Examination Timetabling)<br>
024 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
025 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
026 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
027 * <br>
028 *          This library is free software; you can redistribute it and/or modify
029 *          it under the terms of the GNU Lesser General Public License as
030 *          published by the Free Software Foundation; either version 3 of the
031 *          License, or (at your option) any later version. <br>
032 * <br>
033 *          This library is distributed in the hope that it will be useful, but
034 *          WITHOUT ANY WARRANTY; without even the implied warranty of
035 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
036 *          Lesser General Public License for more details. <br>
037 * <br>
038 *          You should have received a copy of the GNU Lesser General Public
039 *          License along with this library; if not see
040 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
041 */
042public class PeriodSizePenalty extends ExamCriterion {
043    
044    @Override
045    public String getWeightName() {
046        return "Exams.PeriodSizeWeight";
047    }
048    
049    @Override
050    public String getXmlWeightName() {
051        return "periodSizeWeight";
052    }
053    
054    @Override
055    public double getWeightDefault(DataProperties config) {
056        return 1.0;
057    }
058    
059    @Override
060    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
061        return value.getPeriodPlacement().getPenalty() * (value.variable().getSize() + 1);
062    }
063    
064    @Override
065    public String getName() {
066        return "Period&times;Size Penalty";
067    }
068    
069    @Override
070    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
071        double[] bounds = new double[] { 0.0, 0.0 };
072        for (Exam exam : variables) {
073            if (!exam.getPeriodPlacements().isEmpty()) {
074                int minSizePenalty = Integer.MAX_VALUE, maxSizePenalty = Integer.MIN_VALUE;
075                for (ExamPeriodPlacement periodPlacement : exam.getPeriodPlacements()) {
076                    minSizePenalty = Math.min(minSizePenalty, periodPlacement.getPenalty() * (exam.getSize() + 1));
077                    maxSizePenalty = Math.max(maxSizePenalty, periodPlacement.getPenalty() * (exam.getSize() + 1));
078                }
079                bounds[0] += minSizePenalty;
080                bounds[1] += maxSizePenalty;
081            }
082        }
083        return bounds;
084    }
085    
086    @Override
087    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
088        if (getValue(assignment) != 0.0) {
089            info.put(getName(), sDoubleFormat.format(getValue(assignment) / assignment.nrAssignedVariables()));
090        }
091    }
092
093    @Override
094    public String toString(Assignment<Exam, ExamPlacement> assignment) {
095        return "PS:" + sDoubleFormat.format(getValue(assignment) / assignment.nrAssignedVariables());
096    }
097}