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.ExamPlacement;
010import org.cpsolver.ifs.assignment.Assignment;
011import org.cpsolver.ifs.util.DataProperties;
012
013
014/**
015 * Number of direct instructor conflicts. I.e., number of cases when an
016 * exam is attended by an instructor that attends some other exam at the
017 * same period.
018 * <br><br>
019 * Direct instructor conflict weight can be set by problem property
020 * Exams.InstructorDirectConflictWeight, or in the input xml file, property
021 * instructorDirectConflictWeight.
022 * 
023 * <br>
024 * 
025 * @version ExamTT 1.3 (Examination Timetabling)<br>
026 *          Copyright (C) 2008 - 2014 Tomas Muller<br>
027 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
028 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
029 * <br>
030 *          This library is free software; you can redistribute it and/or modify
031 *          it under the terms of the GNU Lesser General Public License as
032 *          published by the Free Software Foundation; either version 3 of the
033 *          License, or (at your option) any later version. <br>
034 * <br>
035 *          This library is distributed in the hope that it will be useful, but
036 *          WITHOUT ANY WARRANTY; without even the implied warranty of
037 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
038 *          Lesser General Public License for more details. <br>
039 * <br>
040 *          You should have received a copy of the GNU Lesser General Public
041 *          License along with this library; if not see
042 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
043 */
044public class InstructorDirectConflicts extends StudentDirectConflicts {
045
046    @Override
047    public String getWeightName() {
048        return "Exams.InstructorDirectConflictWeight";
049    }
050    
051    @Override
052    public String getXmlWeightName() {
053        return "instructorDirectConflictWeight";
054    }
055
056    @Override
057    public double getWeightDefault(DataProperties config) {
058        return 1000.0;
059    }
060
061    @Override
062    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
063        Exam exam = value.variable();
064        int penalty = 0;
065        Map<ExamInstructor, Set<Exam>> instructors = ((ExamModel)getModel()).getInstructorsOfPeriod(assignment, value.getPeriod());
066        for (ExamInstructor s : exam.getInstructors()) {
067            Set<Exam> exams = instructors.get(s);
068            if (exams == null) continue;
069            int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1);
070            if (nrExams > 1)
071                penalty++;
072        }
073        /*
074        for (ExamInstructor s : exam.getInstructors()) {
075            Set<Exam> exams = s.getExams(assignment, value.getPeriod());
076            int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1);
077            if (nrExams > 1)
078                penalty++;
079        }
080        */
081        return penalty;
082    }
083
084    @Override
085    public String getName() {
086        return "Instructor Direct Conflicts";
087    }
088    
089    @Override
090    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
091        InstructorNotAvailableConflicts na = (InstructorNotAvailableConflicts)getModel().getCriterion(InstructorNotAvailableConflicts.class);
092        if (getValue(assignment) != 0.0 || (na != null && na.getValue(assignment) != 0.0))
093            info.put(getName(), sDoubleFormat.format(getValue(assignment) + (na == null ? 0.0 : na.getValue(assignment))) +
094                    (na == null || na.getValue(assignment) == 0.0 ? "" : " (" + sDoubleFormat.format(na.getValue(assignment)) + " N/A)"));
095    }
096    
097    @Override
098    public String toString(Assignment<Exam, ExamPlacement> assignment) {
099        return "iDC:" + sDoubleFormat.format(getValue(assignment));
100    }
101}