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