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