Skip to content

Commit

Permalink
merge latest master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
luchengqi7 committed Sep 18, 2024
2 parents ff66b9a + 8b27d43 commit cf7467b
Show file tree
Hide file tree
Showing 13 changed files with 520 additions and 235 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<!--<version>14.0-PR1452</version>-->

<!-- snapshot == not recommended: rather use PR-labelled release!-->
<version>2025.0-PR3429</version>
<version>2025.0-PR3486</version>
</parent>


Expand Down
52 changes: 52 additions & 0 deletions src/main/R/commuter-analysis.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
library(matsim)
library(tidyverse)
library(readr)
library(sf)

#FILES
FILE_DIR = "../../shared-svn/projects/DiTriMo/data/commuters-by-town"
SIM <- paste0(FILE_DIR, "lausitz-v1.0-commuter.csv")
GEMEINDE <- paste0(FILE_DIR, "pgemeinden.csv")
COMMUTER <- paste0(FILE_DIR, "commuter.csv")
SHP <- paste0(FILE_DIR, "VG5000_GEM/VG5000_GEM.shp")
LAUSITZ.SHP <- paste0(FILE_DIR, "network-area/network-area.shp")

shp <- st_read(SHP)
lausitz.shp <- st_read(LAUSITZ.SHP)

sim <- read_csv(file = SIM)
gemeinden <- read_csv( file = GEMEINDE) %>%
mutate(code = str_remove(string = code, pattern = "P"))

commuter <- read_csv(file = COMMUTER) %>%
mutate(key = paste0(from, "-", to))

sim.1 <- sim %>%
filter(!is.na(from) & !is.na(to)) %>%
left_join(gemeinden, by = c("from" = "code")) %>%
left_join(gemeinden, by = c("from" = "code"), suffix = c("_from", "_to")) %>%
mutate(key = paste0(from, "-", to)) %>%
select(-c(from, to)) %>%
left_join(commuter, by = "key", suffix = c("_sim", "_real")) %>%
filter(!is.na(from)) %>%
# pivot_longer(cols = starts_with("n_"), names_to = "src", values_to = "n", names_prefix = "n_") %>%
arrange(desc(n_real))

breaks <- c(-Inf, 0.8, 1.2, Inf)
labels <- c("less", "exakt", "more")

sim.2 <- sim.1 %>%
filter(n_sim > 10) %>%
select(from, to, starts_with("n_"), starts_with("name_")) %>%
mutate(n_rel = n_sim / n_real,
quality = cut(n_rel, breaks = breaks, labels = labels))

ggplot(sim.2, aes(x = n_real, y = n_sim, col = quality)) +

geom_point() +

scale_x_log10() +

scale_y_log10() +

theme_bw()
34 changes: 28 additions & 6 deletions src/main/R/counts.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ devtools::install_github("matsim-vsp/matsim-r",ref="counts")
library(matsim)
library(tidyverse)

COUNTS <- "Y:/matsim-lausitz/input/v1.0/lausitz-v1.0-counts-car-bast.xml.gz"
NETWORK <- "Y:/matsim-lausitz/input/v1.0/lausitz-v1.0-network-with-pt.xml.gz"
COUNTS <- "../../public-svn/matsim/scenarios/countries/de/lausitz/input/v1.0/lausitz-v1.0-counts-car-bast.xml.gz"
NETWORK <- "../../public-svn/matsim/scenarios/countries/de/lausitz/input/v1.0/lausitz-v1.0-network-with-pt.xml.gz"

linkstats <- readLinkStats(runId = "v1.0-uncalibrated", file = "Y:/matsim-lausitz/qa/output/lausitz-25pct.output_linkstats.csv.gz")
linkstats <- readLinkStats(runId = "v1.0-uncalibrated", file = "Y:/matsim-lausitz/qa/output/lausitz-25pct.output_linkstats.csv.gz", sampleSize = 1)

