001package org.cpsolver.studentsct.report; 002 003import java.text.DecimalFormat; 004import java.util.ArrayList; 005import java.util.Comparator; 006import java.util.HashSet; 007import java.util.HashMap; 008import java.util.List; 009import java.util.Map; 010import java.util.Set; 011import java.util.TreeSet; 012 013import org.cpsolver.coursett.model.Placement; 014import org.cpsolver.coursett.model.RoomLocation; 015import org.cpsolver.ifs.assignment.Assignment; 016import org.cpsolver.ifs.util.CSVFile; 017import org.cpsolver.ifs.util.DataProperties; 018import org.cpsolver.ifs.util.DistanceMetric; 019import org.cpsolver.studentsct.StudentSectioningModel; 020import org.cpsolver.studentsct.extension.DistanceConflict; 021import org.cpsolver.studentsct.extension.DistanceConflict.Conflict; 022import org.cpsolver.studentsct.model.Course; 023import org.cpsolver.studentsct.model.CourseRequest; 024import org.cpsolver.studentsct.model.Enrollment; 025import org.cpsolver.studentsct.model.Request; 026import org.cpsolver.studentsct.model.Section; 027 028 029/** 030 * This class lists distance student conflicts in a {@link CSVFile} comma 031 * separated text file. Two sections that are attended by the same student are 032 * considered in a distance conflict if they are back-to-back taught in 033 * locations that are two far away. See {@link DistanceConflict} for more 034 * details. <br> 035 * <br> 036 * 037 * Each line represent a pair if classes that are in a distance conflict and have 038 * one or more students in common. 039 * 040 * <br> 041 * <br> 042 * 043 * Usage: new DistanceConflictTable(model),createTable(true, true).save(aFile); 044 * 045 * <br> 046 * <br> 047 * 048 * @version StudentSct 1.3 (Student Sectioning)<br> 049 * Copyright (C) 2007 - 2014 Tomáš Müller<br> 050 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 051 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 052 * <br> 053 * This library is free software; you can redistribute it and/or modify 054 * it under the terms of the GNU Lesser General Public License as 055 * published by the Free Software Foundation; either version 3 of the 056 * License, or (at your option) any later version. <br> 057 * <br> 058 * This library is distributed in the hope that it will be useful, but 059 * WITHOUT ANY WARRANTY; without even the implied warranty of 060 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 061 * Lesser General Public License for more details. <br> 062 * <br> 063 * You should have received a copy of the GNU Lesser General Public 064 * License along with this library; if not see 065 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 066 */ 067public class DistanceConflictTable extends AbstractStudentSectioningReport { 068 private static org.apache.logging.log4j.Logger sLog = org.apache.logging.log4j.LogManager.getLogger(DistanceConflictTable.class); 069 private static DecimalFormat sDF1 = new DecimalFormat("0.####"); 070 private static DecimalFormat sDF2 = new DecimalFormat("0.0000"); 071 072 private DistanceConflict iDC = null; 073 private DistanceMetric iDM = null; 074 075 /** 076 * Constructor 077 * 078 * @param model 079 * student sectioning model 080 */ 081 public DistanceConflictTable(StudentSectioningModel model) { 082 super(model); 083 iDC = model.getDistanceConflict(); 084 if (iDC == null) { 085 iDM = new DistanceMetric(model.getProperties()); 086 iDC = new DistanceConflict(iDM, model.getProperties()); 087 } else { 088 iDM = iDC.getDistanceMetric(); 089 } 090 } 091 092 /** 093 * Create report 094 * 095 * @param assignment current assignment 096 * @return report as comma separated text file 097 */ 098 @Override 099 public CSVFile createTable(Assignment<Request, Enrollment> assignment, DataProperties properties) { 100 CSVFile csv = new CSVFile(); 101 csv.setHeader(new CSVFile.CSVField[] { new CSVFile.CSVField("Course"), new CSVFile.CSVField("Total\nConflicts"), 102 new CSVFile.CSVField("Class"), new CSVFile.CSVField("Meeting Time"), new CSVFile.CSVField("Room"), 103 new CSVFile.CSVField("Distance\nConflicts"), new CSVFile.CSVField("% of Total\nConflicts"), 104 new CSVFile.CSVField("Conflicting\nClass"), new CSVFile.CSVField("Conflicting\nMeeting Time"), new CSVFile.CSVField("Conflicting\nRoom"), 105 new CSVFile.CSVField("Distance [m]"), new CSVFile.CSVField("Distance [min]"), new CSVFile.CSVField("Joined\nConflicts"), new CSVFile.CSVField("% of Total\nConflicts") 106 }); 107 108 Set<Conflict> confs = new HashSet<Conflict>(); 109 for (Request r1 : getModel().variables()) { 110 Enrollment e1 = assignment.getValue(r1); 111 if (e1 == null || !(r1 instanceof CourseRequest)) 112 continue; 113 confs.addAll(iDC.conflicts(e1)); 114 for (Request r2 : r1.getStudent().getRequests()) { 115 Enrollment e2 = assignment.getValue(r2); 116 if (e2 == null || r1.getId() >= r2.getId() || !(r2 instanceof CourseRequest)) 117 continue; 118 confs.addAll(iDC.conflicts(e1, e2)); 119 } 120 } 121 122 HashMap<Course, Set<Long>> totals = new HashMap<Course, Set<Long>>(); 123 HashMap<CourseSection, Map<CourseSection, Double>> conflictingPairs = new HashMap<CourseSection, Map<CourseSection,Double>>(); 124 HashMap<CourseSection, Set<Long>> sectionOverlaps = new HashMap<CourseSection, Set<Long>>(); 125 126 for (Conflict conflict : confs) { 127 if (!matches(conflict.getR1(), conflict.getE1())) continue; 128 Section s1 = conflict.getS1(), s2 = conflict.getS2(); 129 Course c1 = null, c2 = null; 130 Request r1 = null, r2 = null; 131 for (Request request : conflict.getStudent().getRequests()) { 132 Enrollment enrollment = assignment.getValue(request); 133 if (enrollment == null || !enrollment.isCourseRequest()) continue; 134 if (c1 == null && enrollment.getAssignments().contains(s1)) { 135 c1 = enrollment.getCourse(); 136 r1 = request; 137 Set<Long> total = totals.get(enrollment.getCourse()); 138 if (total == null) { 139 total = new HashSet<Long>(); 140 totals.put(enrollment.getCourse(), total); 141 } 142 total.add(enrollment.getStudent().getId()); 143 } 144 if (c2 == null && enrollment.getAssignments().contains(s2)) { 145 c2 = enrollment.getCourse(); 146 r2 = request; 147 Set<Long> total = totals.get(enrollment.getCourse()); 148 if (total == null) { 149 total = new HashSet<Long>(); 150 totals.put(enrollment.getCourse(), total); 151 } 152 total.add(enrollment.getStudent().getId()); 153 } 154 } 155 if (c1 == null) { 156 sLog.error("Unable to find a course for " + s1); 157 continue; 158 } 159 if (c2 == null) { 160 sLog.error("Unable to find a course for " + s2); 161 continue; 162 } 163 CourseSection a = new CourseSection(c1, s1); 164 CourseSection b = new CourseSection(c2, s2); 165 166 Set<Long> total = sectionOverlaps.get(a); 167 if (total == null) { 168 total = new HashSet<Long>(); 169 sectionOverlaps.put(a, total); 170 } 171 total.add(r1.getStudent().getId()); 172 Map<CourseSection, Double> pair = conflictingPairs.get(a); 173 if (pair == null) { 174 pair = new HashMap<CourseSection, Double>(); 175 conflictingPairs.put(a, pair); 176 } 177 Double prev = pair.get(b); 178 pair.put(b, r2.getWeight() + (prev == null ? 0.0 : prev.doubleValue())); 179 180 total = sectionOverlaps.get(b); 181 if (total == null) { 182 total = new HashSet<Long>(); 183 sectionOverlaps.put(b, total); 184 } 185 total.add(r2.getStudent().getId()); 186 pair = conflictingPairs.get(b); 187 if (pair == null) { 188 pair = new HashMap<CourseSection, Double>(); 189 conflictingPairs.put(b, pair); 190 } 191 prev = pair.get(a); 192 pair.put(a, r1.getWeight() + (prev == null ? 0.0 : prev.doubleValue())); 193 } 194 195 Comparator<Course> courseComparator = new Comparator<Course>() { 196 @Override 197 public int compare(Course a, Course b) { 198 int cmp = a.getName().compareTo(b.getName()); 199 if (cmp != 0) return cmp; 200 return a.getId() < b.getId() ? -1 : a.getId() == b.getId() ? 0 : 1; 201 } 202 }; 203 Comparator<Section> sectionComparator = new Comparator<Section>() { 204 @Override 205 public int compare(Section a, Section b) { 206 int cmp = a.getSubpart().getConfig().getOffering().getName().compareTo(b.getSubpart().getConfig().getOffering().getName()); 207 if (cmp != 0) return cmp; 208 cmp = a.getSubpart().getInstructionalType().compareTo(b.getSubpart().getInstructionalType()); 209 // if (cmp != 0) return cmp; 210 // cmp = a.getName().compareTo(b.getName()); 211 if (cmp != 0) return cmp; 212 return a.getId() < b.getId() ? -1 : a.getId() == b.getId() ? 0 : 1; 213 } 214 }; 215 216 TreeSet<Course> courses = new TreeSet<Course>(courseComparator); 217 courses.addAll(totals.keySet()); 218 for (Course course: courses) { 219 Set<Long> total = totals.get(course); 220 221 TreeSet<Section> sections = new TreeSet<Section>(sectionComparator); 222 for (Map.Entry<CourseSection, Set<Long>> entry: sectionOverlaps.entrySet()) 223 if (course.equals(entry.getKey().getCourse())) 224 sections.add(entry.getKey().getSection()); 225 226 boolean firstCourse = true; 227 for (Section section: sections) { 228 Set<Long> sectionOverlap = sectionOverlaps.get(new CourseSection(course, section)); 229 Map<CourseSection, Double> pair = conflictingPairs.get(new CourseSection(course, section)); 230 boolean firstClass = true; 231 232 String rooms = ""; 233 if (section.getRooms() != null) 234 for (RoomLocation r: section.getRooms()) { 235 if (!rooms.isEmpty()) rooms += "\n"; 236 rooms += r.getName(); 237 } 238 239 for (CourseSection other: new TreeSet<CourseSection>(pair.keySet())) { 240 List<CSVFile.CSVField> line = new ArrayList<CSVFile.CSVField>(); 241 line.add(new CSVFile.CSVField(firstCourse && firstClass ? course.getName() : "")); 242 line.add(new CSVFile.CSVField(firstCourse && firstClass ? total.size() : "")); 243 244 line.add(new CSVFile.CSVField(firstClass ? section.getSubpart().getName() + " " + section.getName(course.getId()): "")); 245 line.add(new CSVFile.CSVField(firstClass ? section.getTime() == null ? "" : section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(isUseAmPm()) + " - " + section.getTime().getEndTimeHeader(isUseAmPm()): "")); 246 247 line.add(new CSVFile.CSVField(firstClass ? rooms : "")); 248 249 line.add(new CSVFile.CSVField(firstClass && sectionOverlap != null ? String.valueOf(sectionOverlap.size()): "")); 250 line.add(new CSVFile.CSVField(firstClass && sectionOverlap != null ? sDF2.format(((double)sectionOverlap.size()) / total.size()) : "")); 251 252 line.add(new CSVFile.CSVField(other.getCourse().getName() + " " + other.getSection().getSubpart().getName() + " " + other.getSection().getName(other.getCourse().getId()))); 253 line.add(new CSVFile.CSVField(other.getSection().getTime().getDayHeader() + " " + other.getSection().getTime().getStartTimeHeader(isUseAmPm()) + " - " + other.getSection().getTime().getEndTimeHeader(isUseAmPm()))); 254 255 String or = ""; 256 if (other.getSection().getRooms() != null) 257 for (RoomLocation r: other.getSection().getRooms()) { 258 if (!or.isEmpty()) or += "\n"; 259 or += r.getName(); 260 } 261 line.add(new CSVFile.CSVField(or)); 262 263 line.add(new CSVFile.CSVField(sDF2.format(Placement.getDistanceInMeters(iDM, section.getPlacement(), other.getSection().getPlacement())))); 264 line.add(new CSVFile.CSVField(String.valueOf(Placement.getDistanceInMinutes(iDM, section.getPlacement(), other.getSection().getPlacement())))); 265 line.add(new CSVFile.CSVField(sDF1.format(pair.get(other)))); 266 line.add(new CSVFile.CSVField(sDF2.format(pair.get(other) / total.size()))); 267 268 csv.addLine(line); 269 firstClass = false; 270 } 271 firstCourse = false; 272 } 273 274 csv.addLine(); 275 } 276 277 278 return csv; 279 } 280}