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