    001package net.sf.cpsolver.exam.criteria.additional;
    002
    003import java.util.Collection;
    004import java.util.Set;
    005
    006import net.sf.cpsolver.exam.criteria.ExamCriterion;
    007import net.sf.cpsolver.exam.model.Exam;
    008import net.sf.cpsolver.exam.model.ExamPeriodPlacement;
    009import net.sf.cpsolver.exam.model.ExamPlacement;
    010
    011/**
    012 * Experimental criterion counting violations of periods assignments. If this
    013 * criterion is enabled, any period can be assigned to an exam (not only those that are
    014 * in the domain of the exam).
    015 * <br><br>
    016 * To enable assignment of prohibited periods, set parameter Exam.SoftPeriods to
    017 * a weight that should be inferred by a prohibited period assignment.
    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 PeriodViolation extends ExamCriterion {
    041
    042    @Override
    043    public String getWeightName() {
    044        return "Exam.SoftPeriods";
    045    }
    046
    047    @Override
    048    public String getXmlWeightName() {
    049        return "softPeriods";
    050    }
    051
    052    @Override
    053    public double getValue(ExamPlacement value, Set<ExamPlacement> conflicts) {
    054        return (value.getPeriodPlacement().getExamPenalty() == getWeight() || value.getPeriodPlacement().getPeriod().getPenalty() == getWeight() ? 1.0 : 0.0);
    055    }
    056
    057    @Override
    058    public double[] getBounds(Collection<Exam> variables) {
    059        double[] bounds = new double[] { 0.0, 0.0 };
    060        for (Exam exam : variables) {
    061            if (!exam.getPeriodPlacements().isEmpty()) {
    062                for (ExamPeriodPlacement periodPlacement : exam.getPeriodPlacements()) {
    063                    if (periodPlacement.getExamPenalty() == getWeight() || periodPlacement.getPeriod().getPenalty() == getWeight()) {
    064                        bounds[1] ++; break;
    065                    }
    066                }
    067            }
    068        }
    069        return bounds;
    070    }
    071
    072    @Override
    073    public String toString() {
    074        return (getValue() <= 0.0 ? "" : "!P:" + sDoubleFormat.format(getValue()));
    075    }
    076
    077}