counts <- readCounts(COUNTS)
network <- loadNetwork(NETWORK)
Expand All @@ -17,7 +17,7 @@ join <- mergeCountsAndLinks(counts = counts, linkStats = list(linkstats), netwo

#### VIA-styled scatterplot ####

FILE_DIR = "C:/Users/ACER/Desktop/Uni/VSP/Lausitz-Plots/"
FILE_DIR = "../../shared-svn/projects/DiTriMo/data/commuters-by-town"

createCountScatterPlot(joinedFrame = join)
ggsave(filename = paste0(FILE_DIR, "Traffic_Count_Scatterplot_with_freight.jpg"))
Expand All @@ -43,7 +43,7 @@ rm(join.dtv.distribution)

#### Analysis of Estimation Quality ####

join.est.quality <- processDtvEstimationQuality(joinedFrame = join, aggr = T) %>%
join.est.quality <- processDtvEstimationQuality(joinedFrame = join, aggr = F) %>%
filter(!type %in% c("residential", "unclassified", NA))

ggplot(join.est.quality, aes(estimation, share, fill = type)) +
Expand All @@ -59,4 +59,26 @@ ggplot(join.est.quality, aes(estimation, share, fill = type)) +
theme(legend.position = "none", axis.text.x = element_text(angle = 90))

rm(join.est.quality)
ggsave(filename = paste0(FILE_DIR, "Estimation_quality_by_road_type_with_freight.jpg"))
ggsave(filename = paste0(FILE_DIR, "Estimation_quality_by_road_type_with_freight.jpg"))


#### network plot ####

library(tmap)
library(tmaptools)
library(OpenStreetMap)
library(sf)

link.geom <- join %>%
left_join(network$links, by = c("loc_id" = "id")) %>%
mutate(geom = sprintf("LINESTRING(%s %s, %s %s)", x.from, y.from, x.to, y.to)) %>%
st_as_sf(crs = 25832, wkt = "geom") %>%
transmute(loc_id, type.x, rel_vol = volume / count, geom)

tmap_mode("view")

tm_shape(shp = link.geom) +
tm_lines(col = "estimation", style = "cont", lwd = 3.5, palette = c("red", "green", "blue"))

tm_shape(shp = link.geom) +
tm_lines(col = "rel_vol", style = "cont", lwd = 5, palette = c("red", "yellow", "green"), breaks = c(0, 0.05, 0.8, 2))
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.matsim.dashboards;

import org.matsim.core.config.Config;
import org.matsim.core.population.PersonUtils;
import org.matsim.simwrapper.Dashboard;
import org.matsim.simwrapper.DashboardProvider;
import org.matsim.simwrapper.SimWrapper;
Expand All @@ -20,8 +21,9 @@ public List<Dashboard> getDashboards(Config config, SimWrapper simWrapper) {
"lausitz_mode_share.csv",
"lausitz_mode_share_per_dist.csv",
"lausitz_mode_users.csv")
.withGroupedRefData("lausitz_mode_share_per_group_dist_ref.csv", "age", "economic_status")
.withDistanceDistribution("lausitz_mode_share_distance_distribution.csv");
.withGroupedRefData("lausitz_mode_share_per_group_dist_ref.csv", "age", "economic_status", "income")
.withDistanceDistribution("lausitz_mode_share_distance_distribution.csv")
.setAnalysisArgs("--person-filter", "subpopulation=person");

return List.of(trips,
new EmissionsDashboard()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
name = "simwrapper",
description = "Run additional analysis and create SimWrapper dashboard for existing run output."
)
final class LausitzSimWrapperRunner implements MATSimAppCommand {
public final class LausitzSimWrapperRunner implements MATSimAppCommand {

private static final Logger log = LogManager.getLogger(LausitzSimWrapperRunner.class);

Expand All @@ -56,7 +56,8 @@ final class LausitzSimWrapperRunner implements MATSimAppCommand {
private boolean noise;


private LausitzSimWrapperRunner(){
public LausitzSimWrapperRunner(){
// public constructor needed for testing purposes.
}

@Override
Expand All @@ -81,7 +82,7 @@ public Integer call() throws Exception {
simwrapperCfg.defaultDashboards = SimWrapperConfigGroup.Mode.disabled;

//add dashboards according to command line parameters
// TODO: if more dashboards are to be added here, we need to check if noise==true before adding noise dashboard here
// if more dashboards are to be added here, we need to check if noise==true before adding noise dashboard here
sw.addDashboard(Dashboard.customize(new NoiseDashboard()).context("noise"));


Expand Down
43 changes: 9 additions & 34 deletions src/main/java/org/matsim/run/LausitzScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.google.common.collect.Sets;
import org.matsim.analysis.personMoney.PersonMoneyEventsAnalysisModule;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.population.Person;
import org.matsim.application.MATSimApplication;
import org.matsim.application.analysis.CheckPopulation;
import org.matsim.application.analysis.traffic.LinkStats;
Expand All @@ -31,10 +29,11 @@
import org.matsim.core.config.groups.ScoringConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.replanning.strategies.DefaultPlanStrategiesModule;
import org.matsim.core.scoring.functions.ScoringParametersForPerson;
import org.matsim.run.analysis.CommunityFilter;
import org.matsim.run.analysis.CommuterAnalysis;
import org.matsim.run.analysis.DistanceMatrix;
import org.matsim.run.prepare.PrepareNetwork;
import org.matsim.run.prepare.PreparePopulation;
import org.matsim.simwrapper.SimWrapperConfigGroup;
Expand All @@ -48,9 +47,6 @@
import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.List;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Set;

@CommandLine.Command(header = ":: Open Lausitz Scenario ::", version = LausitzScenario.VERSION, mixinStandardHelpOptions = true)
Expand All @@ -61,7 +57,7 @@
SplitActivityTypesDuration.class, CreateCountsFromBAStData.class, PreparePopulation.class, CleanPopulation.class, PrepareNetwork.class
})
@MATSimApplication.Analysis({
LinkStats.class, CheckPopulation.class, CommuterAnalysis.class,
LinkStats.class, CheckPopulation.class, CommuterAnalysis.class, CommunityFilter.class, DistanceMatrix.class
})
public class LausitzScenario extends MATSimApplication {

Expand All @@ -79,6 +75,9 @@ public class LausitzScenario extends MATSimApplication {
private static final String HBEFA_FILE_COLD_AVERAGE = HBEFA_2020_PATH + "r9230ru2n209r30u2fn0c9rn20n2rujkhkjhoewt84202.enc" ;
private static final String HBEFA_FILE_WARM_AVERAGE = HBEFA_2020_PATH + "7eff8f308633df1b8ac4d06d05180dd0c5fdf577.enc";

@CommandLine.Option(names = "--alpha", description = "alpha for ride, this is just to get a feeling for the parameters dimension, should never be configurable in release.", defaultValue = "2.")
private double alpha;

@CommandLine.Mixin
private final SampleOptions sample = new SampleOptions( 100, 25, 10, 1);

Expand Down Expand Up @@ -135,7 +134,7 @@ protected Config prepareConfig(Config config) {
// 2.0 + 1.0 = alpha + 1
// ride cost = alpha * car cost
// ride marg utility of traveling = (alpha + 1) * marg utility travelling car + alpha * beta perf
double alpha = 2;
// double alpha = 2;
rideParams.setMarginalUtilityOfTraveling((alpha + 1) * carParams.getMarginalUtilityOfTraveling() - alpha * config.scoring().getPerforming_utils_hr());
rideParams.setDailyMonetaryConstant(0.);
rideParams.setMonetaryDistanceRate(carParams.getMonetaryDistanceRate() * 2);
Expand All @@ -155,13 +154,9 @@ protected Config prepareConfig(Config config) {
vvo20.setTransactionPartner("VVO Tarifzone 20");
vvo20.setDescription("VVO Tarifzone 20");
vvo20.setOrder(1);
try {
vvo20.setFareZoneShp(Paths.get(config.getContext().toURI()).getParent().toString() + "/vvo_tarifzone20/vvo_tarifzone20_hoyerswerda_utm32n.shp");
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
vvo20.setFareZoneShp("./vvo_tarifzone20/vvo_tarifzone20_hoyerswerda_utm32n.shp");

DistanceBasedPtFareParams germany = DistanceBasedPtFareParams.GERMAN_WIDE_FARE;
DistanceBasedPtFareParams germany = DistanceBasedPtFareParams.GERMAN_WIDE_FARE_2024;
germany.setTransactionPartner("Deutschlandtarif");
germany.setDescription("Deutschlandtarif");
germany.setOrder(2);
Expand All @@ -184,26 +179,6 @@ protected Config prepareConfig(Config config) {

@Override
protected void prepareScenario(Scenario scenario) {


for (Person person : scenario.getPopulation().getPersons().values()) {

if (PopulationUtils.getSubpopulation(person).contains("commercialPersonTraffic") ||
PopulationUtils.getSubpopulation(person).contains("goodsTraffic")) {

Map<String, Id<VehicleType>> types = VehicleUtils.getVehicleTypes(person);

for (Map.Entry<String, Id<VehicleType>> entry : types.entrySet()) {
if (Set.of(HEAVY_MODE, MEDIUM_MODE, LIGHT_MODE).contains(entry.getKey())) {
types.put(entry.getKey(), Id.create(entry.getKey(), VehicleType.class));
}
}
}

}



// add freight and truck as allowed modes together with car
PrepareNetwork.prepareFreightNetwork(scenario.getNetwork());

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/matsim/run/RunLausitzPtScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class RunLausitzPtScenario extends MATSimApplication {

private final LausitzScenario baseScenario = new LausitzScenario();

public RunLausitzPtScenario(@Nullable Config config) {
super(config);
}

public RunLausitzPtScenario() {
super(String.format("input/v%s/lausitz-v%s-10pct.config.xml", LausitzScenario.VERSION, LausitzScenario.VERSION));
}
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/org/matsim/run/analysis/CommunityFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.matsim.run.analysis;

import org.apache.commons.csv.CSVPrinter;
import org.geotools.api.feature.simple.SimpleFeature;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.matsim.application.MATSimAppCommand;
import org.matsim.application.options.CsvOptions;
import org.matsim.core.utils.collections.Tuple;
import org.matsim.core.utils.gis.GeoFileReader;
import picocli.CommandLine;

import java.nio.file.Path;
import java.util.*;

@CommandLine.Command(name = "community-filter", description = "creates a csv with commuity keys within shape")
public class CommunityFilter implements MATSimAppCommand {

@CommandLine.Option(names = "--community-shp", description = "path to VG5000_GEM.shp", required = true)
private static Path communityShapePath;

@CommandLine.Option(names = "--dilution-area", description = "path to area-of-interest-shape file", required = true)
private static Path dilutionAreaShapePath;

@CommandLine.Option(names = "--output", description = "output csv filepath", required = true)
private static Path output;

@CommandLine.Mixin
CsvOptions csvOptions = new CsvOptions();
private final Map<String, Tuple<Double, Double>> filtered = new HashMap<>();

public static void main(String[] args) {
new CommunityFilter().execute(args);
}

@Override
public Integer call() throws Exception {

Collection<SimpleFeature> communities = readShapeFile(communityShapePath);
Collection<SimpleFeature> dilutionArea = readShapeFile(dilutionAreaShapePath);

List<Geometry> geometries = dilutionArea.stream().map(feature -> (Geometry) feature.getDefaultGeometry()).toList();

for(var community: communities){

String attribute = (String) community.getAttribute("ARS");

if(geometries.stream()
.anyMatch(geometry -> geometry.covers((Geometry) community.getDefaultGeometry()))){

Point centroid = ((Geometry) community.getDefaultGeometry()).getCentroid();
Tuple<Double, Double> coordinates = Tuple.of(centroid.getX(), centroid.getY());

filtered.put(attribute, coordinates);
}

}

try (CSVPrinter printer = csvOptions.createPrinter(output)) {
printer.print("ars");
printer.print("x");
printer.print("y");
printer.println();

for (Map.Entry<String, Tuple<Double, Double>> entry : filtered.entrySet()) {
printer.print(entry.getKey());
printer.print(entry.getValue().getFirst());
printer.print(entry.getValue().getSecond());
printer.println();
}
}

return 0;
}

private static Collection<SimpleFeature> readShapeFile(Path filepath){

return GeoFileReader.getAllFeatures(filepath.toString());
}
}
Loading

0 comments on commit cf7467b

Please sign in to comment.