    001package org.cpsolver.exam.criteria;
    002
    003import java.util.Collection;
    004import java.util.Set;
    005
    006import org.cpsolver.exam.criteria.additional.RoomViolation;
    007import org.cpsolver.exam.model.Exam;
    008import org.cpsolver.exam.model.ExamModel;
    009import org.cpsolver.exam.model.ExamPeriod;
    010import org.cpsolver.exam.model.ExamPlacement;
    011import org.cpsolver.exam.model.ExamRoom;
    012import org.cpsolver.exam.model.ExamRoomPlacement;
    013import org.cpsolver.ifs.assignment.Assignment;
    014import org.cpsolver.ifs.solver.Solver;
    015import org.cpsolver.ifs.util.DataProperties;
    016
    017
    018/**
    019 * Room penalty (penalty for using given rooms). I.e., sum of
    020 * {@link ExamRoomPlacement#getPenalty(ExamPeriod)} of assigned rooms.
    021 * <br><br>
    022 * A weight for room penalty can be set by problem property
    023 * Exams.RoomPreferenceWeight, or in the input xml file, property
    024 * roomWeight).
    025 *
    026 * <br>
    027 *
    028 * @author  Tomáš Müller
    029 * @version ExamTT 1.3 (Examination Timetabling)<br>
    030 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    031 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    032 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    033 * <br>
    034 *          This library is free software; you can redistribute it and/or modify
    035 *          it under the terms of the GNU Lesser General Public License as
    036 *          published by the Free Software Foundation; either version 3 of the
    037 *          License, or (at your option) any later version. <br>
    038 * <br>
    039 *          This library is distributed in the hope that it will be useful, but
    040 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    041 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    042 *          Lesser General Public License for more details. <br>
    043 * <br>
    044 *          You should have received a copy of the GNU Lesser General Public
    045 *          License along with this library; if not see
    046 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    047 */
    048public class RoomPenalty extends ExamCriterion {
    049    protected Integer iSoftRooms = null;
    050
    051    @Override
    052    public boolean init(Solver<Exam, ExamPlacement> solver) {
    053        if (super.init(solver)) {
    054            iSoftRooms = solver.getProperties().getPropertyInteger("Exam.SoftRooms", null);
    055            if (iSoftRooms != null) {
    056                RoomViolation rv = new RoomViolation();
    057                getModel().addCriterion(rv);
    058                return rv.init(solver);
    059            }
    060        }
    061        return true;
    062    }
    063
    064    @Override
    065    public String getWeightName() {
    066        return "Exams.RoomWeight";
    067    }
    068
    069    @Override
    070    public String getXmlWeightName() {
    071        return "roomWeight";
    072    }
    073
    074    @Override
    075    public double getWeightDefault(DataProperties config) {
    076        return 0.1;
    077    }
    078
    079    @Override
    080    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    081        double penalty = 0.0;
    082        if (value.getRoomPlacements() != null)
    083            for (ExamRoomPlacement r : value.getRoomPlacements()) {
    084                penalty += (iSoftRooms != null && (iSoftRooms == r.getPenalty() || iSoftRooms == r.getPenalty(value.getPeriod())) ? 0.0 : r.getPenalty(value.getPeriod()));
    085            }
    086        return penalty;
    087    }
    088
    089    private int getMinPenalty(ExamRoom r) {
    090        int min = Integer.MAX_VALUE;
    091        for (ExamPeriod p : ((ExamModel)getModel()).getPeriods()) {
    092            if (r.isAvailable(p) && (iSoftRooms == null || r.getPenalty(p) != iSoftRooms)) {
    093                min = Math.min(min, r.getPenalty(p));
    094            }
    095        }
    096        return min;
    097    }
    098
    099    private int getMaxPenalty(ExamRoom r) {
    100        int max = Integer.MIN_VALUE;
    101        for (ExamPeriod p : ((ExamModel)getModel()).getPeriods()) {
    102            if (r.isAvailable(p) && (iSoftRooms == null || r.getPenalty(p) != iSoftRooms)) {
    103                max = Math.max(max, r.getPenalty(p));
    104            }
    105        }
    106        return max;
    107    }
    108
    109    private boolean isAvailable(ExamRoom r) {
    110        for (ExamPeriod p : ((ExamModel)getModel()).getPeriods()) {
    111            if (r.isAvailable(p) && (iSoftRooms == null || r.getPenalty(p) != iSoftRooms))
    112                return true;
    113        }
    114        return false;
    115    }
    116
    117    @Override
    118    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
    119        double[] bounds = new double[] { 0.0, 0.0 };
    120        for (Exam exam : variables) {
    121            if (!exam.getRoomPlacements().isEmpty()) {
    122                int minPenalty = Integer.MAX_VALUE, maxPenalty = Integer.MIN_VALUE;
    123                for (ExamRoomPlacement roomPlacement : exam.getRoomPlacements()) {
    124                    if (iSoftRooms != null && iSoftRooms == roomPlacement.getPenalty()) continue;
    125                    if (!isAvailable(roomPlacement.getRoom())) continue;
    126                    minPenalty = Math.min(minPenalty, 2 * roomPlacement.getPenalty() + getMinPenalty(roomPlacement.getRoom()));
    127                    maxPenalty = Math.max(maxPenalty, 2 * roomPlacement.getPenalty() + getMaxPenalty(roomPlacement.getRoom()));
    128                }
    129                bounds[0] += minPenalty;
    130                bounds[1] += maxPenalty;
    131            }
    132        }
    133        return bounds;
    134    }
    135
    136    @Override
    137    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    138        return "RP:" + sDoubleFormat.format(getValue(assignment));
    139    }
    140
    141    @Override
    142    public boolean isPeriodCriterion() { return false; }
    143}