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.ExamPlacement;
009import org.cpsolver.exam.model.ExamRoomPlacement;
010import org.cpsolver.ifs.assignment.Assignment;
011
012
013/**
014 * Experimental criterion counting violations of room assignments. If this
015 * criterion is enabled, any room 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 rooms, set parameter Exam.SoftRooms to
019 * a weight that should be inferred by a prohibited room 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 RoomViolation extends ExamCriterion {
043    
044    @Override
045    public String getWeightName() {
046        return "Exam.SoftRooms";
047    }
048    
049    @Override
050    public String getXmlWeightName() {
051        return "softRooms";
052    }
053
054    @Override
055    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
056        double penalty = 0.0;
057        if (value.getRoomPlacements() != null)
058            for (ExamRoomPlacement r : value.getRoomPlacements()) {
059                penalty += (getWeight() == r.getPenalty() || getWeight() == r.getRoom().getPenalty(value.getPeriod()) ? 1.0 / value.getRoomPlacements().size() : 0.0);
060            }
061        return penalty;
062    }
063    
064    @Override
065    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
066        double[] bounds = new double[] { 0.0, 0.0 };
067        for (Exam exam : variables) {
068            if (!exam.getRoomPlacements().isEmpty()) {
069                rooms: for (ExamRoomPlacement roomPlacement : exam.getRoomPlacements()) {
070                    if (getWeight() == roomPlacement.getPenalty() && roomPlacement.getRoom().isAvailable()) {
071                        bounds[1] ++; break rooms;
072                    }
073                }
074            }
075        }
076        return bounds;
077    }
078
079    @Override
080    public String toString(Assignment<Exam, ExamPlacement> assignment) {
081        return (getValue(assignment) <= 0.0 ? "" : "!R:" + sDoubleFormat.format(getValue(assignment)));
082    }
083    
084    @Override
085    public boolean isPeriodCriterion() { return false; }
086}