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 * @author  Tomáš Müller
029 * @version CourseTT 1.3 (University Course Timetabling)<br>
030 *          Copyright (C) 2006 - 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 RoomSizePenalty extends TimetablingCriterion {
049    private double iRoomSizeFactor = 1.0;
050
051    @Override
052    public void configure(DataProperties properties) {   
053        super.configure(properties);
054        iRoomSizeFactor = properties.getPropertyDouble("Comparator.RoomSizeFactor", 1.05);
055    }
056    
057    @Override
058    public double getWeightDefault(DataProperties config) {
059        return config.getPropertyDouble("Comparator.RoomSizeWeight", 0.001);
060    }
061    
062    @Override
063    public String getPlacementSelectionWeightName() {
064        return "Placement.RoomSizeWeight";
065    }
066    
067    @Override
068    public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
069        if (value.variable().getNrRooms() <= 0) return 0.0;
070        double size = 0;
071        if (value.getRoomLocation() != null)
072            size = value.getRoomLocation().getRoomSize();
073        else if (value.getRoomLocations() != null) {
074            for (RoomLocation room: value.getRoomLocations())
075                size += room.getRoomSize();
076            size /= value.getNrRooms();
077        }
078        double diff = size - value.variable().minRoomSize();
079        return (diff < 0 ? 0 : Math.pow(diff, iRoomSizeFactor));
080    }
081    
082    @Override
083    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
084        if (getValue(assignment) != 0.0)
085            info.put(getName(), sDoubleFormat.format(Math.pow(getValue(assignment) / getModel().nrAssignedVariables(assignment), 1.0 / iRoomSizeFactor)));
086    }
087
088}