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