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.ExamPlacement;
007    import net.sf.cpsolver.ifs.util.DataProperties;
008    
009    /**
010     * Rotation penalty. I.e., an exam that has been in later period last times tries
011     * to be in an earlier period.
012     * <br><br>
013     * A weight for exam rotation penalty can be set by problem property
014     * Exams.RotationWeight, or in the input xml file, property examRotationWeight.
015     * 
016     * 
017     * <br>
018     * 
019     * @version ExamTT 1.2 (Examination Timetabling)<br>
020     *          Copyright (C) 2008 - 2012 Tomas Muller<br>
021     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
022     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
023     * <br>
024     *          This library is free software; you can redistribute it and/or modify
025     *          it under the terms of the GNU Lesser General Public License as
026     *          published by the Free Software Foundation; either version 3 of the
027     *          License, or (at your option) any later version. <br>
028     * <br>
029     *          This library is distributed in the hope that it will be useful, but
030     *          WITHOUT ANY WARRANTY; without even the implied warranty of
031     *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
032     *          Lesser General Public License for more details. <br>
033     * <br>
034     *          You should have received a copy of the GNU Lesser General Public
035     *          License along with this library; if not see
036     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
037     */
038    public class ExamRotationPenalty extends ExamCriterion {
039        private int iAssignedExamsWithAvgPeriod = 0;
040        private double iAveragePeriod = 0.0;
041        
042        @Override
043        public String getWeightName() {
044            return "Exams.RotationWeight";
045        }
046        
047        @Override
048        public String getXmlWeightName() {
049            return "examRotationWeight";
050        }
051        
052        @Override
053        public double getWeightDefault(DataProperties config) {
054            return 0.001;
055        }
056    
057        @Override
058        public double getValue(ExamPlacement value, Set<ExamPlacement> conflicts) {
059            if (value.variable().getAveragePeriod() < 0) return 0;
060            return (1 + value.getPeriod().getIndex()) * (1 + value.variable().getAveragePeriod());
061        }
062        
063        @Override
064        public void beforeUnassigned(long iteration, ExamPlacement value) {
065            super.beforeUnassigned(iteration, value);
066            if (value.variable().getAveragePeriod() >= 0) {
067                iAssignedExamsWithAvgPeriod --;
068                iAveragePeriod -= value.variable().getAveragePeriod();
069            }
070        }
071        
072        @Override
073        public void afterAssigned(long iteration, ExamPlacement value) {
074            super.afterAssigned(iteration, value);
075            if (value.variable().getAveragePeriod() >= 0) {
076                iAssignedExamsWithAvgPeriod ++;
077                iAveragePeriod += value.variable().getAveragePeriod();
078            }
079        }
080        
081        public int nrAssignedExamsWithAvgPeriod() {
082            return iAssignedExamsWithAvgPeriod;
083        }
084        
085        public double averagePeriod() {
086            return iAveragePeriod / iAssignedExamsWithAvgPeriod;
087        }
088        
089        @Override
090        public void getInfo(Map<String, String> info) {
091            if (getValue() != 0.0) {
092                info.put(getName(), sDoubleFormat.format(Math.sqrt(getValue() / iAssignedExamsWithAvgPeriod) - 1));
093            }
094        }
095        
096        @Override
097        public String toString() {
098            return "@P:" + sDoubleFormat.format((Math.sqrt(getValue() / iAssignedExamsWithAvgPeriod) - 1));
099        }
100    }