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