001package net.sf.cpsolver.coursett;
002
003import java.io.File;
004import java.util.Comparator;
005import java.util.Iterator;
006import java.util.TreeSet;
007
008import net.sf.cpsolver.coursett.model.Lecture;
009import net.sf.cpsolver.coursett.model.TimetableModel;
010import net.sf.cpsolver.ifs.util.ToolBox;
011
012/**
013 * Create joint enrollment chart of the given input problem as CSV file (3
014 * dimensions: 1st variable, 2nd variable, number of students in common)
015 * 
016 * @version CourseTT 1.2 (University Course Timetabling)<br>
017 *          Copyright (C) 2007 - 2010 Tomáš Müller<br>
018 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
019 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
020 * <br>
021 *          This library is free software; you can redistribute it and/or modify
022 *          it under the terms of the GNU Lesser General Public License as
023 *          published by the Free Software Foundation; either version 3 of the
024 *          License, or (at your option) any later version. <br>
025 * <br>
026 *          This library is distributed in the hope that it will be useful, but
027 *          WITHOUT ANY WARRANTY; without even the implied warranty of
028 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
029 *          Lesser General Public License for more details. <br>
030 * <br>
031 *          You should have received a copy of the GNU Lesser General Public
032 *          License along with this library; if not see
033 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
034 */
035public class JenrlChart extends DomainChart {
036    protected int iMax = 100;
037
038    public JenrlChart(String name, TimetableModel model, int max) {
039        super(name, model, Math.min(max, model.variables().size()), Math.min(max, model.variables().size()));
040        iMax = max;
041    }
042
043    public JenrlChart(File xmlFile, int max) throws Exception {
044        super(xmlFile, 0, 0);
045        iMax = max;
046        iSizeX = Math.min(iMax, iModel.variables().size());
047        iSizeY = Math.min(iMax, iModel.variables().size());
048    }
049
050    @Override
051    protected void computeTable() {
052        clearTable();
053        TreeSet<Lecture> vars = new TreeSet<Lecture>(new Comparator<Lecture>() {
054            @Override
055            public int compare(Lecture l1, Lecture l2) {
056                int cmp = -Double.compare(l1.students().size(), l2.students().size());
057                if (cmp != 0)
058                    return cmp;
059                cmp = -Double.compare(l1.classLimit(), l2.classLimit());
060                if (cmp != 0)
061                    return cmp;
062                return Double.compare(l1.getId(), l2.getId());
063            }
064        });
065        vars.addAll(iModel.variables());
066        int x = 1;
067        for (Iterator<Lecture> i = vars.iterator(); i.hasNext(); x++) {
068            Lecture l1 = i.next();
069            if (x > iMax)
070                continue;
071            iHeader[x] = String.valueOf(l1.students().size());
072            int y = 1;
073            for (Iterator<Lecture> j = vars.iterator(); j.hasNext(); y++) {
074                Lecture l2 = j.next();
075                if (y > iMax)
076                    continue;
077                iTitle[y] = l2.getName();
078                if (x >= y)
079                    continue;
080                add(x, y, l1.sameStudents(l2).size());
081            }
082        }
083    }
084
085    public static void main(String args[]) {
086        try {
087            ToolBox.configureLogging();
088            File input = new File(args[0]);
089            int max = Integer.parseInt(args[1]);
090            File output = null;
091            if (args.length > 2) {
092                output = new File(args[2]);
093                if (output.exists() && output.isDirectory())
094                    output = new File(output, input.getName().substring(0, input.getName().lastIndexOf('.'))
095                            + "_jenrl.csv");
096            } else {
097                output = new File(input.getParentFile(), input.getName().substring(0, input.getName().lastIndexOf('.'))
098                        + "_jenrl.csv");
099            }
100            new JenrlChart(input, max).createTable().save(output);
101        } catch (Exception e) {
102            e.printStackTrace();
103        }
104    }
105
106}