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 direct student conflicts. I.e., number of cases when an
017 * exam is attended by a student that attends some other exam at the
018 * same period.
019 * <br><br>
020 * Direct student conflict weight can be set by problem property
021 * Exams.DirectConflictWeight, or in the input xml file, property
022 * directConflictWeight.
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 StudentDirectConflicts extends ExamCriterion {
046
047    @Override
048    public String getWeightName() {
049        return "Exams.DirectConflictWeight";
050    }
051    
052    @Override
053    public String getXmlWeightName() {
054        return "directConflictWeight";
055    }
056
057    @Override
058    public double getWeightDefault(DataProperties config) {
059        return 1000.0;
060    }
061    
062    @Override
063    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
064        Exam exam = value.variable();
065        int penalty = 0;
066        ExamPeriod period = value.getPeriod();
067        Map<ExamStudent, Set<Exam>> students = ((ExamModel)getModel()).getStudentsOfPeriod(assignment, period);
068        for (ExamStudent s : exam.getStudents()) {
069            Set<Exam> exams = students.get(s);
070            if (exams == null) continue;
071            int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1);
072            if (nrExams > 1)
073                penalty++;
074        }
075        /*
076        for (ExamStudent s : exam.getStudents()) {
077            Set<Exam> exams = s.getExams(assignment, period);
078            if (exams.isEmpty()) continue;
079            int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1);
080            if (nrExams > 1)
081                penalty++;
082        }
083        */
084        return penalty;
085    }
086
087    @Override
088    public String getName() {
089        return "Direct Conflicts";
090    }
091    
092    @Override
093    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
094        StudentNotAvailableConflicts na = (StudentNotAvailableConflicts)getModel().getCriterion(StudentNotAvailableConflicts.class);
095        if (getValue(assignment) != 0.0 || (na != null && na.getValue(assignment) != 0.0))
096            info.put(getName(), sDoubleFormat.format(getValue(assignment) + (na == null ? 0.0 : na.getValue(assignment))) +
097                    (na == null || na.getValue(assignment) == 0.0 ? "" : " (" + sDoubleFormat.format(na.getValue(assignment)) + " N/A)"));
098    }
099
100    @Override
101    public String toString(Assignment<Exam, ExamPlacement> assignment) {
102        return "DC:" + sDoubleFormat.format(getValue(assignment));
103    }
104}