001    package net.sf.cpsolver.exam.criteria;
002    
003    import java.util.Set;
004    
005    import net.sf.cpsolver.exam.model.Exam;
006    import net.sf.cpsolver.exam.model.ExamPeriodPlacement;
007    import net.sf.cpsolver.exam.model.ExamPlacement;
008    import net.sf.cpsolver.ifs.util.DataProperties;
009    
010    /**
011     * A weight for period penalty (used in
012     * {@link ExamPeriodPlacement#getPenalty()} multiplied by examination size
013     * {@link Exam#getSize()}. Can be set by problem property
014     * Exams.PeriodSizeWeight, or in the input xml file, property periodSizeWeight).
015     * 
016     * <br>
017     * 
018     * @version ExamTT 1.2 (Examination Timetabling)<br>
019     *          Copyright (C) 2008 - 2012 Tomas Muller<br>
020     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
021     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
022     * <br>
023     *          This library is free software; you can redistribute it and/or modify
024     *          it under the terms of the GNU Lesser General Public License as
025     *          published by the Free Software Foundation; either version 3 of the
026     *          License, or (at your option) any later version. <br>
027     * <br>
028     *          This library is distributed in the hope that it will be useful, but
029     *          WITHOUT ANY WARRANTY; without even the implied warranty of
030     *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
031     *          Lesser General Public License for more details. <br>
032     * <br>
033     *          You should have received a copy of the GNU Lesser General Public
034     *          License along with this library; if not see
035     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
036     */
037    public class PeriodSizePenalty extends ExamCriterion {
038        
039        @Override
040        public String getWeightName() {
041            return "Exams.PeriodSizeWeight";
042        }
043        
044        @Override
045        public String getXmlWeightName() {
046            return "periodSizeWeight";
047        }
048        
049        @Override
050        public double getWeightDefault(DataProperties config) {
051            return 1.0;
052        }
053        
054        @Override
055        public double getValue(ExamPlacement value, Set<ExamPlacement> conflicts) {
056            return value.getPeriodPlacement().getPenalty() * (value.variable().getSize() + 1);
057        }
058        
059        @Override
060        public String getName() {
061            return "Period&times;Size Penalty";
062        }
063        
064        @Override
065        protected void computeBounds() {
066            iBounds = new double[] { 0.0, 0.0 };
067            for (Exam exam : getModel().variables()) {
068                if (!exam.getPeriodPlacements().isEmpty()) {
069                    int minSizePenalty = Integer.MAX_VALUE, maxSizePenalty = Integer.MIN_VALUE;
070                    for (ExamPeriodPlacement periodPlacement : exam.getPeriodPlacements()) {
071                        minSizePenalty = Math.min(minSizePenalty, periodPlacement.getPenalty() * (exam.getSize() + 1));
072                        maxSizePenalty = Math.max(maxSizePenalty, periodPlacement.getPenalty() * (exam.getSize() + 1));
073                    }
074                    iBounds[0] += minSizePenalty;
075                    iBounds[1] += maxSizePenalty;
076                }
077            }
078        }
079    
080        @Override
081        public String toString() {
082            return "PS:" + sDoubleFormat.format(getWeightedValue());
083        }
084    }