    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.ExamModel;
    010import org.cpsolver.exam.model.ExamPeriod;
    011import org.cpsolver.exam.model.ExamPlacement;
    012import org.cpsolver.exam.model.ExamStudent;
    013import org.cpsolver.ifs.assignment.Assignment;
    014import org.cpsolver.ifs.util.DataProperties;
    015
    016
    017/**
    018 * Number of more than two exams a day student conflicts. I.e., when an
    019 * exam is attended by a student student that attends two or more other
    020 * exams at the same day.
    021 * <br><br>
    022 * More than two exams a day student conflict weight can be set by
    023 * problem property Exams.MoreThanTwoADayWeight, or in the input
    024 * xml file, property moreThanTwoADayWeight.
    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 StudentMoreThan2ADayConflicts extends ExamCriterion {
    049
    050    @Override
    051    public String getWeightName() {
    052        return "Exams.MoreThanTwoADayWeight";
    053    }
    054
    055    @Override
    056    public double getWeightDefault(DataProperties config) {
    057        return 100.0;
    058    }
    059
    060    @Override
    061    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    062        Exam exam = value.variable();
    063        int penalty = 0;
    064        ExamPeriod period = value.getPeriod();
    065        Map<ExamStudent, Set<Exam>> students = ((ExamModel)getModel()).getStudentsOfDay(assignment, period);
    066        for (ExamStudent s : exam.getStudents()) {
    067            Set<Exam> exams = students.get(s);
    068            if (exams == null || exams.size() < 2) continue;
    069            int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1);
    070            if (nrExams > 2)
    071                penalty++;
    072        }
    073        /*
    074        for (ExamStudent s : exam.getStudents()) {
    075            Set<Exam> exams = s.getExamsADay(assignment, period);
    076            int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1);
    077            if (nrExams > 2)
    078                penalty++;
    079        }
    080        */
    081        return penalty;
    082    }
    083
    084    @Override
    085    public String getName() {
    086        return "More Than 2 A Day Conflicts";
    087    }
    088
    089    @Override
    090    public String getXmlWeightName() {
    091        return "moreThanTwoADayWeight";
    092    }
    093
    094    @Override
    095    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
    096        if (getValue(assignment) != 0.0)
    097            info.put(getName(), sDoubleFormat.format(getValue(assignment)));
    098    }
    099
    100    @Override
    101    public double getValue(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
    102        int ret = 0;
    103        ExamModel m = (ExamModel)getModel();
    104        Set<Integer> days = new HashSet<Integer>();
    105        for (ExamPeriod p: m.getPeriods()) {
    106            if (days.add(p.getDay())) {
    107                Map<ExamStudent, Set<Exam>> students = ((ExamModel)getModel()).getStudentsOfDay(assignment, p);
    108                for (Set<Exam> exams: students.values()) {
    109                    int nrExams = exams.size();
    110                    if (nrExams > 2)
    111                        ret += nrExams - 2;
    112                }
    113            }
    114        }
    115        return ret;
    116    }
    117
    118    @Override
    119    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    120        return "M2D:" + sDoubleFormat.format(getValue(assignment));
    121    }
    122}