    001package org.cpsolver.exam.reports;
    002
    003import java.text.DecimalFormat;
    004import java.util.ArrayList;
    005import java.util.List;
    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.ifs.assignment.Assignment;
    012import org.cpsolver.ifs.util.CSVFile;
    013import org.cpsolver.ifs.util.CSVFile.CSVField;
    014
    015
    016/**
    017 * Export period usage into CSV file. <br>
    018 * <br>
    019 * Usage:
    020 * <pre><code>
    021 * &nbsp;&nbsp;&nbsp;&nbsp;new ExamPeriodUsage(model).report().save(file);
    022 * </code></pre>
    023 * <br>
    024 *
    025 * @version ExamTT 1.3 (Examination Timetabling)<br>
    026 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    027 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    028 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    029 * <br>
    030 *          This library is free software; you can redistribute it and/or modify
    031 *          it under the terms of the GNU Lesser General Public License as
    032 *          published by the Free Software Foundation; either version 3 of the
    033 *          License, or (at your option) any later version. <br>
    034 * <br>
    035 *          This library is distributed in the hope that it will be useful, but
    036 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    037 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    038 *          Lesser General Public License for more details. <br>
    039 * <br>
    040 *          You should have received a copy of the GNU Lesser General Public
    041 *          License along with this library; if not see
    042 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    043 */
    044public class ExamPeriodUsage {
    045    private ExamModel iModel = null;
    046    /** Exam enrollment limits */
    047    public static int[] sLimits = new int[] { 10, 50, 100, 200 };
    048    private static DecimalFormat sDF = new DecimalFormat("0.00");
    049
    050    /**
    051     * Constructor
    052     *
    053     * @param model
    054     *            examination timetabling model
    055     */
    056    public ExamPeriodUsage(ExamModel model) {
    057        iModel = model;
    058    }
    059
    060    /**
    061     * generate report
    062     * @param assignment current assignment
    063     * @return resultant report
    064     */
    065    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
    066        CSVFile csv = new CSVFile();
    067        List<CSVField> header = new ArrayList<CSVField>();
    068        header.add(new CSVField("Period"));
    069        header.add(new CSVField("Date"));
    070        header.add(new CSVField("Time"));
    071        header.add(new CSVField("Weight"));
    072        header.add(new CSVField("NrExams"));
    073        header.add(new CSVField("Students"));
    074        for (int i = 0; i < sLimits.length; i++) {
    075            header.add(new CSVField("NrExams>=" + sLimits[i]));
    076        }
    077        header.add(new CSVField("AvgPeriod"));
    078        header.add(new CSVField("WgAvgPeriod"));
    079        csv.setHeader(header);
    080        for (ExamPeriod period : iModel.getPeriods()) {
    081            int nrExams = 0;
    082            int nrStudents = 0;
    083            int[] nrExamsLim = new int[sLimits.length];
    084            int totAvgPer = 0, nrAvgPer = 0, totWgAvgPer = 0;
    085            for (int i = 0; i < sLimits.length; i++)
    086                nrExamsLim[i] = 0;
    087            for (Exam exam : iModel.variables()) {
    088                ExamPlacement placement = assignment.getValue(exam);
    089                if (placement == null || !(placement.getPeriod().equals(period)))
    090                    continue;
    091                nrExams++;
    092                nrStudents += exam.getStudents().size();
    093                if (exam.getAveragePeriod() >= 0) {
    094                    totAvgPer += exam.getAveragePeriod();
    095                    nrAvgPer++;
    096                    totWgAvgPer += exam.getAveragePeriod() * exam.getStudents().size();
    097                }
    098                for (int i = 0; i < sLimits.length; i++)
    099                    if (exam.getStudents().size() >= sLimits[i])
    100                        nrExamsLim[i]++;
    101            }
    102            List<CSVField> line = new ArrayList<CSVField>();
    103            line.add(new CSVField(period.getIndex() + 1));
    104            line.add(new CSVField(period.getDayStr()));
    105            line.add(new CSVField(period.getTimeStr()));
    106            line.add(new CSVField(period.getPenalty()));
    107            line.add(new CSVField(nrExams));
    108            line.add(new CSVField(nrStudents));
    109            for (int i = 0; i < sLimits.length; i++)
    110                line.add(new CSVField(nrExamsLim[i]));
    111            if (nrAvgPer > 0) {
    112                line.add(new CSVField(sDF.format(((double) totAvgPer) / nrAvgPer)));
    113                line.add(new CSVField(sDF.format(((double) totWgAvgPer) / nrAvgPer)));
    114            }
    115            csv.addLine(line);
    116        }
    117        return csv;
    118    }
    119}