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