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