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.solver.Solver;
010import org.cpsolver.ifs.util.DataProperties;
011
012
013/**
014 * Perturbation penalty. I.e., penalty for using a different examination period than
015 * initial. Only applicable when {@link PerturbationPenalty#isMPP()} is true (minimal
016 * perturbation problem).
017 * <br><br>
018 * A weight of perturbations (i.e., a penalty for an
019 * assignment of an exam to a place different from the initial one) can be
020 * set by problem property Exams.PerturbationWeight, or in the input xml
021 * file, property perturbationWeight).
022 * 
023 * <br>
024 * 
025 * @version ExamTT 1.3 (Examination Timetabling)<br>
026 *          Copyright (C) 2008 - 2014 Tomas Muller<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 boolean init(Solver<Exam, ExamPlacement> solver) {
049        boolean ret = super.init(solver);
050        iMPP = solver.getProperties().getPropertyBoolean("General.MPP", iMPP);
051        return ret;
052    }
053    
054    @Override
055    public String getWeightName() {
056        return "Exams.PerturbationWeight";
057    }
058    
059    @Override
060    public String getXmlWeightName() {
061        return "perturbationWeight";
062    }
063    
064    @Override
065    public double getWeightDefault(DataProperties config) {
066        return 0.01;
067    }
068    
069    public boolean isMPP() {
070        return iMPP;
071    }
072    
073    @Override
074    public void getXmlParameters(Map<String, String> params) {
075        params.put(getXmlWeightName(), String.valueOf(getWeight()));
076        params.put("mpp", isMPP() ? "true" : "false");
077    }
078    
079    @Override
080    public void setXmlParameters(Map<String, String> params) {
081        try {
082            setWeight(Double.valueOf(params.get(getXmlWeightName())));
083        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
084        try {
085            iMPP = "true".equals(params.get("mpp"));
086        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
087    }
088
089    @Override
090    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
091        if (!isMPP()) return 0;
092        Exam exam = value.variable();
093        ExamPlacement initial = exam.getInitialAssignment();
094        if (initial == null) return 0;
095        return Math.abs(initial.getPeriod().getIndex() - value.getPeriod().getIndex()) * (1 + exam.getSize());
096    }
097
098    @Override
099    public String toString(Assignment<Exam, ExamPlacement> assignment) {
100        return (isMPP() ? "IP:" + sDoubleFormat.format(getValue(assignment)) : "");
101    }
102}