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