    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 * Rotation penalty. I.e., an exam that has been in later period last times tries
    014 * to be in an earlier period.
    015 * <br><br>
    016 * A weight for exam rotation penalty can be set by problem property
    017 * Exams.RotationWeight, or in the input xml file, property examRotationWeight.
    018 *
    019 *
    020 * <br>
    021 *
    022 * @author  Tomáš Müller
    023 * @version ExamTT 1.3 (Examination Timetabling)<br>
    024 *          Copyright (C) 2008 - 2014 Tomáš Müller<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 */
    042public class ExamRotationPenalty extends ExamCriterion {
    043
    044    @Override
    045    public ValueContext createAssignmentContext(Assignment<Exam, ExamPlacement> assignment) {
    046        return new RotationContext(assignment);
    047    }
    048
    049    @Override
    050    public String getWeightName() {
    051        return "Exams.RotationWeight";
    052    }
    053
    054    @Override
    055    public String getXmlWeightName() {
    056        return "examRotationWeight";
    057    }
    058
    059    @Override
    060    public double getWeightDefault(DataProperties config) {
    061        return 0.001;
    062    }
    063
    064    @Override
    065    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    066        if (value.variable().getAveragePeriod() < 0) return 0;
    067        return (1 + value.getPeriod().getIndex()) * (1 + value.variable().getAveragePeriod());
    068    }
    069
    070    public int nrAssignedExamsWithAvgPeriod(Assignment<Exam, ExamPlacement> assignment) {
    071        return ((RotationContext)getContext(assignment)).nrAssignedExamsWithAvgPeriod();
    072    }
    073
    074    public double averagePeriod(Assignment<Exam, ExamPlacement> assignment) {
    075        return ((RotationContext)getContext(assignment)).averagePeriod();
    076    }
    077
    078    @Override
    079    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
    080        if (getValue(assignment) != 0.0) {
    081            info.put(getName(), sDoubleFormat.format(Math.sqrt(getValue(assignment) / nrAssignedExamsWithAvgPeriod(assignment)) - 1));
    082        }
    083    }
    084
    085    @Override
    086    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    087        return "@P:" + sDoubleFormat.format((Math.sqrt(getValue(assignment) / nrAssignedExamsWithAvgPeriod(assignment)) - 1));
    088    }
    089
    090    protected class RotationContext extends ValueContext {
    091        private int iAssignedExamsWithAvgPeriod = 0;
    092        private double iAveragePeriod = 0.0;
    093
    094        public RotationContext(Assignment<Exam, ExamPlacement> assignment) {
    095            super(assignment);
    096            for (Exam exam: getModel().variables())
    097                if (exam.getAveragePeriod() > 0) {
    098                    ExamPlacement placement = assignment.getValue(exam);
    099                    if (placement != null) {
    100                        iAssignedExamsWithAvgPeriod ++;
    101                        iAveragePeriod += exam.getAveragePeriod();
    102                    }
    103                }
    104        }
    105
    106        @Override
    107        public void assigned(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value) {
    108            super.assigned(assignment, value);
    109            if (value.variable().getAveragePeriod() >= 0) {
    110                iAssignedExamsWithAvgPeriod ++;
    111                iAveragePeriod += value.variable().getAveragePeriod();
    112            }
    113        }
    114
    115        @Override
    116        public void unassigned(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value) {
    117            super.unassigned(assignment, value);
    118            if (value.variable().getAveragePeriod() >= 0) {
    119                iAssignedExamsWithAvgPeriod --;
    120                iAveragePeriod -= value.variable().getAveragePeriod();
    121            }
    122        }
    123
    124        public int nrAssignedExamsWithAvgPeriod() {
    125            return iAssignedExamsWithAvgPeriod;
    126        }
    127
    128        public double averagePeriod() {
    129            return iAveragePeriod / iAssignedExamsWithAvgPeriod;
    130        }
    131    }
    132}