001    package net.sf.cpsolver.coursett.criteria.additional;
002    
003    import java.util.Map;
004    import java.util.Set;
005    
006    import net.sf.cpsolver.coursett.criteria.TimetablingCriterion;
007    import net.sf.cpsolver.coursett.model.Lecture;
008    import net.sf.cpsolver.coursett.model.Placement;
009    import net.sf.cpsolver.coursett.model.RoomLocation;
010    import net.sf.cpsolver.ifs.solver.Solver;
011    import net.sf.cpsolver.ifs.util.DataProperties;
012    
013    /**
014     * Cost for using room(s) that are too big. I.e., a difference between size of the assigned room and the size of the 
015     * smallest room in which the class can be placed {@link Lecture#minRoomSize()}.
016     * <br><br>
017     * A weight for room size penalty can be set by problem property Comparator.RoomSizeWeight.
018     * <br><br>
019     * The difference function can be made polynomial by using Comparator.RoomSizeFactor parameter
020     * (defaults to 1.05). The value of this criteria is then cubed by the power of this room
021     * size factor. This is to be able to favor a room swap between two classes at the same time,
022     * in which a smaller class takes a smaller room. To do this, set Comparator.RoomSizeFactor to
023     * a number bigger than one that is close to one (e.g., 1.05).
024     *   
025     * <br>
026     * 
027     * @version CourseTT 1.2 (University Course Timetabling)<br>
028     *          Copyright (C) 2006 - 2013 Tomas Muller<br>
029     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
030     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
031     * <br>
032     *          This library is free software; you can redistribute it and/or modify
033     *          it under the terms of the GNU Lesser General Public License as
034     *          published by the Free Software Foundation; either version 3 of the
035     *          License, or (at your option) any later version. <br>
036     * <br>
037     *          This library is distributed in the hope that it will be useful, but
038     *          WITHOUT ANY WARRANTY; without even the implied warranty of
039     *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
040     *          Lesser General Public License for more details. <br>
041     * <br>
042     *          You should have received a copy of the GNU Lesser General Public
043     *          License along with this library; if not see
044     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
045     */
046    public class RoomSizePenalty extends TimetablingCriterion {
047        private double iRoomSizeFactor = 1.0;
048    
049        @Override
050        public boolean init(Solver<Lecture, Placement> solver) {   
051            super.init(solver);
052            iRoomSizeFactor = solver.getProperties().getPropertyDouble("Comparator.RoomSizeFactor", 1.05);
053            return true; 
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(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(Map<String, String> info) {
083            if (getValue() != 0.0)
084                info.put(getName(), sDoubleFormat.format(Math.pow(getValue() / getModel().nrAssignedVariables(), 1.0 / iRoomSizeFactor)));
085        }
086    
087    }