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 * @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 PeriodViolation extends ExamCriterion {
043    
044    @Override
045    public String getWeightName() {
046        return "Exam.SoftPeriods";
047    }
048    
049    @Override
050    public String getXmlWeightName() {
051        return "softPeriods";
052    }
053    
054    @Override
055    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
056        return (value.getPeriodPlacement().getExamPenalty() == getWeight() || value.getPeriodPlacement().getPeriod().getPenalty() == getWeight() ? 1.0 : 0.0);
057    }
058    
059    @Override
060    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
061        double[] bounds = new double[] { 0.0, 0.0 };
062        for (Exam exam : variables) {
063            if (!exam.getPeriodPlacements().isEmpty()) {
064                for (ExamPeriodPlacement periodPlacement : exam.getPeriodPlacements()) {
065                    if (periodPlacement.getExamPenalty() == getWeight() || periodPlacement.getPeriod().getPenalty() == getWeight()) {
066                        bounds[1] ++; break;
067                    }
068                }
069            }
070        }
071        return bounds;
072    }
073
074    @Override
075    public String toString(Assignment<Exam, ExamPlacement> assignment) {
076        return (getValue(assignment) <= 0.0 ? "" : "!P:" + sDoubleFormat.format(getValue(assignment)));
077    }
078
079}