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 * @author  Tomáš Müller
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 ExamPeriodUsage {
046    private ExamModel iModel = null;
047    /** Exam enrollment limits */
048    public static int[] sLimits = new int[] { 10, 50, 100, 200 };
049    private static DecimalFormat sDF = new DecimalFormat("0.00");
050
051    /**
052     * Constructor
053     * 
054     * @param model
055     *            examination timetabling model
056     */
057    public ExamPeriodUsage(ExamModel model) {
058        iModel = model;
059    }
060
061    /**
062     * generate report
063     * @param assignment current assignment
064     * @return resultant report
065     */
066    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
067        CSVFile csv = new CSVFile();
068        List<CSVField> header = new ArrayList<CSVField>();
069        header.add(new CSVField("Period"));
070        header.add(new CSVField("Date"));
071        header.add(new CSVField("Time"));
072        header.add(new CSVField("Weight"));
073        header.add(new CSVField("NrExams"));
074        header.add(new CSVField("Students"));
075        for (int i = 0; i < sLimits.length; i++) {
076            header.add(new CSVField("NrExams>=" + sLimits[i]));
077        }
078        header.add(new CSVField("AvgPeriod"));
079        header.add(new CSVField("WgAvgPeriod"));
080        csv.setHeader(header);
081        for (ExamPeriod period : iModel.getPeriods()) {
082            int nrExams = 0;
083            int nrStudents = 0;
084            int[] nrExamsLim = new int[sLimits.length];
085            int totAvgPer = 0, nrAvgPer = 0, totWgAvgPer = 0;
086            for (int i = 0; i < sLimits.length; i++)
087                nrExamsLim[i] = 0;
088            for (Exam exam : iModel.variables()) {
089                ExamPlacement placement = assignment.getValue(exam);
090                if (placement == null || !(placement.getPeriod().equals(period)))
091                    continue;
092                nrExams++;
093                nrStudents += exam.getStudents().size();
094                if (exam.getAveragePeriod() >= 0) {
095                    totAvgPer += exam.getAveragePeriod();
096                    nrAvgPer++;
097                    totWgAvgPer += exam.getAveragePeriod() * exam.getStudents().size();
098                }
099                for (int i = 0; i < sLimits.length; i++)
100                    if (exam.getStudents().size() >= sLimits[i])
101                        nrExamsLim[i]++;
102            }
103            List<CSVField> line = new ArrayList<CSVField>();
104            line.add(new CSVField(period.getIndex() + 1));
105            line.add(new CSVField(period.getDayStr()));
106            line.add(new CSVField(period.getTimeStr()));
107            line.add(new CSVField(period.getPenalty()));
108            line.add(new CSVField(nrExams));
109            line.add(new CSVField(nrStudents));
110            for (int i = 0; i < sLimits.length; i++)
111                line.add(new CSVField(nrExamsLim[i]));
112            if (nrAvgPer > 0) {
113                line.add(new CSVField(sDF.format(((double) totAvgPer) / nrAvgPer)));
114                line.add(new CSVField(sDF.format(((double) totWgAvgPer) / nrAvgPer)));
115            }
116            csv.addLine(line);
117        }
118        return csv;
119    }
120}