    001package org.cpsolver.exam.criteria.additional;
    002
    003import java.util.Collection;
    004import java.util.HashSet;
    005import java.util.Map;
    006import java.util.Set;
    007
    008import org.cpsolver.exam.criteria.ExamCriterion;
    009import org.cpsolver.exam.model.Exam;
    010import org.cpsolver.exam.model.ExamModel;
    011import org.cpsolver.exam.model.ExamPeriod;
    012import org.cpsolver.exam.model.ExamPlacement;
    013import org.cpsolver.exam.model.ExamStudent;
    014import org.cpsolver.ifs.assignment.Assignment;
    015import org.cpsolver.ifs.util.DataProperties;
    016
    017
    018/**
    019 * Number of more than One exam a day student conflicts. I.e., when an
    020 * exam is attended by a student student that attends one or more other
    021 * exams at the same day.
    022 * <br><br>
    023 * More than one exam a day student conflict weight can be set by
    024 * problem property Exams.MoreThanOneADayWeight, or in the input
    025 * xml file, property moreThanOneADayWeight.
    026 *
    027 * <br>
    028 *
    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 StudentMoreThan1ADayConflicts extends ExamCriterion {
    049
    050    @Override
    051    public String getWeightName() {
    052        return "Exams.MoreThanOneADayWeight";
    053    }
    054
    055    @Override
    056    public double getWeightDefault(DataProperties config) {
    057        return 1.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.isEmpty()) continue;
    069            int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1);
    070            if (nrExams > 1)
    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 1 A Day Conflicts";
    087    }
    088
    089    @Override
    090    public String getXmlWeightName() {
    091        return "moreThanOneADayWeight";
    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 > 1)
    111                        ret += nrExams - 1;
    112                }
    113            }
    114        }
    115        return ret;
    116    }
    117
    118    @Override
    119    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    120        return "M1D:" + sDoubleFormat.format(getValue(assignment));
    121    }
    122}