21 package org.matsim.analysis;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
33 import jakarta.inject.Inject;
34 import java.io.BufferedReader;
35 import java.io.BufferedWriter;
36 import java.io.IOException;
52 public LinkData(
final double[][] linksVolumes,
final double[][] linksTTimes) {
53 this.volumes = linksVolumes.clone();
54 this.ttimes = linksTTimes.clone();
65 private static final int MIN = 0;
66 private static final int MAX = 1;
67 private static final int SUM = 2;
86 this.volScaleFactor = vol_scale_factor;
109 for (
int hour = 0; hour < this.
nofHours; hour++) {
115 sumVolumes += volumes[hour];
119 if (this.count == 1) {
125 if (volumes[hour] < data.
volumes[MIN][hour]) data.
volumes[
MIN][hour] = volumes[hour];
126 if (volumes[hour] > data.
volumes[MAX][hour]) data.
volumes[
MAX][hour] = volumes[hour];
133 data.
ttimes[
SUM][hour] += volumes[hour] * ttime;
136 if (this.count == 1) {
149 this.linkData.
clear();
151 log.info(
" resetting `count' to zero. This info is here since we want to check when this" +
152 " is happening during normal simulation runs. kai, jan'11") ;
156 LinkData data =
new LinkData(
new double[NOF_STATS][this.nofHours + 1],
new double[NOF_STATS][this.nofHours]);
157 this.linkData.
put(link.getId(), data);
166 out.write(
"LINK\tORIG_ID\tFROM\tTO\tLENGTH\tFREESPEED\tCAPACITY");
167 for (
int i = 0; i < this.
nofHours; i++) {
168 out.write(
"\tHRS" + i +
"-" + (i+1) +
"min");
169 out.write(
"\tHRS" + i +
"-" + (i+1) +
"avg");
170 out.write(
"\tHRS" + i +
"-" + (i+1) +
"max");
172 out.write(
"\tHRS0-" + this.nofHours +
"min");
173 out.write(
"\tHRS0-" + this.nofHours +
"avg");
174 out.write(
"\tHRS0-" + this.nofHours +
"max");
175 for (
int i = 0; i < this.
nofHours; i++) {
176 out.write(
"\tTRAVELTIME" + i +
"-" + (i+1) +
"min");
177 out.write(
"\tTRAVELTIME" + i +
"-" + (i+1) +
"avg");
178 out.write(
"\tTRAVELTIME" + i +
"-" + (i+1) +
"max");
188 out.write(linkId.toString());
192 out.write(
"\t" + Double.toString(link.
getLength()));
194 out.write(
"\t" + Double.toString(link.
getCapacity()));
198 for (
int i = 0; i < this.
nofHours; i++) {
199 out.write(
"\t" + Double.toString(data.
volumes[MIN][i]));
201 out.write(
"\t" + Double.toString((data.
volumes[SUM][i]) /
this.count));
203 out.write(
"\t" + Double.toString(data.
volumes[MAX][i]));
208 out.write(
"\t" + Double.toString(data.
volumes[MIN][
this.nofHours]));
209 out.write(
"\t" + Double.toString((data.
volumes[SUM][
this.nofHours]) /
this.count));
210 out.write(
"\t" + Double.toString(data.
volumes[MAX][
this.nofHours]));
213 for (
int i = 0; i < this.
nofHours; i++) {
214 String ttimesMin = Double.toString(data.
ttimes[MIN][i]);
215 out.write(
"\t" + ttimesMin);
216 if (data.
volumes[SUM][i] == 0) {
221 out.write(
"\t" + Double.toString(ttsum));
223 out.write(
"\t" + ttimesMin);
228 out.write(
"\t" + ttimesMin);
230 out.write(
"\t" + Double.toString(ttsum / data.
volumes[SUM][i]));
233 out.write(
"\t" + Double.toString(data.
ttimes[MAX][i]));
238 }
catch (IOException e) {
239 log.error(
"could not write linkstats", e);
250 String header = reader.readLine();
251 if (header == null) {
259 String line = reader.readLine();
260 while (line != null) {
262 if (parts.length == 154) {
266 System.err.println(
"CalcLinkStats.readFile(); unknown link: " + linkId.toString());
269 for (
int i = 0; i < this.
nofHours; i++) {
270 data.
volumes[
MIN][i] = Double.parseDouble(parts[7 + i*3]);
272 data.
volumes[
SUM][i] = Double.parseDouble(parts[8 + i*3]);
274 data.
volumes[
MAX][i] = Double.parseDouble(parts[9 + i*3]);
276 baseTTimes = 7 + (this.nofHours+1)*3;
277 data.
ttimes[
MIN][i] = Double.parseDouble(parts[baseTTimes + i*3]);
278 if (data.
volumes[SUM][i] == 0) {
279 data.
ttimes[
SUM][i] = Double.parseDouble(parts[baseTTimes + i*3 + 1]);
281 data.
ttimes[
SUM][i] = Double.parseDouble(parts[baseTTimes + i*3 + 1]) * data.
volumes[
SUM][i];
283 data.
ttimes[
MAX][i] = Double.parseDouble(parts[baseTTimes + i*3 + 2]);
293 else if (parts.length == 153) {
294 String linkId = parts[0];
297 System.err.println(
"CalcLinkStats.readFile(); unknown link: " + linkId);
300 for (
int i = 0; i < this.
nofHours; i++) {
301 data.
volumes[
MIN][i] = Double.parseDouble(parts[6 + i*3]);
303 data.
volumes[
SUM][i] = Integer.parseInt(parts[7 + i*3]);
305 data.
volumes[
MAX][i] = Double.parseDouble(parts[8 + i*3]);
307 baseTTimes = 6 + (this.nofHours+1)*3;
308 data.
ttimes[
MIN][i] = Double.parseDouble(parts[baseTTimes + i*3]);
309 if (data.
volumes[SUM][i] == 0) {
310 data.
ttimes[
SUM][i] = Double.parseDouble(parts[baseTTimes + i*3 + 1]);
312 data.
ttimes[
SUM][i] = Double.parseDouble(parts[baseTTimes + i*3 + 1]) * data.
volumes[
SUM][i];
314 data.
ttimes[
MAX][i] = Double.parseDouble(parts[baseTTimes + i*3 + 2]);
325 System.err.println(
"CalcLinkStats.readFile(); line cannot be parsed: " + line +
" number of colums is: " + parts.length);
328 line = reader.readLine();
331 }
catch (IOException e) {
332 log.error(
"could not read linkstats.", e);
343 return new double[0];
345 if (this.count == 0) {
346 return new double[0];
349 for (
int i = 0; i < this.
nofHours; i++) {
350 volumes[i] = (data.
volumes[
SUM][i]) / (this.count);
366 return new double[0];
368 if (this.count == 0) {
369 return new double[0];
371 double[] ttimesMin =
new double[this.
nofHours];
372 double[] ttimesSum =
new double[this.
nofHours];
375 double[] avgTTimes =
new double[this.
nofHours];
377 for (
int i = 0; i < this.
nofHours; i++) {
378 volumes[i] = (data.
volumes[
SUM][i]) / (this.count);
379 ttimesMin[i] = (data.
ttimes[
MIN][i]) / (this.count);
380 ttimesSum[i] = (data.
ttimes[
SUM][i]) / (this.count);
382 if (volumes[i] == 0.) {
383 avgTTimes[i] = ttimesMin[i];
385 avgTTimes[i] = ttimesSum[i] / volumes[i];
void writeFile(final String filename)
double [] getVolumesPerHourForLink(final Id< Link > linkId)
void readFile(final String filename)
CalcLinkStats(final Network network)
static BufferedReader getBufferedReader(URL url, Charset charset)
double [] getAvgLinkVolumes(final Id< Link > linkId)
final IdMap< Link, LinkData > linkData
static String [] explode(final String str, final char delimiter, final int limit)
LinkData(final double[][] linksVolumes, final double[][] linksTTimes)
double getLinkTravelTime(Link link, double time, Person person, Vehicle vehicle)
static final int NOF_STATS
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
static< T > Id< T > create(final long key, final Class< T > type)
CalcLinkStats(final Network network, double vol_scale_factor)
void addData(final VolumesAnalyzer analyzer, final TravelTime ttimes)
Map< Id< Link >, ? extends Link > getLinks()
final double [][] volumes
V put(Id< T > key, V value)
double [] getAvgTravelTimes(final Id< Link > linkId)