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