001package org.cpsolver.coursett.criteria.additional;
002
003import java.util.Map;
004import java.util.Set;
005
006import org.cpsolver.coursett.criteria.TimetablingCriterion;
007import org.cpsolver.coursett.model.Lecture;
008import org.cpsolver.coursett.model.Placement;
009import org.cpsolver.coursett.model.RoomLocation;
010import org.cpsolver.ifs.assignment.Assignment;
011import org.cpsolver.ifs.util.DataProperties;
012
013
014/**
015 * Cost for using room(s) that are too big. I.e., a difference between size of the assigned room and the size of the 
016 * smallest room in which the class can be placed {@link Lecture#minRoomSize()}.
017 * <br><br>
018 * A weight for room size penalty can be set by problem property Comparator.RoomSizeWeight.
019 * <br><br>
020 * The difference function can be made polynomial by using Comparator.RoomSizeFactor parameter
021 * (defaults to 1.05). The value of this criteria is then cubed by the power of this room
022 * size factor. This is to be able to favor a room swap between two classes at the same time,
023 * in which a smaller class takes a smaller room. To do this, set Comparator.RoomSizeFactor to
024 * a number bigger than one that is close to one (e.g., 1.05).
025 *   
026 * <br>
027 * 
028 * @version CourseTT 1.3 (University Course Timetabling)<br>
029 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
030 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
031 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
032 * <br>
033 *          This library is free software; you can redistribute it and/or modify
034 *          it under the terms of the GNU Lesser General Public License as
035 *          published by the Free Software Foundation; either version 3 of the
036 *          License, or (at your option) any later version. <br>
037 * <br>
038 *          This library is distributed in the hope that it will be useful, but
039 *          WITHOUT ANY WARRANTY; without even the implied warranty of
040 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
041 *          Lesser General Public License for more details. <br>
042 * <br>
043 *          You should have received a copy of the GNU Lesser General Public
044 *          License along with this library; if not see
045 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
046 */
047public class RoomSizePenalty extends TimetablingCriterion {
048    private double iRoomSizeFactor = 1.0;
049
050    @Override
051    public void configure(DataProperties properties) {   
052        super.configure(properties);
053        iRoomSizeFactor = properties.getPropertyDouble("Comparator.RoomSizeFactor", 1.05);
054    }
055    
056    @Override
057    public double getWeightDefault(DataProperties config) {
058        return config.getPropertyDouble("Comparator.RoomSizeWeight", 0.001);
059    }
060    
061    @Override
062    public String getPlacementSelectionWeightName() {
063        return "Placement.RoomSizeWeight";
064    }
065    
066    @Override
067    public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
068        if (value.variable().getNrRooms() <= 0) return 0.0;
069        double size = 0;
070        if (value.getRoomLocation() != null)
071            size = value.getRoomLocation().getRoomSize();
072        else if (value.getRoomLocations() != null) {
073            for (RoomLocation room: value.getRoomLocations())
074                size += room.getRoomSize();
075            size /= value.getNrRooms();
076        }
077        double diff = size - value.variable().minRoomSize();
078        return (diff < 0 ? 0 : Math.pow(diff, iRoomSizeFactor));
079    }
080    
081    @Override
082    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
083        if (getValue(assignment) != 0.0)
084            info.put(getName(), sDoubleFormat.format(Math.pow(getValue(assignment) / getModel().nrAssignedVariables(assignment), 1.0 / iRoomSizeFactor)));
085    }
086
087}