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.solver.Solver;
012import org.cpsolver.ifs.util.DataProperties;
013
014
015/**
016 * Cost for using room(s) that are too big. I.e., a difference between size of the assigned room and the size of the 
017 * smallest room in which the class can be placed {@link Lecture#minRoomSize()}.
018 * <br><br>
019 * A weight for room size penalty can be set by problem property Comparator.RoomSizeWeight.
020 * <br><br>
021 * The difference function can be made polynomial by using Comparator.RoomSizeFactor parameter
022 * (defaults to 1.05). The value of this criteria is then cubed by the power of this room
023 * size factor. This is to be able to favor a room swap between two classes at the same time,
024 * in which a smaller class takes a smaller room. To do this, set Comparator.RoomSizeFactor to
025 * a number bigger than one that is close to one (e.g., 1.05).
026 *   
027 * <br>
028 * 
029 * @version CourseTT 1.3 (University Course Timetabling)<br>
030 *          Copyright (C) 2006 - 2014 Tomas Muller<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 boolean init(Solver<Lecture, Placement> solver) {   
053        super.init(solver);
054        iRoomSizeFactor = solver.getProperties().getPropertyDouble("Comparator.RoomSizeFactor", 1.05);
055        return true; 
056    }
057    
058    @Override
059    public double getWeightDefault(DataProperties config) {
060        return config.getPropertyDouble("Comparator.RoomSizeWeight", 0.001);
061    }
062    
063    @Override
064    public String getPlacementSelectionWeightName() {
065        return "Placement.RoomSizeWeight";
066    }
067    
068    @Override
069    public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
070        if (value.variable().getNrRooms() <= 0) return 0.0;
071        double size = 0;
072        if (value.getRoomLocation() != null)
073            size = value.getRoomLocation().getRoomSize();
074        else if (value.getRoomLocations() != null) {
075            for (RoomLocation room: value.getRoomLocations())
076                size += room.getRoomSize();
077            size /= value.getNrRooms();
078        }
079        double diff = size - value.variable().minRoomSize();
080        return (diff < 0 ? 0 : Math.pow(diff, iRoomSizeFactor));
081    }
082    
083    @Override
084    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
085        if (getValue(assignment) != 0.0)
086            info.put(getName(), sDoubleFormat.format(Math.pow(getValue(assignment) / getModel().nrAssignedVariables(assignment), 1.0 / iRoomSizeFactor)));
087    }
088
089}