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 * @author  Tomáš Müller
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 InstructorBackToBackConflicts extends StudentBackToBackConflicts {
051
052    @Override
053    public String getWeightName() {
054        return "Exams.InstructorBackToBackConflictWeight";
055    }
056    
057    @Override
058    public String getXmlWeightName() {
059        return "instructorBackToBackConflictWeight";
060    }
061    
062    @Override
063    public double getWeightDefault(DataProperties config) {
064        return 10.0;
065    }
066    
067    @Override
068    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
069        Exam exam = value.variable();
070        int penalty = 0;
071        ExamPeriod period = value.getPeriod();
072        Map<ExamInstructor, Set<Exam>> prev = (period.prev() != null && (isDayBreakBackToBack() || period.prev().getDay() == period.getDay()) ? ((ExamModel)getModel()).getInstructorsOfPeriod(assignment, period.prev()) : null);
073        Map<ExamInstructor, Set<Exam>> next = (period.next() != null && (isDayBreakBackToBack() || period.next().getDay() == period.getDay()) ? ((ExamModel)getModel()).getInstructorsOfPeriod(assignment, period.next()) : null);
074        for (ExamInstructor s : exam.getInstructors()) {
075            if (prev != null) {
076                Set<Exam> exams = prev.get(s);
077                if (exams != null) {
078                    int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
079                    penalty += nrExams;
080                }
081            }
082            if (next != null) {
083                Set<Exam> exams = next.get(s);
084                if (exams != null) {
085                    int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
086                    penalty += nrExams;
087                }
088            }
089        }
090        /*
091        for (ExamInstructor s : exam.getInstructors()) {
092            if (period.prev() != null) {
093                if (isDayBreakBackToBack() || period.prev().getDay() == period.getDay()) {
094                    Set<Exam> exams = s.getExams(assignment, period.prev());
095                    int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
096                    penalty += nrExams;
097                }
098            }
099            if (period.next() != null) {
100                if (isDayBreakBackToBack() || period.next().getDay() == period.getDay()) {
101                    Set<Exam> exams = s.getExams(assignment, period.next());
102                    int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
103                    penalty += nrExams;
104                }
105            }
106        }
107        */
108        return penalty;
109    }
110
111    @Override
112    public String getName() {
113        return "Instructor Back-To-Back Conflicts";
114    }
115    
116    @Override
117    public String toString(Assignment<Exam, ExamPlacement> assignment) {
118        return "iBTB:" + sDoubleFormat.format(getValue(assignment));
119    }
120
121}