    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.ExamInstructor;
    008import org.cpsolver.exam.model.ExamModel;
    009import org.cpsolver.exam.model.ExamPeriod;
    010import org.cpsolver.exam.model.ExamPlacement;
    011import org.cpsolver.ifs.assignment.Assignment;
    012import org.cpsolver.ifs.util.DataProperties;
    013
    014
    015/**
    016 * Number of back-to-back distance instructor conflicts. I.e., number of
    017 * cases when an exam is attended by an instructor that attends some other
    018 * exam at the previous {@link ExamPeriod#prev()} or following
    019 * {@link ExamPeriod#next()} period and the distance
    020 * {@link ExamPlacement#getDistanceInMeters(ExamPlacement)} between these two exams
    021 * is greater than {@link ExamModel#getBackToBackDistance()}. Distance
    022 * back-to-back conflicts are only considered between consecutive periods
    023 * that are of the same day.
    024 * <br><br>
    025 * Distance back-to-back instructor conflict weight can be set by problem
    026 * property Exams.InstructorDistanceBackToBackConflictWeight, or in the
    027 * input xml file, property instructorDistanceBackToBackConflictWeight.
    028 *
    029 * <br>
    030 *
    031 * @author  Tomáš Müller
    032 * @version ExamTT 1.3 (Examination Timetabling)<br>
    033 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    034 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    035 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    036 * <br>
    037 *          This library is free software; you can redistribute it and/or modify
    038 *          it under the terms of the GNU Lesser General Public License as
    039 *          published by the Free Software Foundation; either version 3 of the
    040 *          License, or (at your option) any later version. <br>
    041 * <br>
    042 *          This library is distributed in the hope that it will be useful, but
    043 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    044 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    045 *          Lesser General Public License for more details. <br>
    046 * <br>
    047 *          You should have received a copy of the GNU Lesser General Public
    048 *          License along with this library; if not see
    049 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    050 */
    051public class InstructorDistanceBackToBackConflicts extends StudentDistanceBackToBackConflicts {
    052
    053    @Override
    054    public String getWeightName() {
    055        return "Exams.InstructorDistanceBackToBackConflictWeight";
    056    }
    057
    058    @Override
    059    public String getXmlWeightName() {
    060        return "instructorDistanceBackToBackConflictWeight";
    061    }
    062
    063    @Override
    064    public double getWeightDefault(DataProperties config) {
    065        return 25.0;
    066    }
    067
    068    @Override
    069    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    070        Exam exam = value.variable();
    071        double btbDist = getBackToBackDistance();
    072        if (btbDist < 0)
    073            return 0;
    074        int penalty = 0;
    075        ExamPeriod period = value.getPeriod();
    076        Map<ExamInstructor, Set<Exam>> prev = (period.prev() != null && period.prev().getDay() == period.getDay() ? ((ExamModel)getModel()).getInstructorsOfPeriod(assignment, period.prev()) : null);
    077        Map<ExamInstructor, Set<Exam>> next = (period.next() != null && period.next().getDay() == period.getDay() ? ((ExamModel)getModel()).getInstructorsOfPeriod(assignment, period.next()) : null);
    078        for (ExamInstructor s : exam.getInstructors()) {
    079            if (prev != null) {
    080                Set<Exam> exams = prev.get(s);
    081                if (exams != null)
    082                    for (Exam x : exams) {
    083                        if (x.equals(exam))
    084                            continue;
    085                        if (value.getDistanceInMeters(assignment.getValue(x)) > getBackToBackDistance())
    086                            penalty++;
    087                    }
    088            }
    089            if (next != null) {
    090                Set<Exam> exams = next.get(s);
    091                if (exams != null)
    092                    for (Exam x : exams) {
    093                        if (x.equals(exam))
    094                            continue;
    095                        if (value.getDistanceInMeters(assignment.getValue(x)) > getBackToBackDistance())
    096                            penalty++;
    097                    }
    098            }
    099        }
    100        /*
    101        for (ExamInstructor s : exam.getInstructors()) {
    102            if (period.prev() != null) {
    103                if (period.prev().getDay() == period.getDay()) {
    104                    for (Exam x : s.getExams(assignment, period.prev())) {
    105                        if (x.equals(exam))
    106                            continue;
    107                        if (value.getDistanceInMeters(assignment.getValue(x)) > btbDist)
    108                            penalty++;
    109                    }
    110                }
    111            }
    112            if (period.next() != null) {
    113                if (period.next().getDay() == period.getDay()) {
    114                    for (Exam x : s.getExams(assignment, period.next())) {
    115                        if (x.equals(exam))
    116                            continue;
    117                        if (value.getDistanceInMeters(assignment.getValue(x)) > btbDist)
    118                            penalty++;
    119                    }
    120                }
    121            }
    122        }
    123        */
    124        return penalty;
    125    }
    126
    127    @Override
    128    public String getName() {
    129        return "Instructor Distance Back-To-Back Conflicts";
    130    }
    131
    132    @Override
    133    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    134        return (getValue(assignment) <= 0.0 ? "" : "iBTBd:" + sDoubleFormat.format(getValue(assignment)));
    135    }
    136
    137    @Override
    138    public boolean isPeriodCriterion() { return false; }
    139}