diff --git a/src/main/R/populationAnalysis/blockedRoadAgentsAnalysis.R b/src/main/R/populationAnalysis/blockedRoadAgentsAnalysis.R index d0dbb0f3..3e1466e7 100644 --- a/src/main/R/populationAnalysis/blockedRoadAgentsAnalysis.R +++ b/src/main/R/populationAnalysis/blockedRoadAgentsAnalysis.R @@ -1,58 +1,94 @@ -library(tidyverse) -library(matsim) -library(ggalluvial) -library(lubridate) -# library(ggplot2) - - -setwd("Y:/net/ils/matsim-kelheim/run-roadBlock/output/") - -baseDir <- "Y:/net/ils/matsim-kelheim/run-roadBlock/output/kelheim-v2.0-network-with-pt.xml.gz-seed1111-CORE/" -policyDir <- "Y:/net/ils/matsim-kelheim/run-roadBlock/output/output-casekelheim-v2.0-network-with-pt_blocked-Maximiliansbruecke.xml.gz-seed1111/" - -tsvFile <- "analysis-road-usage/blocked_infrastructure_trip_comparison.tsv" - -"" - -ifelse(endsWith(policyDir, "/"),tsvFile <- tsvFile, tsvFile <- paste0("/",tsvFile)) - - -affectedTrips <- read.csv2(paste0(policyDir,tsvFile), stringsAsFactors = FALSE, header = TRUE, encoding = "UTF-8", sep="\t") - -tripsBase <- readTripsTable(pathToMATSimOutputDirectory = baseDir) -tripsPolicy <- readTripsTable(pathToMATSimOutputDirectory = policyDir) - -# filter trips with usage of blocked infrastructure in base case only -tripsBase <- tripsBase %>% - filter(trip_id %in% affectedTrips$trip_id) - -tripsPolicy <- tripsPolicy %>% - filter(trip_id %in% affectedTrips$trip_id) - -# join relevant trips by id and join + filter important stats only -tripsCombined <- left_join(tripsBase, tripsPolicy, by = "trip_id") %>% - filter(trip_number.x == trip_number.y) %>% - select(trip_id, - trav_time.x, - trav_time.y, - traveled_distance.x, - traveled_distance.y, - modes.x, - modes.y) %>% - rename("trav_time_base" = trav_time.x, - "trav_time_policy" = trav_time.y, - "traveled_distance_base" = traveled_distance.x, - "traveled_distance_policy" = traveled_distance.y, - "modes_base" = modes.x, - "modes_policy" = modes.y) - -tripsCombined <- tripsCombined %>% - mutate(trav_time_diff_s = trav_time_policy - trav_time_base, - traveled_distance_diff_m = traveled_distance_policy - traveled_distance_base) - -tripsChangedModeChain <- tripsCombined %>% - filter(modes_base != modes_policy) - - - - +library(tidyverse) +library(matsim) +library(ggalluvial) +library(lubridate) + + +setwd("Y:/net/ils/matsim-kelheim/run-roadBlock/output/") + +baseDir <- "Y:/net/ils/matsim-kelheim/run-roadBlock/output/kelheim-v2.0-network-with-pt.xml.gz-seed5678-CORE/" +policyDir <- "Y:/net/ils/matsim-kelheim/run-roadBlock/output/output-casekelheim-v2.0-network-with-pt_blocked-RegensburgerStr.xml.gz-seed5678-CORE/" + +tsvFile <- "analysis-road-usage/blocked_infrastructure_trip_comparison.tsv" + +ifelse(endsWith(policyDir, "/"),tsvFile <- tsvFile, tsvFile <- paste0("/",tsvFile)) + + +affectedTrips <- read.csv2(paste0(policyDir,tsvFile), stringsAsFactors = FALSE, header = TRUE, encoding = "UTF-8", sep="\t") + +tripsBase <- readTripsTable(pathToMATSimOutputDirectory = baseDir) +tripsPolicy <- readTripsTable(pathToMATSimOutputDirectory = policyDir) + +# filter trips with usage of blocked infrastructure in base case only +tripsBase <- tripsBase %>% + filter(trip_id %in% affectedTrips$trip_id) + +tripsPolicy <- tripsPolicy %>% + filter(trip_id %in% affectedTrips$trip_id) + +# join relevant trips by id and join + filter important stats only +tripsCombined <- left_join(tripsBase, tripsPolicy, by = "trip_id") %>% + filter(trip_number.x == trip_number.y) %>% + select(trip_id, + trav_time.x, + trav_time.y, + traveled_distance.x, + traveled_distance.y, + modes.x, + modes.y, + main_mode.x, + main_mode.y, + wait_time.x, + wait_time.y, + euclidean_distance.x, + euclidean_distance.y) %>% + rename("trav_time_base" = trav_time.x, + "trav_time_policy" = trav_time.y, + "traveled_distance_base" = traveled_distance.x, + "traveled_distance_policy" = traveled_distance.y, + "modes_base" = modes.x, + "modes_policy" = modes.y, + "main_mode_base" = main_mode.x, + "main_mode_policy" = main_mode.y, + "wait_time_base" = wait_time.x, + "wait_time_policy" = wait_time.y, + "euclidean_distance_base" = euclidean_distance.x, + "euclidean_distance_policy" = euclidean_distance.y) + +tripsCombined <- tripsCombined %>% + mutate(trav_time_diff_s = trav_time_policy - trav_time_base, + traveled_distance_diff_m = traveled_distance_policy - traveled_distance_base, + trav_time_base = seconds(trav_time_base), + trav_time_policy = seconds(trav_time_policy), + wait_time_base = seconds(wait_time_base), + wait_time_policy = seconds(wait_time_policy)) + +meanTravTimeBase <- mean(tripsCombined$trav_time_base) +meanTravTimePolicy <- mean(tripsCombined$trav_time_policy) +meanTravDistBase <- mean(tripsCombined$traveled_distance_base) +meanTravDistPolicy <- mean(tripsCombined$traveled_distance_policy) +meanEuclDistBase <- mean(tripsCombined$euclidean_distance_base) +meanEuclDistPolicy <- mean(tripsCombined$euclidean_distance_policy) +meanWaitTimeBase <- mean(tripsCombined$wait_time_base) +meanWaitTimePolicy <- mean(tripsCombined$wait_time_policy) + +tripsChangedMainMode <- tripsCombined %>% + filter(main_mode_base != main_mode_policy) + +noTripsChangedMainMode <- nrow(tripsChangedMainMode) + +#save avg values into df +avgValues <- setNames(data.frame(matrix(ncol = 9, nrow = 0)), c("meanTravTimeBase[s]", "meanTravTimePolicy[s]", "meanTravDistBase[m]", "meanTravDistPolicy[m]", "meanEuclDistBase[m]", + "meanEuclDistPolicy[m]", "meanWaitTimeBase[s]", "meanWaitTimePolicy[s]","nrTripsChangedMainMode")) + +avgValuesDataset <- data.frame(meanTravTimeBase, meanTravTimePolicy,meanTravDistBase,meanTravDistPolicy,meanEuclDistBase,meanEuclDistPolicy,meanWaitTimeBase,meanWaitTimePolicy,noTripsChangedMainMode) +names(avgValuesDataset) <- names(avgValues) +avgValues <- rbind(avgValues,avgValuesDataset) + +tsvFileName <- paste0("avg_params_blocked_infrastructure_agents.tsv") +write.table(avgValues,paste0(policyDir,"analysis-road-usage/",tsvFileName),quote=FALSE, row.names=FALSE, dec=".", sep="\t") +print(paste0("avg values for agents affected by blocked infrastructure ",policyDir,"analysis-road-usage/",tsvFileName)) + + + + diff --git a/src/main/java/org/matsim/analysis/postAnalysis/BlockedInfrastructureRouteAnalysis.java b/src/main/java/org/matsim/analysis/postAnalysis/BlockedInfrastructureRouteAnalysis.java index 0af3394a..460ef8c8 100644 --- a/src/main/java/org/matsim/analysis/postAnalysis/BlockedInfrastructureRouteAnalysis.java +++ b/src/main/java/org/matsim/analysis/postAnalysis/BlockedInfrastructureRouteAnalysis.java @@ -2,6 +2,7 @@ import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; +import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.locationtech.jts.geom.Geometry; @@ -22,6 +23,7 @@ import picocli.CommandLine; import java.io.FileWriter; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; @@ -31,7 +33,7 @@ /** * Analyse routes for agents which use a certain infrastructure segment. * 1) Retrieves agents which use the infrastructure (e.g. in base case) - * 2) Searches for the (new) routes of the agents in a policy case + * 2) Save trips of those agents to tsv file for further R analysis. * * @author Simon Meinhardt (simei94) */ @@ -63,7 +65,6 @@ public Integer call() throws Exception { Path basePopulationPath = globFile(directoryBase, "*output_plans*"); - Path policyPopulationPath = globFile(directoryPolicy, "*output_plans*"); //output will be written into policy case folder Path outputFolder = Path.of(directoryPolicy.toString() + "/analysis-road-usage"); @@ -78,48 +79,44 @@ public Integer call() throws Exception { Population populationBase = ScenarioUtils.loadScenario(config).getPopulation(); - config.plans().setInputFile(policyPopulationPath.toString()); - Population populationPolicy = ScenarioUtils.loadScenario(config).getPopulation(); - //get links, which are affected by blocked infrastructure List blockedLinks = getBlockedLinks(network, blockedInfrastructureArea); relevantTripsBase = getTripsFromPlans(populationBase, blockedLinks); - Map, Map> relevantTripsPolicy = getTripsFromPlans(populationPolicy, blockedLinks); //writeResults String outputFile = outputFolder + "/" + "blocked_infrastructure_trip_comparison.tsv"; CSVPrinter tsvPrinter = new CSVPrinter(new FileWriter(outputFile), CSVFormat.TDF); - List header = new ArrayList<>(); - header.add("person_id"); - header.add("trip_number"); - header.add("trip_id"); -// header.add("tripBase"); -// header.add("tripPolicy"); + try { + List header = new ArrayList<>(); + header.add("person_id"); + header.add("trip_number"); + header.add("trip_id"); - tsvPrinter.printRecord(header); + tsvPrinter.printRecord(header); - for (Id personId : relevantTripsBase.keySet()) { + for (Map.Entry, Map> entry : relevantTripsBase.entrySet()) { - Integer tripNumber; + Integer tripNumber; - for (Integer index : relevantTripsBase.get(personId).keySet()) { + for (Integer index : relevantTripsBase.get(entry.getKey()).keySet()) { - tripNumber = index + 1; + tripNumber = index + 1; - List entry = new ArrayList<>(); - entry.add(personId.toString()); - entry.add(tripNumber.toString()); - entry.add(personId + "_" + tripNumber); -// entry.add(relevantTripsBase.get(personId).get(index).toString()); -// entry.add(relevantTripsPolicy.get(personId).get(index).toString()); + List outputEntry = new ArrayList<>(); + outputEntry.add(entry.getKey().toString()); + outputEntry.add(tripNumber.toString()); + outputEntry.add(entry.getKey() + "_" + tripNumber); - tsvPrinter.printRecord(entry); + tsvPrinter.printRecord(outputEntry); + } } + } catch (IOException e) { + e.printStackTrace(); + } finally { + tsvPrinter.close(); + log.log(Level.INFO, "Analysis output has been written to: {}", outputFile); } - tsvPrinter.close(); - - log.info("Analysis output has been written to: " + outputFile); return 0; } @@ -129,10 +126,8 @@ private List getBlockedLinks(Network network, Geometry geometry) { List blockedLinks = new ArrayList<>(); for (Link link : network.getLinks().values()) { - if (!link.getId().toString().contains("pt_")) { - if (isInsideArea(link, geometry)) { - blockedLinks.add(link.getId().toString()); - } + if (!link.getId().toString().contains("pt_") && isInsideArea(link, geometry)) { + blockedLinks.add(link.getId().toString()); } } return blockedLinks; @@ -169,13 +164,13 @@ private Map, Map> getTripsFromPlans //get corresponding trips to base case trips (where the now-blocked infrastructure is used) log.info("Analyzing legs on blocked infrastructure for policy case population"); - for (Id personId : relevantTripsBase.keySet()) { - for ( Integer index : relevantTripsBase.get(personId).keySet()) { - relevantTrips.putIfAbsent(personId, new HashMap<>()); + for (Map.Entry, Map> entry : relevantTripsBase.entrySet()) { + for ( Integer index : relevantTripsBase.get(entry.getKey()).keySet()) { + relevantTrips.putIfAbsent(entry.getKey(), new HashMap<>()); - TripStructureUtils.Trip policyTrip = TripStructureUtils.getTrips(population.getPersons().get(personId).getSelectedPlan()).get(index); + TripStructureUtils.Trip policyTrip = TripStructureUtils.getTrips(population.getPersons().get(entry.getKey()).getSelectedPlan()).get(index); - relevantTrips.get(personId).put(index, policyTrip); + relevantTrips.get(entry.getKey()).put(index, policyTrip); } } }