001package org.cpsolver.exam.criteria; 002 003import java.util.Collection; 004import java.util.Map; 005import java.util.Set; 006 007import org.cpsolver.exam.model.Exam; 008import org.cpsolver.exam.model.ExamModel; 009import org.cpsolver.exam.model.ExamPeriod; 010import org.cpsolver.exam.model.ExamPlacement; 011import org.cpsolver.exam.model.ExamStudent; 012import org.cpsolver.ifs.assignment.Assignment; 013import org.cpsolver.ifs.util.DataProperties; 014 015 016/** 017 * Number of direct student conflicts. I.e., number of cases when an 018 * exam is attended by a student that attends some other exam at the 019 * same period. 020 * <br><br> 021 * Direct student conflict weight can be set by problem property 022 * Exams.DirectConflictWeight, or in the input xml file, property 023 * directConflictWeight. 024 * 025 * <br> 026 * 027 * @author Tomáš Müller 028 * @version ExamTT 1.3 (Examination Timetabling)<br> 029 * Copyright (C) 2008 - 2014 Tomáš Müller<br> 030 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 031 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 032 * <br> 033 * This library is free software; you can redistribute it and/or modify 034 * it under the terms of the GNU Lesser General Public License as 035 * published by the Free Software Foundation; either version 3 of the 036 * License, or (at your option) any later version. <br> 037 * <br> 038 * This library is distributed in the hope that it will be useful, but 039 * WITHOUT ANY WARRANTY; without even the implied warranty of 040 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 041 * Lesser General Public License for more details. <br> 042 * <br> 043 * You should have received a copy of the GNU Lesser General Public 044 * License along with this library; if not see 045 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 046 */ 047public class StudentDirectConflicts extends ExamCriterion { 048 049 @Override 050 public String getWeightName() { 051 return "Exams.DirectConflictWeight"; 052 } 053 054 @Override 055 public String getXmlWeightName() { 056 return "directConflictWeight"; 057 } 058 059 @Override 060 public double getWeightDefault(DataProperties config) { 061 return 1000.0; 062 } 063 064 @Override 065 public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) { 066 Exam exam = value.variable(); 067 int penalty = 0; 068 ExamPeriod period = value.getPeriod(); 069 ExamModel m = (ExamModel)getModel(); 070 Map<ExamStudent, Set<Exam>> students = m.getStudentsOfPeriod(assignment, period); 071 for (ExamStudent s : exam.getStudents()) { 072 Set<Exam> exams = students.get(s); 073 if (exams == null) continue; 074 int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1); 075 if (nrExams > 1) 076 penalty++; 077 } 078 if (m.isCheckForPeriodOverlaps()) { 079 for (ExamPeriod p: m.getPeriods()) { 080 if (period.hasIntersection(p)) { 081 Map<ExamStudent, Set<Exam>> others = m.getStudentsOfPeriod(assignment, p); 082 s: for (ExamStudent s : exam.getStudents()) { 083 Set<Exam> exams = students.get(s); 084 if (exams == null || exams.size() + (exams.contains(exam) ? 0 : 1) <= 1) { 085 Set<Exam> other = others.get(s); 086 if (other != null && !other.isEmpty()) 087 for (Exam x: other) { 088 if (period.hasIntersection(exam, x, p)) { 089 penalty ++; continue s; 090 } 091 } 092 } 093 } 094 } 095 } 096 } 097 /* 098 for (ExamStudent s : exam.getStudents()) { 099 Set<Exam> exams = s.getExams(assignment, period); 100 if (exams.isEmpty()) continue; 101 int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1); 102 if (nrExams > 1) 103 penalty++; 104 } 105 */ 106 return penalty; 107 } 108 109 @Override 110 public String getName() { 111 return "Direct Conflicts"; 112 } 113 114 @Override 115 public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) { 116 StudentNotAvailableConflicts na = (StudentNotAvailableConflicts)getModel().getCriterion(StudentNotAvailableConflicts.class); 117 if (getValue(assignment) != 0.0 || (na != null && na.getValue(assignment) != 0.0)) 118 info.put(getName(), sDoubleFormat.format(getValue(assignment) + (na == null ? 0.0 : na.getValue(assignment))) + 119 (na == null || na.getValue(assignment) == 0.0 ? "" : " (" + sDoubleFormat.format(na.getValue(assignment)) + " N/A)")); 120 } 121 122 @Override 123 public double getValue(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) { 124 int ret = 0; 125 ExamModel m = (ExamModel)getModel(); 126 for (ExamPeriod p: m.getPeriods()) { 127 Map<ExamStudent, Set<Exam>> students = ((ExamModel)getModel()).getStudentsOfPeriod(assignment, p); 128 for (Set<Exam> exams: students.values()) { 129 int nrExams = exams.size(); 130 if (nrExams > 1) 131 ret += nrExams - 1; 132 } 133 } 134 if (m.isCheckForPeriodOverlaps()) { 135 for (ExamPeriod p: m.getPeriods()) { 136 for (ExamPeriod q: m.getPeriods()) { 137 if (p.getIndex() < q.getIndex() && p.hasIntersection(q)) { 138 Map<ExamStudent, Set<Exam>> s1 = m.getStudentsOfPeriod(assignment, p); 139 Map<ExamStudent, Set<Exam>> s2 = m.getStudentsOfPeriod(assignment, q); 140 for (Map.Entry<ExamStudent, Set<Exam>> e: s1.entrySet()) { 141 ExamStudent s = e.getKey(); 142 if (!e.getValue().isEmpty()) { 143 Set<Exam> x = s2.get(s); 144 if (x != null && x.isEmpty()) { 145 x1: for (Exam x1: e.getValue()) { 146 for (Exam x2: x) { 147 if (p.hasIntersection(x1, x2, q)) { 148 ret += 1; break x1; 149 } 150 } 151 } 152 } 153 } 154 } 155 } 156 } 157 } 158 } 159 return ret; 160 } 161 162 @Override 163 public String toString(Assignment<Exam, ExamPlacement> assignment) { 164 return "DC:" + sDoubleFormat.format(getValue(assignment)); 165 } 166}