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