    001package org.cpsolver.exam.criteria;
    002
    003import java.util.Map;
    004import java.util.Set;
    005
    006import org.cpsolver.exam.model.Exam;
    007import org.cpsolver.exam.model.ExamModel;
    008import org.cpsolver.exam.model.ExamPlacement;
    009import org.cpsolver.exam.model.ExamRoom;
    010import org.cpsolver.exam.model.ExamRoomPlacement;
    011import org.cpsolver.ifs.assignment.Assignment;
    012import org.cpsolver.ifs.model.Model;
    013import org.cpsolver.ifs.util.DataProperties;
    014
    015
    016/**
    017 *
    018 * Cost for using room(s) that are too big. I.e., a difference between total room size
    019 * (computed using either {@link ExamRoom#getSize()} or {@link ExamRoom#getAltSize()} based
    020 * on {@link Exam#hasAltSeating()}) and the number of students {@link Exam#getSize()}.
    021 * <br><br>
    022 * A weight for room size penalty can be set by problem
    023 * property Exams.RoomSizeWeight, or in the input xml file, property
    024 * roomSizeWeight).
    025 * <br><br>
    026 * The difference function can be made polynomial by using Exams.RoomSizeFactor parameter
    027 * (defaults to 1.0). The value of this criteria is then cubed by the power of this room
    028 * size factor. This is to be able to favor a room swap between two exams at the same period,
    029 * in which a smaller exam takes a smaller room. To do this, set Exams.RoomSizeFactor to
    030 * a number bigger than one that is close to one (e.g., 1.05).
    031 *
    032 * <br>
    033 *
    034 * @author  Tomáš Müller
    035 * @version ExamTT 1.3 (Examination Timetabling)<br>
    036 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    037 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    038 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    039 * <br>
    040 *          This library is free software; you can redistribute it and/or modify
    041 *          it under the terms of the GNU Lesser General Public License as
    042 *          published by the Free Software Foundation; either version 3 of the
    043 *          License, or (at your option) any later version. <br>
    044 * <br>
    045 *          This library is distributed in the hope that it will be useful, but
    046 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    047 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    048 *          Lesser General Public License for more details. <br>
    049 * <br>
    050 *          You should have received a copy of the GNU Lesser General Public
    051 *          License along with this library; if not see
    052 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    053 */
    054public class RoomSizePenalty extends ExamCriterion {
    055    private double iRoomSizeFactor = 1.0;
    056
    057    @Override
    058    public void configure(DataProperties properties) {
    059        super.configure(properties);
    060        iRoomSizeFactor = properties.getPropertyDouble("Exams.RoomSizeFactor", 1.0);
    061    }
    062
    063    @Override
    064    public void setModel(Model<Exam, ExamPlacement> model) {
    065        super.setModel(model);
    066        iRoomSizeFactor = ((ExamModel)model).getProperties().getPropertyDouble("Exams.RoomSizeFactor", 1.0);
    067    }
    068
    069    @Override
    070    public String getWeightName() {
    071        return "Exams.RoomSizeWeight";
    072    }
    073
    074    @Override
    075    public String getXmlWeightName() {
    076        return "roomSizeWeight";
    077    }
    078
    079    @Override
    080    public double getWeightDefault(DataProperties config) {
    081        return 0.0001;
    082    }
    083
    084    @Override
    085    public void getXmlParameters(Map<String, String> params) {
    086        params.put(getXmlWeightName(), String.valueOf(getWeight()));
    087        params.put("roomSizeFactor", String.valueOf(iRoomSizeFactor));
    088    }
    089
    090    @Override
    091    public void setXmlParameters(Map<String, String> params) {
    092        try {
    093            setWeight(Double.valueOf(params.get(getXmlWeightName())));
    094        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
    095        try {
    096            iRoomSizeFactor = Double.valueOf(params.get("roomSizeFactor"));
    097        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
    098    }
    099
    100    @Override
    101    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    102        Exam exam = value.variable();
    103        int size = 0;
    104        if (value.getRoomPlacements() != null)
    105            for (ExamRoomPlacement r : value.getRoomPlacements()) {
    106                size += r.getSize(exam.hasAltSeating());
    107            }
    108        int diff = size - exam.getSize();
    109        return (diff < 0 ? 0 : Math.pow(diff, iRoomSizeFactor));
    110    }
    111
    112    @Override
    113    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
    114        if (getValue(assignment) != 0.0) {
    115            info.put(getName(), sDoubleFormat.format(getValue(assignment) / assignment.nrAssignedVariables()));
    116        }
    117    }
    118
    119    @Override
    120    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    121        return "RSz:" + sDoubleFormat.format(getValue(assignment) / assignment.nrAssignedVariables());
    122    }
    123
    124    @Override
    125    public boolean isPeriodCriterion() { return false; }
    126}