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