    001package org.cpsolver.exam.reports;
    002
    003import java.text.DecimalFormat;
    004import java.util.List;
    005
    006import org.cpsolver.exam.criteria.StudentBackToBackConflicts;
    007import org.cpsolver.exam.criteria.StudentDistanceBackToBackConflicts;
    008import org.cpsolver.exam.model.Exam;
    009import org.cpsolver.exam.model.ExamModel;
    010import org.cpsolver.exam.model.ExamPlacement;
    011import org.cpsolver.exam.model.ExamStudent;
    012import org.cpsolver.ifs.assignment.Assignment;
    013import org.cpsolver.ifs.util.CSVFile;
    014import org.cpsolver.ifs.util.CSVFile.CSVField;
    015
    016
    017/**
    018 * Export student back-to-back conflicts between pairs of exams into a CSV file. <br>
    019 * <br>
    020 * Usage:
    021 * <pre><code>
    022 * &nbsp;&nbsp;&nbsp;&nbsp;new ExamStudentBackToBackConflicts(model).report().save(file);
    023 * </code></pre>
    024 * <br>
    025 *
    026 * @version ExamTT 1.3 (Examination Timetabling)<br>
    027 *          Copyright (C) 2008 - 2014 Tomáš Müller<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 ExamStudentBackToBackConflicts {
    046    private ExamModel iModel = null;
    047
    048    /**
    049     * Constructor
    050     *
    051     * @param model
    052     *            examination timetabling model
    053     */
    054    public ExamStudentBackToBackConflicts(ExamModel model) {
    055        iModel = model;
    056    }
    057
    058    /**
    059     * generate report
    060     * @param assignment current assignment
    061     * @return resultant report
    062     */
    063    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
    064        CSVFile csv = new CSVFile();
    065        csv.setHeader(new CSVField[] { new CSVField("Exam 1"), new CSVField("Enrl 1"), new CSVField("Period 1"),
    066                new CSVField("Date 1"), new CSVField("Time 1"), new CSVField("Exam 2"), new CSVField("Enrl 2"),
    067                new CSVField("Back-To-Back"), new CSVField("Back-To-Back [%]"), new CSVField("Distance") });
    068        DecimalFormat df = new DecimalFormat("0.0");
    069        boolean isDayBreakBackToBack = ((StudentBackToBackConflicts)iModel.getCriterion(StudentBackToBackConflicts.class)).isDayBreakBackToBack();
    070        double backToBackDistance = ((StudentDistanceBackToBackConflicts)iModel.getCriterion(StudentDistanceBackToBackConflicts.class)).getBackToBackDistance();
    071        for (Exam ex1 : iModel.variables()) {
    072            ExamPlacement p1 = assignment.getValue(ex1);
    073            if (p1 == null || p1.getPeriod().next() == null)
    074                continue;
    075            if (!isDayBreakBackToBack && p1.getPeriod().getDay() != p1.getPeriod().next().getDay())
    076                continue;
    077            for (Exam ex2 : iModel.variables()) {
    078                ExamPlacement p2 = assignment.getValue(ex2);
    079                if (p2 == null || !p2.getPeriod().equals(p1.getPeriod().next()))
    080                    continue;
    081                List<ExamStudent> students = ex1.getJointEnrollments().get(ex2);
    082                if (students == null || students.isEmpty())
    083                    continue;
    084                String distStr = "";
    085                if (backToBackDistance >= 0) {
    086                    double dist = p1.getDistanceInMeters(p2);
    087                    if (dist > 0)
    088                        distStr = String.valueOf(dist);
    089                }
    090                csv
    091                        .addLine(new CSVField[] {
    092                                new CSVField(ex1.getName()),
    093                                new CSVField(ex1.getStudents().size()),
    094                                new CSVField(p1.getPeriod().getIndex() + 1),
    095                                new CSVField(p1.getPeriod().getDayStr()),
    096                                new CSVField(p1.getPeriod().getTimeStr()),
    097                                new CSVField(ex2.getName()),
    098                                new CSVField(ex2.getStudents().size()),
    099                                new CSVField(students.size()),
    100                                new CSVField(df.format(100.0 * students.size()
    101                                        / Math.min(ex1.getStudents().size(), ex2.getStudents().size()))),
    102                                new CSVField(distStr) });
    103            }
    104        }
    105        return csv;
    106    }
    107}