MATSIM
CountSimComparisonTableWriter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * CountSimComparisonTableWriter.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007, 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.counts.algorithms;
22 
23 import java.io.BufferedWriter;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.text.DecimalFormat;
27 import java.text.NumberFormat;
28 import java.util.List;
29 import java.util.Locale;
30 
31 import org.apache.logging.log4j.LogManager;
32 import org.apache.logging.log4j.Logger;
34 
45 public class CountSimComparisonTableWriter extends CountSimComparisonWriter {
46 
50  private static final String SEPARATOR = "\t";
51 
55  private static final String NEWLINE = "\n";
56 
60  private static final String[] COLUMNHEADERS = { "Link Id", "Count Station Id", "Hour",
61  "MATSIM volumes", "Count volumes", "Relative Error", "Normalized Relative Error", "GEH"};
62 
66  private final NumberFormat numberFormat;
67 
68  private static final Logger log = LogManager.getLogger(CountSimComparisonTableWriter.class);
69 
76  final List<CountSimComparison> countSimComparisons, final Locale l) {
77  super(countSimComparisons);
78  if (l == null) {
79  this.numberFormat = new DecimalFormat("#.############");
80  }
81  else {
82  this.numberFormat = NumberFormat.getInstance(l);
83  }
84  this.numberFormat.setGroupingUsed(false);
85  }
86 
92  @Override
93  public void writeFile(final String filename) {
94  log.info("Writing CountsSimComparison to " + filename);
95  try (BufferedWriter out = new BufferedWriter(new FileWriter(filename))) {
96 
97  out.write(COLUMNHEADERS[0]);
98  for (int i = 1; i < COLUMNHEADERS.length; i++) {
99  out.write(SEPARATOR);
100  out.write(COLUMNHEADERS[i]);
101  }
102  out.write(NEWLINE);
103 
104  for (CountSimComparison csc : this.countComparisonFilter.getCountsForHour(null)) {
105 
106  out.write(csc.getId().toString());
107  out.write(SEPARATOR);
108  out.write(csc.getCsId());
109  out.write(SEPARATOR);
110  out.write(Integer.toString(csc.getHour()));
111  out.write(SEPARATOR);
112  out.write(this.numberFormat.format(csc.getSimulationValue()));
113  out.write(SEPARATOR);
114  out.write(this.numberFormat.format(csc.getCountValue()));
115  out.write(SEPARATOR);
116  out.write(this.numberFormat.format(csc.calculateRelativeError()));
117  out.write(SEPARATOR);
118  out.write(this.numberFormat.format(csc.calculateNormalizedRelativeError()));
119  out.write(SEPARATOR);
120  out.write(this.numberFormat.format(csc.calculateGEHValue()));
121  out.write(NEWLINE);
122  }
123  } catch (IOException e) {
124  e.printStackTrace();
125  }
126 
127  // writing the 'average weekday traffic volume' table:
128  this.writeAWTVTable(filename);
129  }
130 
131  private void writeAWTVTable(final String file) {
132  // output file always ends with ".txt"
133  String filename = file.substring(0, file.length() - 4) + "AWTV.txt";
134 
135  log.info("Writing 'average weekday traffic volume' to " + filename);
136 
138  this.countComparisonFilter.getCountsForHour(null));
139 
140  try (BufferedWriter out = new BufferedWriter(new FileWriter(filename))) {
141  out.write("Link Id");
142  out.write(SEPARATOR);
143  out.write("Count Station Id");
144  out.write(SEPARATOR);
145  out.write("MATSIM volumes");
146  out.write(SEPARATOR);
147  out.write("Count volumes");
148  out.write(SEPARATOR);
149  out.write("Normalized Relative Error");
150  out.write(NEWLINE);
151 
152  for (CountSimComparison csc : this.countComparisonFilter.getCountsForHour(null)) {
153 
154  final double simValue = linkFilter.getAggregatedSimValue(csc.getId());
155  final double countValue = linkFilter.getAggregatedCountValue(csc.getId());
156  final double nomRelError;
157 
158  final double max = Math.max(simValue, countValue);
159  if (max == 0.0) nomRelError = 0;
160  else nomRelError = Math.abs(simValue - countValue) / max;
161 
162  out.write(csc.getId().toString());
163  out.write(SEPARATOR);
164  out.write(csc.getCsId());
165  out.write(SEPARATOR);
166  out.write(this.numberFormat.format(simValue));
167  out.write(SEPARATOR);
168  out.write(this.numberFormat.format(countValue));
169  out.write(SEPARATOR);
170  out.write(this.numberFormat.format(nomRelError));
171  out.write(NEWLINE);
172  }
173  } catch (IOException e) {
174  e.printStackTrace();
175  }
176  }
177 }
CountSimComparisonTableWriter(final List< CountSimComparison > countSimComparisons, final Locale l)