diff --git a/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/PControlerTestIT.java b/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/PControlerTestIT.java index 57506248366..3429d672745 100644 --- a/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/PControlerTestIT.java +++ b/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/PControlerTestIT.java @@ -94,9 +94,9 @@ public final void testPControler() { filesToCheckFor.add(outputPath + scenarioName + ".0.actsFromParatransitUsers.txt"); filesToCheckFor.add(outputPath + scenarioName + ".pOperatorLogger.txt"); filesToCheckFor.add(outputPath + scenarioName + ".pStats.txt"); - filesToCheckFor.add(outputPath + scenarioName + ".scorestats.txt"); - filesToCheckFor.add(outputPath + scenarioName + ".stopwatch.txt"); - filesToCheckFor.add(outputPath + scenarioName + ".traveldistancestats.txt"); + filesToCheckFor.add(outputPath + scenarioName + ".scorestats.csv"); + filesToCheckFor.add(outputPath + scenarioName + ".stopwatch.csv"); + filesToCheckFor.add(outputPath + scenarioName + ".traveldistancestats.csv"); filesToCheckFor.add(outputPath + scenarioName + ".pStat_light.gexf.gz"); filesToCheckFor.add(outputPath + scenarioName + ".pStat.gexf.gz"); diff --git a/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/SubsidyContextTestIT.java b/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/SubsidyContextTestIT.java index 1f4676a8458..f9881ed715e 100644 --- a/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/SubsidyContextTestIT.java +++ b/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/SubsidyContextTestIT.java @@ -83,9 +83,9 @@ public final void testDefaultPControler() { filesToCheckFor.add(utils.getOutputDirectory() + "0.actsFromParatransitUsers.txt"); filesToCheckFor.add(utils.getOutputDirectory() + "pOperatorLogger.txt"); filesToCheckFor.add(utils.getOutputDirectory() + "pStats.txt"); - filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.txt"); - filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.txt"); - filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.txt"); + filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.csv"); + filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.csv"); + filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.csv"); filesToCheckFor.add(utils.getOutputDirectory() + "pStat_light.gexf.gz"); filesToCheckFor.add(utils.getOutputDirectory() + "pStat.gexf.gz"); @@ -145,9 +145,9 @@ public final void testSubsidyPControler() { filesToCheckFor.add(utils.getOutputDirectory() + "0.actsFromParatransitUsers.txt"); filesToCheckFor.add(utils.getOutputDirectory() + "pOperatorLogger.txt"); filesToCheckFor.add(utils.getOutputDirectory() + "pStats.txt"); - filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.txt"); - filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.txt"); - filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.txt"); + filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.csv"); + filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.csv"); + filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.csv"); filesToCheckFor.add(utils.getOutputDirectory() + "pStat_light.gexf.gz"); filesToCheckFor.add(utils.getOutputDirectory() + "pStat.gexf.gz"); diff --git a/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/SubsidyTestIT.java b/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/SubsidyTestIT.java index b2c7576ae7e..98107c2c0d0 100644 --- a/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/SubsidyTestIT.java +++ b/contribs/minibus/src/test/java/org/matsim/contrib/minibus/integration/SubsidyTestIT.java @@ -84,9 +84,9 @@ public final void testSubsidyPControler() { filesToCheckFor.add(utils.getOutputDirectory() + "0.actsFromParatransitUsers.txt"); filesToCheckFor.add(utils.getOutputDirectory() + "pOperatorLogger.txt"); filesToCheckFor.add(utils.getOutputDirectory() + "pStats.txt"); - filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.txt"); - filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.txt"); - filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.txt"); + filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.csv"); + filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.csv"); + filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.csv"); filesToCheckFor.add(utils.getOutputDirectory() + "pStat_light.gexf.gz"); filesToCheckFor.add(utils.getOutputDirectory() + "pStat.gexf.gz"); diff --git a/contribs/simwrapper/src/main/java/org/matsim/simwrapper/dashboard/OverviewDashboard.java b/contribs/simwrapper/src/main/java/org/matsim/simwrapper/dashboard/OverviewDashboard.java index b24ae180cc9..68b02d22fde 100644 --- a/contribs/simwrapper/src/main/java/org/matsim/simwrapper/dashboard/OverviewDashboard.java +++ b/contribs/simwrapper/src/main/java/org/matsim/simwrapper/dashboard/OverviewDashboard.java @@ -65,8 +65,8 @@ public void configure(Header header, Layout layout) { }).el(PieChart.class, (viz, data) -> { viz.title = "Mode Share"; viz.description = "at final Iteration"; - viz.dataset = data.output("*.modestats.txt"); - viz.ignoreColumns = List.of("Iteration"); + viz.dataset = data.output("*.modestats.csv"); + viz.ignoreColumns = List.of("iteration"); viz.useLastRow = true; }); @@ -74,10 +74,10 @@ public void configure(Header header, Layout layout) { layout.row("second").el(Line.class, (viz, data) -> { viz.title = "Score"; - viz.dataset = data.output("*.scorestats.txt"); + viz.dataset = data.output("*.scorestats.csv"); viz.description = "per Iteration"; viz.x = "ITERATION"; - viz.columns = List.of("avg. EXECUTED", "avg. WORST", "avg. BEST"); + viz.columns = List.of("avg_executed", "avg_worst", "avg_best"); viz.xAxisName = "Iteration"; viz.yAxisName = "Score"; @@ -87,7 +87,7 @@ public void configure(Header header, Layout layout) { .el(Area.class, (viz, data) -> { viz.title = "Mode Share Progression"; viz.description = "per Iteration"; - viz.dataset = data.output("*.modestats.txt"); + viz.dataset = data.output("*.modestats.csv"); viz.x = "Iteration"; viz.xAxisName = "Iteration"; viz.yAxisName = "Share"; diff --git a/matsim/src/main/java/org/matsim/analysis/IterationStopWatch.java b/matsim/src/main/java/org/matsim/analysis/IterationStopWatch.java index 59ef84fc779..54a5b57a05e 100644 --- a/matsim/src/main/java/org/matsim/analysis/IterationStopWatch.java +++ b/matsim/src/main/java/org/matsim/analysis/IterationStopWatch.java @@ -78,15 +78,15 @@ public final class IterationStopWatch { /** data structures to identify nested operations */ private Stack currentMeasuredOperations; private Map> currentIterationChildren; - private Map>> children; - + private final Map>> children; + /** Creates a new IterationStopWatch. */ public IterationStopWatch() { - this.iterations = new LinkedHashMap>(); - this.identifiers = new LinkedList(); - this.operations = new LinkedList(); + this.iterations = new LinkedHashMap<>(); + this.identifiers = new LinkedList<>(); + this.operations = new LinkedList<>(); this.currentIterationValues = null; - this.children = new LinkedHashMap>>(); + this.children = new LinkedHashMap<>(); } /** @@ -109,18 +109,16 @@ public void reset() { * Sets the current iteration, so that the times measured using {@link #beginOperation(String)}, * {@link #endOperation(String)} and {@link #timestamp(String)} are assigned to the correct iteration for * the analysis. - * - * @param iteration */ public void beginIteration(final int iteration) { - this.iteration = Integer.valueOf(iteration); + this.iteration = iteration; if (this.iterations.get(this.iteration) == null) { - this.currentIterationValues = new HashMap(); + this.currentIterationValues = new HashMap<>(); this.iterations.put(this.iteration, this.currentIterationValues); this.nextIdentifierPosition = 0; this.nextOperationPosition = 0; - this.currentMeasuredOperations = new Stack(); - this.currentIterationChildren = new HashMap>(); + this.currentMeasuredOperations = new Stack<>(); + this.currentIterationChildren = new HashMap<>(); this.children.put(this.iteration, this.currentIterationChildren); } this.beginOperation(OPERATION_ITERATION); @@ -139,16 +137,16 @@ public void beginOperation(final String identifier) { String ident = "BEGIN " + identifier; ensureIdentifier(ident); - this.currentIterationValues.put(ident, Long.valueOf(System.currentTimeMillis())); - - this.currentIterationChildren.put(identifier, new ArrayList()); + this.currentIterationValues.put(ident, System.currentTimeMillis()); + + this.currentIterationChildren.put(identifier, new ArrayList<>()); // check whether this operation has a parent operation - if (this.currentMeasuredOperations.size() > 0) { + if (!this.currentMeasuredOperations.isEmpty()) { String parent = this.currentMeasuredOperations.peek(); this.currentIterationChildren.get(parent).add(identifier); } - + // add ident to stack this.currentMeasuredOperations.push(identifier); } @@ -163,9 +161,9 @@ public void endOperation(final String identifier) { String ident = "END " + identifier; ensureIdentifier(ident); ensureOperation(identifier); - this.currentIterationValues.put(ident, Long.valueOf(System.currentTimeMillis())); - - + this.currentIterationValues.put(ident, System.currentTimeMillis()); + + this.currentMeasuredOperations.pop(); } @@ -180,31 +178,30 @@ public void endIteration() { */ public void timestamp(final String identifier) { ensureIdentifier(identifier); - this.currentIterationValues.put(identifier, Long.valueOf(System.currentTimeMillis())); + this.currentIterationValues.put(identifier, System.currentTimeMillis()); } /** - * Writes the gathered data tab-separated into a text file. + * Writes the gathered data into a file. * * @param filename The name of a file where to write the gathered data. + * @param delimiter The delimiter to be used as field separator. */ - public void writeTextFile(final String filename) { - - try { - BufferedWriter writer = IOUtils.getBufferedWriter(filename + ".txt"); + public void writeSeparatedFile(final String filename, final String delimiter) { + try (BufferedWriter writer = IOUtils.getBufferedWriter(filename)) { // print header - writer.write("Iteration"); + writer.write("iteration"); for (String identifier : this.identifiers) { - writer.write('\t'); + writer.write(delimiter); writer.write(identifier); } - writer.write('\t'); + writer.write(delimiter); for (String identifier : this.operations) { - writer.write('\t'); + writer.write(delimiter); writer.write(identifier); } - writer.write("\n"); + writer.write('\n'); // print data for (Map.Entry> entry : this.iterations.entrySet()) { @@ -215,18 +212,18 @@ public void writeTextFile(final String filename) { // identifiers for (String identifier : this.identifiers) { Long time = data.get(identifier); - writer.write('\t'); + writer.write(delimiter); writer.write(formatMilliTime(time)); } // blank separator - writer.write('\t'); + writer.write(delimiter); // durations of operations for (String identifier: this.operations) { Long startTime = data.get("BEGIN " + identifier); Long endTime = data.get("END " + identifier); - writer.write('\t'); + writer.write(delimiter); if (startTime != null && endTime != null) { - double diff = (endTime.longValue() - startTime.longValue()) / 1000.0; + double diff = (endTime - startTime) / 1000.0; writer.write(Time.writeTime(diff)); } } @@ -235,7 +232,6 @@ public void writeTextFile(final String filename) { writer.write("\n"); } writer.flush(); - writer.close(); } catch (IOException e) { throw new RuntimeException(e); } @@ -249,13 +245,13 @@ public void writeTextFile(final String filename) { public void writeGraphFile(String filename) { int iterations = this.iterations.entrySet().size(); - Map arrayMap = new HashMap(); + Map arrayMap = new HashMap<>(); for (String identifier : this.operations) arrayMap.put(identifier, new double[iterations]); int iter = 0; for(Entry> entry : this.iterations.entrySet()) { Map data = entry.getValue(); - + // children map of current iteration Map> childrenMap = this.children.get(entry.getKey()); @@ -264,18 +260,18 @@ public void writeGraphFile(String filename) { Long startTime = data.get("BEGIN " + identifier); Long endTime = data.get("END " + identifier); if (startTime != null && endTime != null) { - double diff = (endTime.longValue() - startTime.longValue()); + double diff = (endTime - startTime); /* * If the operation has children, subtract their durations since they are - * also included in the plot. Otherwise their duration would be counted twice. + * also included in the plot. Otherwise, their duration would be counted twice. */ for (String child : childrenMap.get(identifier)) { Long childStartTime = data.get("BEGIN " + child); Long childEndTime = data.get("END " + child); - diff -= (childEndTime.longValue() - childStartTime.longValue()); + diff -= (childEndTime - childStartTime); } - + arrayMap.get(identifier)[iter] = diff / 1000.0; } else arrayMap.get(identifier)[iter] = 0.0; } @@ -304,17 +300,12 @@ public void writeGraphFile(String filename) { chart.getChart().getCategoryPlot().getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90); double[] iterationData = null; -// double[] sumData = new double[iterations]; for (String operation : this.operations) { double[] data = arrayMap.get(operation); if (operation.equals(OPERATION_ITERATION)) { iterationData = data; - continue; } else { chart.addSeries(operation, data); -// for (int i = 0; i < iterations; i++) { -// sumData[i] += data[i]; -// } } } if (iterationData != null) { @@ -330,8 +321,6 @@ public void writeGraphFile(String filename) { * Make sure the given identifier exists in our collection. If it is missing, insert it at the correct * place. "Correct" means that it tries to insert this identifier right after the last-requested identifier. * This should help to write the gathered data out in a natural way. - * - * @param identifier */ private void ensureIdentifier(final String identifier) { int pos = this.identifiers.indexOf(identifier); @@ -345,8 +334,6 @@ private void ensureIdentifier(final String identifier) { /** * Does the same as {@link #ensureIdentifier(String)}, but for operation-identifiers. - * - * @param identifier */ private void ensureOperation(final String identifier) { int pos = this.operations.indexOf(identifier); @@ -359,7 +346,7 @@ private void ensureOperation(final String identifier) { } /** - * Formats the time given in milliseconds (e.g. returned by {@link java.lang.System#currentTimeMillis()} + * Formats the time given in milliseconds (e.g. returned by {@link java.lang.System#currentTimeMillis()}) * nicely for output. * * @param millis A time value in milliseconds @@ -369,7 +356,7 @@ private String formatMilliTime(final Long millis) { if (millis == null) { return ""; } - return this.formatter.format(new Date(millis.longValue())); + return this.formatter.format(new Date(millis)); } } diff --git a/matsim/src/main/java/org/matsim/analysis/ModeStatsControlerListener.java b/matsim/src/main/java/org/matsim/analysis/ModeStatsControlerListener.java index cc711abb7a5..310c70fddc0 100644 --- a/matsim/src/main/java/org/matsim/analysis/ModeStatsControlerListener.java +++ b/matsim/src/main/java/org/matsim/analysis/ModeStatsControlerListener.java @@ -40,7 +40,7 @@ import org.matsim.api.core.v01.population.Plan; import org.matsim.api.core.v01.population.Population; import org.matsim.core.config.groups.ControllerConfigGroup; -import org.matsim.core.config.groups.ScoringConfigGroup; +import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.controler.OutputDirectoryHierarchy; import org.matsim.core.controler.events.IterationEndsEvent; import org.matsim.core.controler.events.StartupEvent; @@ -66,19 +66,20 @@ */ public final class ModeStatsControlerListener implements StartupListener, IterationEndsListener { - public static final String FILENAME_MODESTATS = "modestats"; + private final static String FILENAME_MODESTATS = "modestats"; - final private Population population; + private final Population population; - final private String modeFileName ; + private final String modeFileName; + private final String delimiter; private final boolean createPNG; private final ControllerConfigGroup controllerConfigGroup; - Map> modeHistories = new HashMap<>() ; + Map> modeHistories = new HashMap<>(); private int minIteration = 0; private MainModeIdentifier mainModeIdentifier; - private Map modeCnt = new TreeMap<>() ; + private Map modeCnt = new TreeMap<>(); private int firstIteration = -1; // Keep all modes encountered so far in a sorted set to ensure output is written for modes sorted by mode. @@ -88,17 +89,18 @@ public final class ModeStatsControlerListener implements StartupListener, Iterat @Inject ModeStatsControlerListener(ControllerConfigGroup controllerConfigGroup, Population population1, OutputDirectoryHierarchy controlerIO, - ScoringConfigGroup scoreConfig, AnalysisMainModeIdentifier mainModeIdentifier) { + GlobalConfigGroup globalConfigGroup, AnalysisMainModeIdentifier mainModeIdentifier) { this.controllerConfigGroup = controllerConfigGroup; this.population = population1; - this.modeFileName = controlerIO.getOutputFilename( FILENAME_MODESTATS ) ; + this.modeFileName = controlerIO.getOutputFilename(FILENAME_MODESTATS); + this.delimiter = globalConfigGroup.getDefaultDelimiter(); this.createPNG = controllerConfigGroup.isCreateGraphs(); this.mainModeIdentifier = mainModeIdentifier; } @Override public void notifyStartup(final StartupEvent event) { - this.minIteration = controllerConfigGroup.getFirstIteration(); + this.minIteration = this.controllerConfigGroup.getFirstIteration(); } @Override @@ -111,18 +113,18 @@ private void collectModeShareInfo(final IterationEndsEvent event) { firstIteration = event.getIteration(); } for (Person person : this.population.getPersons().values()) { - Plan plan = person.getSelectedPlan() ; - List trips = TripStructureUtils.getTrips(plan) ; + Plan plan = person.getSelectedPlan(); + List trips = TripStructureUtils.getTrips(plan); for ( Trip trip : trips ) { - String mode = this.mainModeIdentifier.identifyMainMode( trip.getTripElements() ) ; + String mode = this.mainModeIdentifier.identifyMainMode(trip.getTripElements()); // yy as stated elsewhere, the "computer science" mode identification may not be the same as the "transport planning" // mode identification. Maybe revise. kai, nov'16 Double cnt = this.modeCnt.get( mode ); - if ( cnt==null ) { - cnt = 0. ; + if (cnt == null) { + cnt = 0.; } - this.modeCnt.put( mode, cnt + 1 ) ; + this.modeCnt.put( mode, cnt + 1 ); } } @@ -155,22 +157,21 @@ private void collectModeShareInfo(final IterationEndsEvent event) { modeHistory.put( event.getIteration(), share ) ; } - BufferedWriter modeOut = IOUtils.getBufferedWriter(this.modeFileName + ".txt"); - try { - modeOut.write("Iteration"); + try (BufferedWriter modeOut = IOUtils.getBufferedWriter(this.modeFileName + ".csv")) { + modeOut.write("iteration"); for ( String mode : modes ) { - modeOut.write("\t" + mode); + modeOut.write(this.delimiter); + modeOut.write(mode); } modeOut.write("\n"); for (int iter = firstIteration; iter <= event.getIteration(); iter++) { modeOut.write( String.valueOf(iter) ) ; for ( String mode : modes ) { - modeOut.write( "\t" + modeHistories.get(mode).get(iter)) ; + modeOut.write(this.delimiter + modeHistories.get(mode).get(iter)); } modeOut.write( "\n" ) ; } modeOut.flush(); - modeOut.close(); } catch (IOException e) { e.printStackTrace(); throw new UncheckedIOException(e); diff --git a/matsim/src/main/java/org/matsim/analysis/PHbyModeCalculator.java b/matsim/src/main/java/org/matsim/analysis/PHbyModeCalculator.java index 23973f7b645..b4663b50388 100644 --- a/matsim/src/main/java/org/matsim/analysis/PHbyModeCalculator.java +++ b/matsim/src/main/java/org/matsim/analysis/PHbyModeCalculator.java @@ -39,6 +39,7 @@ import org.matsim.api.core.v01.population.Person; import org.matsim.api.core.v01.population.Plan; import org.matsim.core.config.groups.ControllerConfigGroup; +import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.controler.OutputDirectoryHierarchy; import org.matsim.core.router.StageActivityTypeIdentifier; import org.matsim.core.scoring.EventsToLegs; @@ -52,8 +53,8 @@ public class PHbyModeCalculator { private final Map> phtPerIteration = new TreeMap<>(); private final boolean writePng; - private final OutputDirectoryHierarchy controlerIO; - private final static char DEL = '\t'; + private final OutputDirectoryHierarchy controllerIO; + private final String delimiter; private final static String FILENAME = "ph_modestats"; private static final String TRAVEL_TIME_SUFFIX = "_travel"; @@ -61,9 +62,10 @@ public class PHbyModeCalculator { private static final String STAGE_ACTIVITY = "stageActivity"; @Inject - PHbyModeCalculator(ControllerConfigGroup controllerConfigGroup, OutputDirectoryHierarchy controlerIO) { - writePng = controllerConfigGroup.isCreateGraphs(); - this.controlerIO = controlerIO; + PHbyModeCalculator(ControllerConfigGroup controllerConfigGroup, OutputDirectoryHierarchy controllerIO, GlobalConfigGroup globalConfig) { + this.writePng = controllerConfigGroup.isCreateGraphs(); + this.controllerIO = controllerIO; + this.delimiter = globalConfig.getDefaultDelimiter(); } void addIteration(int iteration, IdMap map) { @@ -122,7 +124,7 @@ private void writePHTText() { .flatMap(i->i.keySet().stream()) .collect(Collectors.toSet())); - try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(Paths.get(controlerIO.getOutputFilename( FILENAME+ ".txt"))), CSVFormat.DEFAULT.withDelimiter(DEL))) { + try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(Paths.get(controllerIO.getOutputFilename( FILENAME+ ".csv"))), CSVFormat.DEFAULT.withDelimiter(this.delimiter.charAt(0)))) { csvPrinter.print("Iteration"); for (String mode: allModes) { csvPrinter.print(mode + TRAVEL_TIME_SUFFIX); @@ -166,7 +168,7 @@ private void writePHTText() { chart.addSeries(mode + WAIT_TIME_SUFFIX, valueWaitTime); } chart.addMatsimLogo(); - chart.saveAsPng(controlerIO.getOutputFilename(FILENAME+ ".png"), 1024, 768); + chart.saveAsPng(controllerIO.getOutputFilename(FILENAME+ ".png"), 1024, 768); } diff --git a/matsim/src/main/java/org/matsim/analysis/PKMbyModeCalculator.java b/matsim/src/main/java/org/matsim/analysis/PKMbyModeCalculator.java index 47016857fdb..1b05fccdb2f 100644 --- a/matsim/src/main/java/org/matsim/analysis/PKMbyModeCalculator.java +++ b/matsim/src/main/java/org/matsim/analysis/PKMbyModeCalculator.java @@ -38,6 +38,7 @@ import org.matsim.api.core.v01.population.Person; import org.matsim.api.core.v01.population.Plan; import org.matsim.core.config.groups.ControllerConfigGroup; +import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.controler.OutputDirectoryHierarchy; import org.matsim.core.utils.charts.StackedBarChart; @@ -49,15 +50,15 @@ public class PKMbyModeCalculator { private final Map> pmtPerIteration = new TreeMap<>(); private final boolean writePng; - private final OutputDirectoryHierarchy controlerIO; - private final static char DEL = '\t'; + private final OutputDirectoryHierarchy controllerIO; + private final String delimiter; private final static String FILENAME = "pkm_modestats"; - @Inject - PKMbyModeCalculator(ControllerConfigGroup controllerConfigGroup, OutputDirectoryHierarchy controlerIO) { + PKMbyModeCalculator(ControllerConfigGroup controllerConfigGroup, OutputDirectoryHierarchy controllerIO, GlobalConfigGroup globalConfig) { writePng = controllerConfigGroup.isCreateGraphs(); - this.controlerIO = controlerIO; + this.controllerIO = controllerIO; + this.delimiter = globalConfig.getDefaultDelimiter(); } void addIteration(int iteration, IdMap map) { @@ -88,7 +89,7 @@ private void writeVKTText() { .flatMap(i->i.keySet().stream()) .collect(Collectors.toSet())); - try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(Paths.get(controlerIO.getOutputFilename( FILENAME+ ".txt"))), CSVFormat.DEFAULT.withDelimiter(DEL))) { + try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(Paths.get(controllerIO.getOutputFilename( FILENAME+ ".csv"))), CSVFormat.DEFAULT.withDelimiter(this.delimiter.charAt(0)))) { csvPrinter.print("Iteration"); csvPrinter.printRecord(allModes); @@ -122,7 +123,7 @@ private void writeVKTText() { chart.addSeries(mode, value); } chart.addMatsimLogo(); - chart.saveAsPng(controlerIO.getOutputFilename(FILENAME+ ".png"), 1024, 768); + chart.saveAsPng(controllerIO.getOutputFilename(FILENAME+ ".png"), 1024, 768); } diff --git a/matsim/src/main/java/org/matsim/analysis/ScoreStatsControlerListener.java b/matsim/src/main/java/org/matsim/analysis/ScoreStatsControlerListener.java index 5d20ff35c15..ecaea8e9aef 100644 --- a/matsim/src/main/java/org/matsim/analysis/ScoreStatsControlerListener.java +++ b/matsim/src/main/java/org/matsim/analysis/ScoreStatsControlerListener.java @@ -20,21 +20,15 @@ package org.matsim.analysis; -import java.io.BufferedWriter; -import java.io.IOException; -import java.util.*; -import java.util.stream.Collectors; - import jakarta.inject.Inject; -import jakarta.inject.Provider; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.matsim.api.core.v01.population.Person; import org.matsim.api.core.v01.population.Plan; import org.matsim.api.core.v01.population.Population; +import org.matsim.core.config.Config; import org.matsim.core.config.groups.ControllerConfigGroup; -import org.matsim.core.config.groups.ScoringConfigGroup; +import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.controler.OutputDirectoryHierarchy; import org.matsim.core.controler.events.IterationEndsEvent; import org.matsim.core.controler.events.ShutdownEvent; @@ -44,11 +38,20 @@ import org.matsim.core.controler.listener.StartupListener; import org.matsim.core.population.PersonUtils; import org.matsim.core.population.PopulationUtils; -import org.matsim.core.router.TripRouter; import org.matsim.core.utils.charts.XYLineChart; import org.matsim.core.utils.io.IOUtils; import org.matsim.core.utils.io.UncheckedIOException; +import java.io.BufferedWriter; +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.TreeMap; +import java.util.stream.Collectors; + /** * Calculates at the end of each iteration the following statistics: *
    @@ -65,13 +68,12 @@ */ public class ScoreStatsControlerListener implements StartupListener, IterationEndsListener, ShutdownListener, ScoreStats { - public static final String FILENAME_SCORESTATS = "scorestats"; + public enum ScoreItem { worst, best, average, executed }; - public static enum ScoreItem { worst, best, average, executed } ; - - final private Population population; - final private BufferedWriter out; - final private String fileName; + private final Population population; + private final OutputDirectoryHierarchy controllerIO; + private final String delimiter; + private final BufferedWriter out; private final boolean createPNG; private final ControllerConfigGroup controllerConfigGroup; @@ -85,14 +87,14 @@ public static enum ScoreItem { worst, best, average, executed } ; private final static Logger log = LogManager.getLogger(ScoreStatsControlerListener.class); @Inject - ScoreStatsControlerListener(ControllerConfigGroup controllerConfigGroup, Population population, OutputDirectoryHierarchy controlerIO, - ScoringConfigGroup scoreConfig, Provider tripRouterFactory ) { - + ScoreStatsControlerListener(ControllerConfigGroup controllerConfigGroup, Population population, OutputDirectoryHierarchy controllerIO, + GlobalConfigGroup globalConfig ) { this.controllerConfigGroup = controllerConfigGroup; this.population = population; - this.fileName = controlerIO.getOutputFilename(FILENAME_SCORESTATS); + this.controllerIO = controllerIO; + this.delimiter = globalConfig.getDefaultDelimiter(); this.createPNG = controllerConfigGroup.isCreateGraphs(); - this.out = IOUtils.getBufferedWriter(this.fileName + ".txt"); + this.out = IOUtils.getBufferedWriter(controllerIO.getOutputFilename("scorestats.csv")); Set subpopulations = population.getPersons().values().stream() .map(PopulationUtils::getSubpopulation) @@ -100,13 +102,15 @@ public static enum ScoreItem { worst, best, average, executed } ; .collect(Collectors.toSet()); for (String sub : subpopulations) { - perSubpop.put(sub, new ScoreHist(new HashMap<>(), IOUtils.getBufferedWriter(this.fileName + "_" + sub + ".txt"))); + this.perSubpop.put(sub, new ScoreHist(new HashMap<>(), IOUtils.getBufferedWriter(controllerIO.getOutputFilename("scorestats_" + sub + ".csv")))); } try { - this.out.write("ITERATION\tavg. EXECUTED\tavg. WORST\tavg. AVG\tavg. BEST\n"); - for (Map.Entry e : perSubpop.entrySet()) { - e.getValue().out.write("ITERATION\tavg. EXECUTED\tavg. WORST\tavg. AVG\tavg. BEST\n"); + this.out.write("iteration" + this.delimiter + "avg_executed" + this.delimiter + + "avg_worst" + this.delimiter + "avg_average" + this.delimiter + "avg_best\n"); + for (Map.Entry e : this.perSubpop.entrySet()) { + e.getValue().out.write("iteration" + this.delimiter + "avg_executed" + this.delimiter + + "avg_worst" + this.delimiter + "avg_average" + this.delimiter + "avg_best\n"); } } catch (IOException e) { @@ -116,13 +120,13 @@ public static enum ScoreItem { worst, best, average, executed } ; @Override public void notifyStartup(final StartupEvent event) { - this.minIteration = controllerConfigGroup.getFirstIteration(); + this.minIteration = this.controllerConfigGroup.getFirstIteration(); // int maxIter = controlerConfigGroup.getLastIteration(); // int iterations = maxIter - this.minIteration; // if (iterations > 5000) iterations = 5000; // limit the history size for ( ScoreItem item : ScoreItem.values() ) { - scoreHistory.put( item, new TreeMap<>() ) ; - perSubpop.forEach((s, data) -> data.hist.put(item, new TreeMap<>())); + this.scoreHistory.put( item, new TreeMap<>() ) ; + this.perSubpop.forEach((s, data) -> data.hist.put(item, new TreeMap<>())); } } @@ -152,9 +156,9 @@ private void collectScoreInfo(final IterationEndsEvent event) { log.info("-- avg. score of the best plan of each agent: " + (info.sumScoreBest / info.nofScoreBest)); try { - info.write(event.getIteration(), this.out); + info.write(event.getIteration(), this.out, this.delimiter); for (Map.Entry e : this.perSubpop.entrySet()) { - perSubpop.get(e.getKey()).write(event.getIteration(), e.getValue().out); + perSubpop.get(e.getKey()).write(event.getIteration(), e.getValue().out, this.delimiter); } } catch (IOException e) { @@ -180,7 +184,7 @@ private void collectScoreInfo(final IterationEndsEvent event) { chart.addSeries("avg. of plans' average score", this.scoreHistory.get( ScoreItem.average) ); chart.addSeries("avg. executed score", this.scoreHistory.get( ScoreItem.executed ) ); chart.addMatsimLogo(); - chart.saveAsPng(this.fileName + ".png", 800, 600); + chart.saveAsPng(this.controllerIO.getOutputFilename("scorestats.png"), 800, 600); } } @@ -188,13 +192,12 @@ private void collectScoreInfo(final IterationEndsEvent event) { public void notifyShutdown(final ShutdownEvent controlerShudownEvent) { try { this.out.close(); - for (ScoreHist data : perSubpop.values()) { + for (ScoreHist data : this.perSubpop.values()) { data.out.close(); } } catch (IOException e) { - e.printStackTrace(); + log.error(e.getMessage(), e); } - } @Override @@ -252,28 +255,31 @@ private void update(Person person) { // executed plan? if (PersonUtils.isSelected(plan)) { - sumExecutedScores += score; - nofExecutedScores++; + this.sumExecutedScores += score; + this.nofExecutedScores++; } } if (worstPlan != null) { - nofScoreWorst++; - sumScoreWorst += worstScore; + this.nofScoreWorst++; + this.sumScoreWorst += worstScore; } if (bestPlan != null) { - nofScoreBest++; - sumScoreBest += bestScore; + this.nofScoreBest++; + this.sumScoreBest += bestScore; } if (cntScores > 0) { - sumAvgScores += (sumScores / cntScores); - nofAvgScores++; + this.sumAvgScores += (sumScores / cntScores); + this.nofAvgScores++; } } - private void write(int iteration, BufferedWriter out) throws IOException { - out.write(iteration + "\t" + (sumExecutedScores / nofExecutedScores) + "\t" + - (sumScoreWorst / nofScoreWorst) + "\t" + (sumAvgScores / nofAvgScores) + "\t" + (sumScoreBest / nofScoreBest) + "\n"); + private void write(int iteration, BufferedWriter out, String delimiter) throws IOException { + out.write(iteration + delimiter + + (this.sumExecutedScores / this.nofExecutedScores) + delimiter + + (this.sumScoreWorst / this.nofScoreWorst) + delimiter + + (this.sumAvgScores / this.nofAvgScores) + delimiter + + (this.sumScoreBest / this.nofScoreBest) + "\n"); out.flush(); } diff --git a/matsim/src/main/java/org/matsim/analysis/TravelDistanceStats.java b/matsim/src/main/java/org/matsim/analysis/TravelDistanceStats.java index 7387cb0536d..587e06d1d88 100644 --- a/matsim/src/main/java/org/matsim/analysis/TravelDistanceStats.java +++ b/matsim/src/main/java/org/matsim/analysis/TravelDistanceStats.java @@ -28,6 +28,7 @@ import org.matsim.api.core.v01.population.Plan; import org.matsim.core.config.Config; import org.matsim.core.config.groups.ControllerConfigGroup; +import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.controler.OutputDirectoryHierarchy; import org.matsim.core.router.TripStructureUtils; import org.matsim.core.router.TripStructureUtils.Trip; @@ -59,9 +60,10 @@ public class TravelDistanceStats { private final ControllerConfigGroup controllerConfigGroup; - final private BufferedWriter out; - final private String legStatsPngName; - final private String tripStatsPngName; + private final BufferedWriter out; + private final String legStatsPngName; + private final String tripStatsPngName; + private final String delimiter; private double[] legStatsHistory = null; private double[] tripStatsHistory = null; @@ -69,10 +71,12 @@ public class TravelDistanceStats { private final static Logger log = LogManager.getLogger(TravelDistanceStats.class); @Inject - TravelDistanceStats(ControllerConfigGroup controllerConfigGroup, OutputDirectoryHierarchy controlerIO) { + TravelDistanceStats(ControllerConfigGroup controllerConfigGroup, OutputDirectoryHierarchy controlerIO, GlobalConfigGroup globalConfig) { this(controllerConfigGroup, controlerIO.getOutputFilename("traveldistancestats"), controlerIO.getOutputFilename("traveldistancestats") + "legs", - controlerIO.getOutputFilename("traveldistancestats") + "trips", controllerConfigGroup.isCreateGraphs()); + controlerIO.getOutputFilename("traveldistancestats") + "trips", + controllerConfigGroup.isCreateGraphs(), + globalConfig.getDefaultDelimiter()); } /** @@ -80,15 +84,16 @@ public class TravelDistanceStats { * @param createPNG true if in every iteration, the distance statistics should be visualized in a graph and written to disk. * @throws UncheckedIOException */ - public TravelDistanceStats(final Config config, final String filename, final boolean createPNG) throws UncheckedIOException { - this(config.controller(), filename, filename + "legs", filename + "trips", createPNG); + public TravelDistanceStats(final Config config, final String filename, final boolean createPNG, final String delimiter) throws UncheckedIOException { + this(config.controller(), filename, filename + "legs", filename + "trips", createPNG, delimiter); } - private TravelDistanceStats(ControllerConfigGroup controllerConfigGroup, String travelDistanceStatsFileName, - String legStatsPngName, String tripStatsPngName, boolean createPNG) { + private TravelDistanceStats(ControllerConfigGroup controllerConfigGroup, String travelDistanceStatsFileName, + String legStatsPngName, String tripStatsPngName, boolean createPNG, String delimiter) { this.controllerConfigGroup = controllerConfigGroup; this.legStatsPngName = legStatsPngName; this.tripStatsPngName = tripStatsPngName; + this.delimiter = delimiter; if (createPNG) { int iterations = controllerConfigGroup.getLastIteration() - controllerConfigGroup.getFirstIteration(); if (iterations > 5000) { @@ -97,13 +102,13 @@ private TravelDistanceStats(ControllerConfigGroup controllerConfigGroup, String this.legStatsHistory = new double[iterations+1]; this.tripStatsHistory = new double[iterations+1]; } - if (travelDistanceStatsFileName.toLowerCase(Locale.ROOT).endsWith(".txt")) { + if (travelDistanceStatsFileName.toLowerCase(Locale.ROOT).endsWith(".csv")) { this.out = IOUtils.getBufferedWriter(travelDistanceStatsFileName); } else { - this.out = IOUtils.getBufferedWriter(travelDistanceStatsFileName + ".txt"); + this.out = IOUtils.getBufferedWriter(travelDistanceStatsFileName + ".csv"); } try { - this.out.write("ITERATION\tavg. Average Leg distance\tavg. Average Trip distance\n"); + this.out.write("ITERATION" + this.delimiter + "avg. Average Leg distance" + this.delimiter + "avg. Average Trip distance\n"); } catch (IOException e) { throw new UncheckedIOException(e); } @@ -149,7 +154,7 @@ public void addIteration(int iteration, IdMap map) { log.info("(and teleported legs whose route contains a distance.)");// TODO: still valid? try { - this.out.write(iteration + "\t" + legStats.getAverage() + "\t" + tripStats.getAverage() + "\t" + "\n"); + this.out.write(iteration + this.delimiter + legStats.getAverage() + this.delimiter + tripStats.getAverage() + "\n"); this.out.flush(); } catch (IOException e) { e.printStackTrace(); diff --git a/matsim/src/main/java/org/matsim/core/controler/AbstractController.java b/matsim/src/main/java/org/matsim/core/controler/AbstractController.java index f18d01fa576..ccccfcd92c2 100644 --- a/matsim/src/main/java/org/matsim/core/controler/AbstractController.java +++ b/matsim/src/main/java/org/matsim/core/controler/AbstractController.java @@ -23,6 +23,7 @@ import org.apache.logging.log4j.Logger; import org.matsim.analysis.IterationStopWatch; import org.matsim.core.config.Config; +import org.matsim.core.config.groups.ControllerConfigGroup; import org.matsim.core.controler.listener.ControlerListener; import org.matsim.core.gbl.MatsimRandom; import org.matsim.core.utils.io.UncheckedIOException; @@ -172,12 +173,12 @@ public void run() { this.getStopwatch().endIteration(); try { - this.getStopwatch().writeTextFile(this.getControlerIO().getOutputFilename("stopwatch")); + this.getStopwatch().writeSeparatedFile(this.getControlerIO().getOutputFilename("stopwatch.csv"), config.global().getDefaultDelimiter()); } catch (UncheckedIOException e) { log.error("Could not write stopwatch file.", e); } if (config.controller().isCreateGraphs()) { - this.getStopwatch().writeGraphFile(this.getControlerIO().getOutputFilename("stopwatch")); + this.getStopwatch().writeGraphFile(this.getControlerIO().getOutputFilename("stopwatch.png", ControllerConfigGroup.CompressionType.none)); } log.info(MARKER + "ITERATION " + iteration + " ENDS"); log.info(Controler.DIVIDER); @@ -224,7 +225,7 @@ public void run() { // Java 7 seems able to detect which throwables this can be, thus no // need to wrap or anything... Nice! - // If an exception occurs in the finally bloc, this exception will be + // If an exception occurs in the finally-block, this exception will be // suppressed, but at least we logged it. throw t; } diff --git a/matsim/src/test/java/org/matsim/analysis/ModeStatsControlerListenerTest.java b/matsim/src/test/java/org/matsim/analysis/ModeStatsControlerListenerTest.java index cd0201d94d1..daf5a8cd90d 100644 --- a/matsim/src/test/java/org/matsim/analysis/ModeStatsControlerListenerTest.java +++ b/matsim/src/test/java/org/matsim/analysis/ModeStatsControlerListenerTest.java @@ -25,6 +25,7 @@ import org.matsim.core.config.ConfigUtils; import org.matsim.core.config.groups.ControllerConfigGroup; import org.matsim.core.config.groups.ControllerConfigGroup.CompressionType; +import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.config.groups.ScoringConfigGroup; import org.matsim.core.config.groups.ScoringConfigGroup.ModeParams; import org.matsim.core.controler.OutputDirectoryHierarchy; @@ -47,9 +48,9 @@ public class ModeStatsControlerListenerTest { int non_network_walk; int ride; int walk; - HashMap person3modes = new HashMap(); - HashMap person1modes = new HashMap(); - HashMap person2modes = new HashMap(); + HashMap person3modes = new HashMap<>(); + HashMap person1modes = new HashMap<>(); + HashMap person2modes = new HashMap<>(); @Rule public MatsimTestUtils utils = new MatsimTestUtils(); @@ -343,12 +344,11 @@ public void testModeStatsControlerListener() { person3.addPlan(plan3); population.addPerson(person3); - performTest(population, transportId, scoreConfig, utils.getOutputDirectory() + "/ModeStatsControlerListener", - planElem); + performTest(population, transportId, utils.getOutputDirectory() + "/ModeStatsControlerListener"); } private void performTest(Population population, TransportPlanningMainModeIdentifier transportId, - ScoringConfigGroup scoreConfig, String outputDirectory, List planElem) { + String outputDirectory) { ControllerConfigGroup controllerConfigGroup = new ControllerConfigGroup(); OutputDirectoryHierarchy controlerIO = new OutputDirectoryHierarchy(outputDirectory, @@ -356,12 +356,12 @@ private void performTest(Population population, TransportPlanningMainModeIdentif controllerConfigGroup.setCreateGraphs(true); controllerConfigGroup.setFirstIteration(0); ModeStatsControlerListener modStatListner = new ModeStatsControlerListener(controllerConfigGroup, population, - controlerIO, scoreConfig, transportId); + controlerIO, new GlobalConfigGroup(), transportId); StartupEvent eventStart = new StartupEvent(null); modStatListner.notifyStartup(eventStart); - HashMap modesIter0 = new HashMap(); + HashMap modesIter0 = new HashMap<>(); IterationEndsEvent event0 = new IterationEndsEvent(null, 0, false); modStatListner.notifyIterationEnds(event0); @@ -411,7 +411,7 @@ private void performTest(Population population, TransportPlanningMainModeIdentif //sum of the scores of each mode should add up to 1 private void readAndcompareValues(HashMap modes, int itr) { - String file = utils.getOutputDirectory() + "/ModeStatsControlerListener" + "/modestats.txt"; + String file = utils.getOutputDirectory() + "/ModeStatsControlerListener" + "/modestats.csv"; BufferedReader br; String line; int totalTrips = modes.get(TransportMode.car) + modes.get(TransportMode.bike) + modes.get(TransportMode.pt) @@ -420,21 +420,22 @@ private void readAndcompareValues(HashMap modes, int itr) { try { br = new BufferedReader(new FileReader(file)); String firstRow = br.readLine(); - String[] columnNames = firstRow.split(" "); + String delimiter = new GlobalConfigGroup().getDefaultDelimiter(); + String[] columnNames = firstRow.split(delimiter); decideColumns(columnNames); int iteration = 0; while ((line = br.readLine()) != null) { if (iteration == itr) { - String[] column = line.split(" "); + String[] column = line.split(delimiter); // checking if column number in greater than 0, because 0th column is always 'Iteration' and we don't need that --> see decideColumns() method - Double carvalue = (car > 0) ? Double.valueOf(column[car]) : 0; - Double walkvalue = (walk > 0) ? Double.valueOf(column[walk]) : 0; - Double ptvalue = (pt > 0) ? Double.valueOf(column[pt]) : 0; - Double bikevalue = (bike > 0) ? Double.valueOf(column[bike]) : 0; - Double non_network_walkvalue = (non_network_walk > 0) ? Double.valueOf(column[non_network_walk]) + double carvalue = (car > 0) ? Double.parseDouble(column[car]) : 0; + double walkvalue = (walk > 0) ? Double.parseDouble(column[walk]) : 0; + double ptvalue = (pt > 0) ? Double.parseDouble(column[pt]) : 0; + double bikevalue = (bike > 0) ? Double.parseDouble(column[bike]) : 0; + double non_network_walkvalue = (non_network_walk > 0) ? Double.valueOf(column[non_network_walk]) : 0; - Double othervalue = (other > 0) ? Double.valueOf(column[other]) : 0; - Double ridevalue = (ride > 0) ? Double.valueOf(column[ride]) : 0; + double othervalue = (other > 0) ? Double.parseDouble(column[other]) : 0; + double ridevalue = (ride > 0) ? Double.parseDouble(column[ride]) : 0; Assert.assertEquals("car mode has an unexpected score", (modes.get(TransportMode.car).doubleValue() / totalTrips), carvalue, 0); Assert.assertEquals("walk mode has an unexpected score", @@ -460,7 +461,6 @@ private void readAndcompareValues(HashMap modes, int itr) { iteration++; } } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } diff --git a/matsim/src/test/java/org/matsim/analysis/PHbyModeCalculatorTest.java b/matsim/src/test/java/org/matsim/analysis/PHbyModeCalculatorTest.java index 51be57b965e..e123bfceea9 100644 --- a/matsim/src/test/java/org/matsim/analysis/PHbyModeCalculatorTest.java +++ b/matsim/src/test/java/org/matsim/analysis/PHbyModeCalculatorTest.java @@ -21,6 +21,7 @@ import org.matsim.api.core.v01.population.PlanElement; import org.matsim.core.config.groups.ControllerConfigGroup; import org.matsim.core.config.groups.ControllerConfigGroup.CompressionType; +import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.controler.OutputDirectoryHierarchy; import org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting; import org.matsim.core.router.StageActivityTypeIdentifier; @@ -92,7 +93,7 @@ private void performTest(IdMap map, String outputDirectory) { controllerConfigGroup.setCreateGraphs(true); controllerConfigGroup.setFirstIteration(0); controllerConfigGroup.setLastIteration(10); - PHbyModeCalculator phbyModeCalculator = new PHbyModeCalculator(controllerConfigGroup, controlerIO); + PHbyModeCalculator phbyModeCalculator = new PHbyModeCalculator(controllerConfigGroup, controlerIO, new GlobalConfigGroup()); phbyModeCalculator.addIteration(1, map); phbyModeCalculator.writeOutput(); @@ -157,25 +158,26 @@ private void readAndValidateValues(int itr, IdMap map) { } } - String file = utils.getOutputDirectory() + "/PHbyModeCalculator" + "/ph_modestats.txt"; + String file = utils.getOutputDirectory() + "/PHbyModeCalculator" + "/ph_modestats.csv"; BufferedReader br; String line; try { br = new BufferedReader(new FileReader(file)); String firstRow = br.readLine(); - String[] columnNames = firstRow.split(" "); + String delimiter = new GlobalConfigGroup().getDefaultDelimiter(); + String[] columnNames = firstRow.split(delimiter); decideColumns(columnNames); int iteration = 1; while ((line = br.readLine()) != null) { if (iteration == itr) { - String[] column = line.split(" "); + String[] column = line.split(delimiter); // checking if column number in greater than 0, because 0th column is always // 'Iteration' and we don't need that --> see decideColumns() method - Double car_travel_value = Double.valueOf(column[car_travel]); - Double pt_travel_value = Double.valueOf(column[pt_travel]); - Double walk_travel_value = Double.valueOf(column[walk_travel]); - Double pt_wait_value = Double.valueOf(column[pt_wait]); - Double stageActivity_wait_value = Double.valueOf(column[stageActivity_wait]); + double car_travel_value = Double.parseDouble(column[car_travel]); + double pt_travel_value = Double.parseDouble(column[pt_travel]); + double walk_travel_value = Double.parseDouble(column[walk_travel]); + double pt_wait_value = Double.parseDouble(column[pt_wait]); + double stageActivity_wait_value = Double.parseDouble(column[stageActivity_wait]); Assert.assertEquals("car_travel hour does not match", Math.round(modeValues.get("car") / 3600.0), car_travel_value, 0); diff --git a/matsim/src/test/java/org/matsim/analysis/PKMbyModeCalculatorTest.java b/matsim/src/test/java/org/matsim/analysis/PKMbyModeCalculatorTest.java index b516327d3c3..ba62f04f994 100644 --- a/matsim/src/test/java/org/matsim/analysis/PKMbyModeCalculatorTest.java +++ b/matsim/src/test/java/org/matsim/analysis/PKMbyModeCalculatorTest.java @@ -19,6 +19,7 @@ import org.matsim.api.core.v01.population.Plan; import org.matsim.core.config.groups.ControllerConfigGroup; import org.matsim.core.config.groups.ControllerConfigGroup.CompressionType; +import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.controler.OutputDirectoryHierarchy; import org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting; import org.matsim.core.population.PopulationUtils; @@ -159,7 +160,7 @@ private void performTest(IdMap map, HashMap modeCa controllerConfigGroup.setCreateGraphs(true); controllerConfigGroup.setFirstIteration(0); controllerConfigGroup.setLastIteration(10); - PKMbyModeCalculator pkmbyModeCalculator = new PKMbyModeCalculator(controllerConfigGroup, controlerIO); + PKMbyModeCalculator pkmbyModeCalculator = new PKMbyModeCalculator(controllerConfigGroup, controlerIO, new GlobalConfigGroup()); // iteration 0 pkmbyModeCalculator.addIteration(0, map); pkmbyModeCalculator.writeOutput(); @@ -198,23 +199,23 @@ private void performTest(IdMap map, HashMap modeCa /************ Reading and validating the output ************/ private void readAndValidateValues(int itr, Double totalCar, Double totalPt, Double totalWalk) { - String file = utils.getOutputDirectory() + "/PKMbyModeCalculator" + "/pkm_modestats.txt"; + String file = utils.getOutputDirectory() + "/PKMbyModeCalculator" + "/pkm_modestats.csv"; BufferedReader br; String line; try { br = new BufferedReader(new FileReader(file)); String firstRow = br.readLine(); - String[] columnNames = firstRow.split(" "); + String[] columnNames = firstRow.split(new GlobalConfigGroup().getDefaultDelimiter()); decideColumns(columnNames); int iteration = 0; while ((line = br.readLine()) != null) { if (iteration == itr) { - String[] column = line.split(" "); + String[] column = line.split(new GlobalConfigGroup().getDefaultDelimiter()); // checking if column number in greater than 0, because 0th column is always // 'Iteration' and we don't need that --> see decideColumns() method - Double carStat = (car > 0) ? Double.valueOf(column[car]) : 0; - Double ptStat = (pt > 0) ? Double.valueOf(column[pt]) : 0; - Double walkStat = (walk > 0) ? Double.valueOf(column[walk]) : 0; + double carStat = (car > 0) ? Double.parseDouble(column[car]) : 0; + double ptStat = (pt > 0) ? Double.parseDouble(column[pt]) : 0; + double walkStat = (walk > 0) ? Double.parseDouble(column[walk]) : 0; Assert.assertEquals("Car stats score does not match", Math.round((totalCar / 1000)), carStat, 0); Assert.assertEquals("PT stats score does not match", Math.round((totalPt / 1000)), ptStat, 0); diff --git a/matsim/src/test/java/org/matsim/analysis/ScoreStatsControlerListenerTest.java b/matsim/src/test/java/org/matsim/analysis/ScoreStatsControlerListenerTest.java index ae2fd2e7596..33345fdfb2b 100644 --- a/matsim/src/test/java/org/matsim/analysis/ScoreStatsControlerListenerTest.java +++ b/matsim/src/test/java/org/matsim/analysis/ScoreStatsControlerListenerTest.java @@ -19,9 +19,11 @@ import org.matsim.api.core.v01.population.Plan; import org.matsim.api.core.v01.population.Population; import org.matsim.api.core.v01.population.Route; +import org.matsim.core.config.Config; import org.matsim.core.config.ConfigUtils; import org.matsim.core.config.groups.ControllerConfigGroup; import org.matsim.core.config.groups.ControllerConfigGroup.CompressionType; +import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.controler.OutputDirectoryHierarchy; import org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting; import org.matsim.core.controler.events.IterationEndsEvent; @@ -480,7 +482,7 @@ private void performTest(String outputDirectory, Population population) throws I controllerConfigGroup.setCreateGraphs(true); controllerConfigGroup.setFirstIteration(0); controllerConfigGroup.setLastIteration(10); - ScoreStatsControlerListener scoreStatsControlerListener = new ScoreStatsControlerListener(controllerConfigGroup, population, controlerIO, null, null); + ScoreStatsControlerListener scoreStatsControlerListener = new ScoreStatsControlerListener(controllerConfigGroup, population, controlerIO, new GlobalConfigGroup()); String outDir = utils.getOutputDirectory() + "/ScoreStatsControlerListener"; @@ -520,25 +522,26 @@ private void performTest(String outputDirectory, Population population) throws I private void readAndValidateValues(String outDir, int itr, Population population) throws IOException { - String file = outDir + "/scorestats.txt"; + String file = outDir + "/scorestats.csv"; BufferedReader br; String line; br = new BufferedReader(new FileReader(file)); String firstRow = br.readLine(); - String[] columnNames = firstRow.split(" "); + String delimiter = new GlobalConfigGroup().getDefaultDelimiter(); + String[] columnNames = firstRow.split(delimiter); decideColumns(columnNames); int iteration = 0; while ((line = br.readLine()) != null) { if (iteration == itr) { - String[] column = line.split(" "); + String[] column = line.split(delimiter); // checking if column number in greater than 0, because 0th column is always // 'Iteration' and we don't need that --> see decideColumns() method - Double avgExecuted = (avgexecuted > 0) ? Double.valueOf(column[avgexecuted]) : 0; - Double avgWorst = (avgworst > 0) ? Double.valueOf(column[avgworst]) : 0; - Double avgBest = (avgbest > 0) ? Double.valueOf(column[avgbest]) : 0; - Double avgAverage = (avgaverage > 0) ? Double.valueOf(column[avgaverage]) : 0; + double avgExecuted = (avgexecuted > 0) ? Double.parseDouble(column[avgexecuted]) : 0; + double avgWorst = (avgworst > 0) ? Double.parseDouble(column[avgworst]) : 0; + double avgBest = (avgbest > 0) ? Double.parseDouble(column[avgbest]) : 0; + double avgAverage = (avgaverage > 0) ? Double.parseDouble(column[avgaverage]) : 0; Assert.assertEquals("avg. executed score does not match", (getScore(population, "avgexecuted")/(4-itr)), avgExecuted, 0); @@ -552,10 +555,10 @@ private void readAndValidateValues(String outDir, int itr, Population populatio iteration++; } - Assertions.assertThat(new File(outDir, "scorestats_group1.txt")) + Assertions.assertThat(new File(outDir, "scorestats_group1.csv")) .isFile(); - Assertions.assertThat(new File(outDir, "scorestats_group2.txt")) + Assertions.assertThat(new File(outDir, "scorestats_group2.csv")) .isFile(); } @@ -603,19 +606,19 @@ private void decideColumns(String[] columnNames) { String name = columnNames[i]; switch (name) { - case "avg. EXECUTED": + case "avg_executed": avgexecuted = i; break; - case "avg. WORST": + case "avg_worst": avgworst = i; break; - case "avg. AVG": + case "avg_average": avgaverage = i; break; - case "avg. BEST": + case "avg_best": avgbest = i; break; diff --git a/matsim/src/test/java/org/matsim/analysis/TravelDistanceStatsTest.java b/matsim/src/test/java/org/matsim/analysis/TravelDistanceStatsTest.java index 627ba72c840..e1e0a8fb0d2 100644 --- a/matsim/src/test/java/org/matsim/analysis/TravelDistanceStatsTest.java +++ b/matsim/src/test/java/org/matsim/analysis/TravelDistanceStatsTest.java @@ -23,6 +23,7 @@ import org.matsim.api.core.v01.population.Route; import org.matsim.core.config.groups.ControllerConfigGroup; import org.matsim.core.config.groups.ControllerConfigGroup.CompressionType; +import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.controler.OutputDirectoryHierarchy; import org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting; import org.matsim.core.population.PopulationUtils; @@ -35,9 +36,9 @@ */ public class TravelDistanceStatsTest { - HashMap person3modes = new HashMap(); - HashMap person1modes = new HashMap(); - HashMap person2modes = new HashMap(); + HashMap person3modes = new HashMap<>(); + HashMap person1modes = new HashMap<>(); + HashMap person2modes = new HashMap<>(); private int avglegdis; private int avgtripdis; private Double person1legsum; @@ -342,7 +343,7 @@ private void performTest(IdMap map, String outputDirectory) { controllerConfigGroup.setCreateGraphs(true); controllerConfigGroup.setFirstIteration(0); controllerConfigGroup.setLastIteration(10); - TravelDistanceStats travelDistanceStats = new TravelDistanceStats(controllerConfigGroup, controlerIO); + TravelDistanceStats travelDistanceStats = new TravelDistanceStats(controllerConfigGroup, controlerIO, new GlobalConfigGroup()); travelDistanceStats.addIteration(0, map); readAndValidateValues(0, person1legsum + person2legsum + person3legsum, 12, person1TotalNumberOfLegs + person2TotalNumberOfLegs + person3TotalNumberOfLegs); @@ -357,22 +358,23 @@ private void performTest(IdMap map, String outputDirectory) { private void readAndValidateValues(int itr, Double legSum, int totalTrip, long totalLeg) { - String file = utils.getOutputDirectory() + "/TravelDistanceStat" + "/traveldistancestats.txt"; + String file = utils.getOutputDirectory() + "/TravelDistanceStat" + "/traveldistancestats.csv"; BufferedReader br; String line; try { br = new BufferedReader(new FileReader(file)); String firstRow = br.readLine(); - String[] columnNames = firstRow.split(" "); + String delimiter = new GlobalConfigGroup().getDefaultDelimiter(); + String[] columnNames = firstRow.split(delimiter); decideColumns(columnNames); int iteration = 0; while ((line = br.readLine()) != null) { if (iteration == itr) { - String[] column = line.split(" "); + String[] column = line.split(delimiter); // checking if column number in greater than 0, because 0th column is always // 'Iteration' and we don't need that --> see decideColumns() method - Double avgLegvalue = (avglegdis > 0) ? Double.valueOf(column[avglegdis]) : 0; - Double avgTripvalue = (avgtripdis > 0) ? Double.valueOf(column[avgtripdis]) : 0; + double avgLegvalue = (avglegdis > 0) ? Double.parseDouble(column[avglegdis]) : 0; + double avgTripvalue = (avgtripdis > 0) ? Double.parseDouble(column[avgtripdis]) : 0; Assert.assertEquals("avg. Average Trip distance does not match", (legSum / totalTrip), avgTripvalue, 0); @@ -384,7 +386,6 @@ private void readAndValidateValues(int itr, Double legSum, int totalTrip, long t iteration++; } } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); }