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.ExamPlacement; 008import org.cpsolver.ifs.assignment.Assignment; 009import org.cpsolver.ifs.util.DataProperties; 010 011 012/** 013 * Perturbation penalty. I.e., penalty for using a different examination period than 014 * initial. Only applicable when {@link PerturbationPenalty#isMPP()} is true (minimal 015 * perturbation problem). 016 * <br><br> 017 * A weight of perturbations (i.e., a penalty for an 018 * assignment of an exam to a place different from the initial one) can be 019 * set by problem property Exams.PerturbationWeight, or in the input xml 020 * file, property perturbationWeight). 021 * 022 * <br> 023 * 024 * @author Tomáš Müller 025 * @version ExamTT 1.3 (Examination Timetabling)<br> 026 * Copyright (C) 2008 - 2014 Tomáš Müller<br> 027 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 028 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 029 * <br> 030 * This library is free software; you can redistribute it and/or modify 031 * it under the terms of the GNU Lesser General Public License as 032 * published by the Free Software Foundation; either version 3 of the 033 * License, or (at your option) any later version. <br> 034 * <br> 035 * This library is distributed in the hope that it will be useful, but 036 * WITHOUT ANY WARRANTY; without even the implied warranty of 037 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 038 * Lesser General Public License for more details. <br> 039 * <br> 040 * You should have received a copy of the GNU Lesser General Public 041 * License along with this library; if not see 042 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 043 */ 044public class PerturbationPenalty extends ExamCriterion { 045 private boolean iMPP = false; 046 047 @Override 048 public void configure(DataProperties properties) { 049 super.configure(properties); 050 iMPP = properties.getPropertyBoolean("General.MPP", iMPP); 051 } 052 053 @Override 054 public String getWeightName() { 055 return "Exams.PerturbationWeight"; 056 } 057 058 @Override 059 public String getXmlWeightName() { 060 return "perturbationWeight"; 061 } 062 063 @Override 064 public double getWeightDefault(DataProperties config) { 065 return 0.01; 066 } 067 068 public boolean isMPP() { 069 return iMPP; 070 } 071 072 @Override 073 public void getXmlParameters(Map<String, String> params) { 074 params.put(getXmlWeightName(), String.valueOf(getWeight())); 075 params.put("mpp", isMPP() ? "true" : "false"); 076 } 077 078 @Override 079 public void setXmlParameters(Map<String, String> params) { 080 try { 081 setWeight(Double.valueOf(params.get(getXmlWeightName()))); 082 } catch (NumberFormatException e) {} catch (NullPointerException e) {} 083 try { 084 iMPP = "true".equals(params.get("mpp")); 085 } catch (NumberFormatException e) {} catch (NullPointerException e) {} 086 } 087 088 @Override 089 public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) { 090 if (!isMPP()) return 0; 091 Exam exam = value.variable(); 092 ExamPlacement initial = exam.getInitialAssignment(); 093 if (initial == null) return 0; 094 return Math.abs(initial.getPeriod().getIndex() - value.getPeriod().getIndex()) * (1 + exam.getSize()); 095 } 096 097 @Override 098 public String toString(Assignment<Exam, ExamPlacement> assignment) { 099 return (isMPP() ? "IP:" + sDoubleFormat.format(getValue(assignment)) : ""); 100 } 101}