001 package net.sf.cpsolver.coursett;
002
003 import java.io.File;
004
005 import net.sf.cpsolver.coursett.model.Lecture;
006 import net.sf.cpsolver.coursett.model.TimetableModel;
007 import net.sf.cpsolver.ifs.util.CSVFile;
008 import net.sf.cpsolver.ifs.util.DataProperties;
009 import net.sf.cpsolver.ifs.util.ToolBox;
010
011 /**
012 * Create domain chart of the given input problem as CSV file (3 dimensions:
013 * #rooms, #times, #variables with the given number of rooms/times)
014 *
015 * @version CourseTT 1.2 (University Course Timetabling)<br>
016 * Copyright (C) 2007 - 2010 Tomas Muller<br>
017 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
018 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
019 * <br>
020 * This library is free software; you can redistribute it and/or modify
021 * it under the terms of the GNU Lesser General Public License as
022 * published by the Free Software Foundation; either version 3 of the
023 * License, or (at your option) any later version. <br>
024 * <br>
025 * This library is distributed in the hope that it will be useful, but
026 * WITHOUT ANY WARRANTY; without even the implied warranty of
027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
028 * Lesser General Public License for more details. <br>
029 * <br>
030 * You should have received a copy of the GNU Lesser General Public
031 * License along with this library; if not see
032 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
033 */
034 public class DomainChart {
035 protected int iSizeX = 60, iSizeY = 100;
036 protected TimetableModel iModel;
037 protected double[][] iTable = null;
038 protected boolean iShowZero = false;
039 protected String iName = null;
040 protected String[] iHeader = null;
041 protected String[] iTitle = null;
042
043 public DomainChart(String name, TimetableModel model, int sizeX, int sizeY) {
044 iModel = model;
045 iName = name;
046 iSizeX = sizeX;
047 iSizeY = sizeY;
048 }
049
050 public DomainChart(File xmlFile, int sizeX, int sizeY) throws Exception {
051 this(xmlFile.getName().substring(0, xmlFile.getName().lastIndexOf('.')), new TimetableModel(
052 new DataProperties()), sizeX, sizeY);
053 TimetableXMLLoader loader = new TimetableXMLLoader(iModel);
054 loader.setInputFile(xmlFile);
055 loader.load();
056 }
057
058 protected void clearTable() {
059 iTable = new double[2 + iSizeX][2 + iSizeY];
060 for (int i = 0; i < iTable.length; i++)
061 for (int j = 0; j < iTable[i].length; j++)
062 iTable[i][j] = 0;
063 iHeader = new String[iSizeX + 2];
064 for (int i = 0; i <= iSizeX; i++)
065 iHeader[i] = String.valueOf(i);
066 iHeader[iSizeX + 1] = (iSizeX + 1) + "+";
067 iTitle = new String[iSizeY + 2];
068 for (int i = 0; i <= iSizeY; i++)
069 iTitle[i] = String.valueOf(i);
070 iTitle[iSizeY + 1] = (iSizeY + 1) + "+";
071 }
072
073 protected void add(int x, int y, double val) {
074 iTable[x <= iSizeX ? x : 1 + iSizeX][y <= iSizeY ? y : 1 + iSizeY] += val;
075 }
076
077 protected void computeTable() {
078 clearTable();
079 for (Lecture lecture : iModel.variables()) {
080 if (lecture.getNrRooms() > 1)
081 add(lecture.nrTimeLocations(), (int) Math.round(Math.pow(lecture.nrRoomLocations(), 1.0 / lecture
082 .getNrRooms())), 1);
083 else
084 add(lecture.nrTimeLocations(), lecture.nrRoomLocations(), 1);
085 }
086 }
087
088 public CSVFile createTable() {
089 computeTable();
090 CSVFile csv = new CSVFile();
091 CSVFile.CSVField[] header = new CSVFile.CSVField[2 + iSizeX + (iShowZero ? 1 : 0)];
092 header[0] = new CSVFile.CSVField(iName);
093 for (int i = (iShowZero ? 0 : 1); i <= iSizeX + 1; i++)
094 header[(iShowZero ? 1 : 0) + i] = new CSVFile.CSVField(iHeader[i]);
095 csv.setHeader(header);
096 for (int y = (iShowZero ? 0 : 1); y <= 1 + iSizeY; y++) {
097 CSVFile.CSVField[] line = new CSVFile.CSVField[2 + iSizeX + (iShowZero ? 1 : 0)];
098 line[0] = new CSVFile.CSVField(iTitle[y]);
099 if (y == 1 + iSizeY)
100 line[0] = new CSVFile.CSVField((1 + iSizeY) + "+");
101 for (int x = (iShowZero ? 0 : 1); x <= 1 + iSizeX; x++)
102 line[(iShowZero ? 1 : 0) + x] = new CSVFile.CSVField(iTable[x][y]);
103 csv.addLine(line);
104 }
105 return csv;
106 }
107
108 public static void main(String args[]) {
109 try {
110 ToolBox.configureLogging();
111 File input = new File(args[0]);
112 int sizeX = Integer.parseInt(args[1]);
113 int sizeY = Integer.parseInt(args[2]);
114 File output = null;
115 if (args.length > 3) {
116 output = new File(args[3]);
117 if (output.exists() && output.isDirectory())
118 output = new File(output, input.getName().substring(0, input.getName().lastIndexOf('.'))
119 + "_domain.csv");
120 } else {
121 output = new File(input.getParentFile(), input.getName().substring(0, input.getName().lastIndexOf('.'))
122 + "_domain.csv");
123 }
124 new DomainChart(input, sizeX, sizeY).createTable().save(output);
125 } catch (Exception e) {
126 e.printStackTrace();
127 }
128 }
129 }