001    package net.sf.cpsolver.exam.criteria;
002    
003    import java.util.Map;
004    import java.util.Set;
005    
006    import net.sf.cpsolver.exam.model.Exam;
007    import net.sf.cpsolver.exam.model.ExamPlacement;
008    import net.sf.cpsolver.ifs.solver.Solver;
009    import net.sf.cpsolver.ifs.util.DataProperties;
010    
011    /**
012     * Perturbation penalty. I.e., penalty for using a different examination period than
013     * initial. Only applicable when {@link PerturbationPenalty#isMPP()} is true (minimal
014     * perturbation problem).
015     * <br><br>
016     * A weight of perturbations (i.e., a penalty for an
017     * assignment of an exam to a place different from the initial one) can be
018     * set by problem property Exams.PerturbationWeight, or in the input xml
019     * file, property perturbationWeight).
020     * 
021     * <br>
022     * 
023     * @version ExamTT 1.2 (Examination Timetabling)<br>
024     *          Copyright (C) 2008 - 2012 Tomas Muller<br>
025     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
026     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
027     * <br>
028     *          This library is free software; you can redistribute it and/or modify
029     *          it under the terms of the GNU Lesser General Public License as
030     *          published by the Free Software Foundation; either version 3 of the
031     *          License, or (at your option) any later version. <br>
032     * <br>
033     *          This library is distributed in the hope that it will be useful, but
034     *          WITHOUT ANY WARRANTY; without even the implied warranty of
035     *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
036     *          Lesser General Public License for more details. <br>
037     * <br>
038     *          You should have received a copy of the GNU Lesser General Public
039     *          License along with this library; if not see
040     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
041     */
042    public class PerturbationPenalty extends ExamCriterion {
043        private boolean iMPP = false;
044        
045        @Override
046        public boolean init(Solver<Exam, ExamPlacement> solver) {
047            boolean ret = super.init(solver);
048            iMPP = solver.getProperties().getPropertyBoolean("General.MPP", iMPP);
049            return ret;
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(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() {
098            return (isMPP() ? "IP:" + sDoubleFormat.format(getValue()) : "");
099        }
100    }