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