    001package org.cpsolver.exam.criteria.additional;
    002
    003import java.util.Collection;
    004import java.util.Set;
    005
    006import org.cpsolver.exam.criteria.ExamCriterion;
    007import org.cpsolver.exam.model.Exam;
    008import org.cpsolver.exam.model.ExamPeriodPlacement;
    009import org.cpsolver.exam.model.ExamPlacement;
    010import org.cpsolver.ifs.assignment.Assignment;
    011
    012
    013/**
    014 * Experimental criterion counting violations of periods assignments. If this
    015 * criterion is enabled, any period can be assigned to an exam (not only those that are
    016 * in the domain of the exam).
    017 * <br><br>
    018 * To enable assignment of prohibited periods, set parameter Exam.SoftPeriods to
    019 * a weight that should be inferred by a prohibited period assignment.
    020 *
    021 * <br>
    022 *
    023 * @author  Tomáš Müller
    024 * @version ExamTT 1.3 (Examination Timetabling)<br>
    025 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    026 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    027 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    028 * <br>
    029 *          This library is free software; you can redistribute it and/or modify
    030 *          it under the terms of the GNU Lesser General Public License as
    031 *          published by the Free Software Foundation; either version 3 of the
    032 *          License, or (at your option) any later version. <br>
    033 * <br>
    034 *          This library is distributed in the hope that it will be useful, but
    035 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    036 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    037 *          Lesser General Public License for more details. <br>
    038 * <br>
    039 *          You should have received a copy of the GNU Lesser General Public
    040 *          License along with this library; if not see
    041 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    042 */
    043public class PeriodViolation extends ExamCriterion {
    044
    045    @Override
    046    public String getWeightName() {
    047        return "Exam.SoftPeriods";
    048    }
    049
    050    @Override
    051    public String getXmlWeightName() {
    052        return "softPeriods";
    053    }
    054
    055    @Override
    056    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    057        return (value.getPeriodPlacement().getExamPenalty() == getWeight() || value.getPeriodPlacement().getPeriod().getPenalty() == getWeight() ? 1.0 : 0.0);
    058    }
    059
    060    @Override
    061    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
    062        double[] bounds = new double[] { 0.0, 0.0 };
    063        for (Exam exam : variables) {
    064            if (!exam.getPeriodPlacements().isEmpty()) {
    065                for (ExamPeriodPlacement periodPlacement : exam.getPeriodPlacements()) {
    066                    if (periodPlacement.getExamPenalty() == getWeight() || periodPlacement.getPeriod().getPenalty() == getWeight()) {
    067                        bounds[1] ++; break;
    068                    }
    069                }
    070            }
    071        }
    072        return bounds;
    073    }
    074
    075    @Override
    076    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    077        return (getValue(assignment) <= 0.0 ? "" : "!P:" + sDoubleFormat.format(getValue(assignment)));
    078    }
    079
    080}