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