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