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 * @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 RoomViolation extends ExamCriterion {
044    
045    @Override
046    public String getWeightName() {
047        return "Exam.SoftRooms";
048    }
049    
050    @Override
051    public String getXmlWeightName() {
052        return "softRooms";
053    }
054
055    @Override
056    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
057        double penalty = 0.0;
058        if (value.getRoomPlacements() != null)
059            for (ExamRoomPlacement r : value.getRoomPlacements()) {
060                penalty += (getWeight() == r.getPenalty() || getWeight() == r.getRoom().getPenalty(value.getPeriod()) ? 1.0 / value.getRoomPlacements().size() : 0.0);
061            }
062        return penalty;
063    }
064    
065    @Override
066    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
067        double[] bounds = new double[] { 0.0, 0.0 };
068        for (Exam exam : variables) {
069            if (!exam.getRoomPlacements().isEmpty()) {
070                rooms: for (ExamRoomPlacement roomPlacement : exam.getRoomPlacements()) {
071                    if (getWeight() == roomPlacement.getPenalty() && roomPlacement.getRoom().isAvailable()) {
072                        bounds[1] ++; break rooms;
073                    }
074                }
075            }
076        }
077        return bounds;
078    }
079
080    @Override
081    public String toString(Assignment<Exam, ExamPlacement> assignment) {
082        return (getValue(assignment) <= 0.0 ? "" : "!R:" + sDoubleFormat.format(getValue(assignment)));
083    }
084    
085    @Override
086    public boolean isPeriodCriterion() { return false; }
087}