    001package org.cpsolver.exam.criteria;
    002
    003import java.util.Collection;
    004import java.util.Set;
    005
    006import org.cpsolver.exam.criteria.additional.PeriodViolation;
    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.solver.Solver;
    012import org.cpsolver.ifs.util.DataProperties;
    013
    014
    015/**
    016 * Cost for using a period. See {@link ExamPeriodPlacement#getPenalty()} for more details.
    017 * <br><br>
    018 * A weight for period penalty can be set by problem property
    019 * Exams.PeriodWeight, or in the input xml file, property periodWeight.
    020 *
    021 * <br>
    022 *
    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 PeriodPenalty extends ExamCriterion {
    043    protected Integer iSoftPeriods = null;
    044
    045    @Override
    046    public boolean init(Solver<Exam, ExamPlacement> solver) {
    047        if (super.init(solver)) {
    048            iSoftPeriods = solver.getProperties().getPropertyInteger("Exam.SoftPeriods", null);
    049            if (iSoftPeriods != null) {
    050                PeriodViolation pv = new PeriodViolation();
    051                getModel().addCriterion(pv);
    052                return pv.init(solver);
    053            }
    054        }
    055        return true;
    056    }
    057
    058    @Override
    059    public String getWeightName() {
    060        return "Exams.PeriodWeight";
    061    }
    062
    063    @Override
    064    public String getXmlWeightName() {
    065        return "periodWeight";
    066    }
    067
    068    @Override
    069    public double getWeightDefault(DataProperties config) {
    070        return 1.0;
    071    }
    072
    073    @Override
    074    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    075        return (iSoftPeriods == null || (value.getPeriodPlacement().getExamPenalty() != iSoftPeriods &&  value.getPeriodPlacement().getPeriod().getPenalty() != iSoftPeriods) ? value.getPeriodPlacement().getPenalty() : 0.0);
    076    }
    077
    078    @Override
    079    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
    080        double[] bounds = new double[] { 0.0, 0.0 };
    081        for (Exam exam : variables) {
    082            if (!exam.getPeriodPlacements().isEmpty()) {
    083                int minPenalty = Integer.MAX_VALUE, maxPenalty = Integer.MIN_VALUE;
    084                for (ExamPeriodPlacement periodPlacement : exam.getPeriodPlacements()) {
    085                    if (iSoftPeriods != null && (periodPlacement.getExamPenalty() == iSoftPeriods || periodPlacement.getPeriod().getPenalty() == iSoftPeriods)) continue;
    086                    minPenalty = Math.min(minPenalty, periodPlacement.getPenalty());
    087                    maxPenalty = Math.max(maxPenalty, periodPlacement.getPenalty());
    088                }
    089                bounds[0] += minPenalty;
    090                bounds[1] += maxPenalty;
    091            }
    092        }
    093        return bounds;
    094    }
    095
    096    @Override
    097    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    098        return "PP:" + sDoubleFormat.format(getValue(assignment));
    099    }
    100}