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