    001package org.cpsolver.exam.criteria.additional;
    002
    003import java.util.Collection;
    004import java.util.HashSet;
    005import java.util.Set;
    006
    007import org.cpsolver.exam.criteria.ExamCriterion;
    008import org.cpsolver.exam.model.Exam;
    009import org.cpsolver.exam.model.ExamDistributionConstraint;
    010import org.cpsolver.exam.model.ExamModel;
    011import org.cpsolver.exam.model.ExamPlacement;
    012import org.cpsolver.ifs.assignment.Assignment;
    013
    014
    015/**
    016 * Experimental criterion counting violations of hard distribution constraints.
    017 * <br><br>
    018 * To enable breaking of hard distribution constraints, set parameter Exam.SoftDistributions to
    019 * a weight that should be inferred by a hard distribution constraint being broken.
    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 DistributionViolation extends ExamCriterion {
    043
    044    @Override
    045    public String getWeightName() {
    046        return "Exam.SoftDistributions";
    047    }
    048
    049    @Override
    050    public String getXmlWeightName() {
    051        return "softDistributions";
    052    }
    053
    054    @Override
    055    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    056        int penalty = 0;
    057        for (ExamDistributionConstraint dc : value.variable().getDistributionConstraints()) {
    058            if (dc.isHard() || getWeight() != dc.getWeight())
    059                continue;
    060            boolean sat = dc.isSatisfied(assignment, value);
    061            if (sat != dc.isSatisfied(assignment))
    062                penalty += (sat ? -1.0 : 1.0);
    063        }
    064        return penalty;
    065    }
    066
    067    @Override
    068    protected double[] computeBounds(Assignment<Exam, ExamPlacement> assignment) {
    069        double[] bounds = new double[] { 0.0, 0.0 };
    070        for (ExamDistributionConstraint dc : ((ExamModel)getModel()).getDistributionConstraints()) {
    071            if (!dc.isHard() && getWeight() == dc.getWeight())
    072                bounds[1] ++;
    073        }
    074        return bounds;
    075    }
    076
    077    @Override
    078    public boolean isRoomCriterion() { return true; }
    079
    080    @Override
    081    public double getRoomValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value) {
    082        int penalty = 0;
    083        for (ExamDistributionConstraint dc : value.variable().getDistributionConstraints()) {
    084            if (dc.isHard() || getWeight() != dc.getWeight() || !dc.isRoomRelated())
    085                continue;
    086            boolean sat = dc.isSatisfied(assignment, value);
    087            if (sat != dc.isSatisfied(assignment))
    088                penalty += (sat ? -1.0 : 1.0);
    089        }
    090        return penalty;
    091    }
    092
    093    @Override
    094    public boolean isPeriodCriterion() { return true; }
    095
    096    @Override
    097    public double getPeriodValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value) {
    098        int penalty = 0;
    099        for (ExamDistributionConstraint dc : value.variable().getDistributionConstraints()) {
    100            if (dc.isHard() || getWeight() != dc.getWeight() || !dc.isPeriodRelated())
    101                continue;
    102            boolean sat = dc.isSatisfied(assignment, value);
    103            if (sat != dc.isSatisfied(assignment))
    104                penalty += (sat ? -1.0 : 1.0);
    105        }
    106        return penalty;
    107    }
    108
    109    @Override
    110    public double getValue(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
    111        int penalty = 0;
    112        Set<ExamDistributionConstraint> added = new HashSet<ExamDistributionConstraint>();
    113        for (Exam exam: variables) {
    114            for (ExamDistributionConstraint dc : exam.getDistributionConstraints()) {
    115                if (added.add(dc)) {
    116                    if (dc.isHard() || getWeight() != dc.getWeight())
    117                        continue;
    118                    if (!dc.isSatisfied(assignment))
    119                        penalty += 1;
    120                }
    121            }
    122        }
    123        return penalty;
    124    }
    125
    126    @Override
    127    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    128        return (getValue(assignment) <= 0.0 ? "" : "!D:" + sDoubleFormat.format(getValue(assignment)));
    129    }
    130}