From fa112081f0182a5c166d2e2a13a37d1bef7ddb46 Mon Sep 17 00:00:00 2001 From: rakow Date: Wed, 8 May 2024 20:27:52 +0200 Subject: [PATCH] more flexible reference models --- .../network/params/ApplyNetworkParams.java | 46 +++++++++++-------- .../network/params/EvalFreespeedParams.java | 2 +- .../prepare/network/params/NetworkModel.java | 4 +- .../network/params/hbs/HBSNetworkParams.java | 20 +++++--- .../network/params/hbs/HBSRoadCapacity.java | 2 + .../params/hbs/HSBMotorwayCapacity.java | 14 ++++++ .../params/ref/DecisionTreeParams.java | 2 +- .../params/ref/GermanyNetworkParams.java | 4 +- 8 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HSBMotorwayCapacity.java diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ApplyNetworkParams.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ApplyNetworkParams.java index 1cbfb9d24fe..50f6ed83912 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ApplyNetworkParams.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ApplyNetworkParams.java @@ -40,10 +40,6 @@ public class ApplyNetworkParams implements MATSimAppCommand { @CommandLine.Mixin private final OutputOptions output = OutputOptions.ofCommand(ApplyNetworkParams.class); - // TODO: - // Filter road types, filter junction types - // adjustment factors - // TODO: capacity threshold handling @CommandLine.Parameters(arity = "1..*", description = "Type of parameters to apply. Available: ${COMPLETION-CANDIDATES}") private Set params; @@ -57,6 +53,15 @@ public class ApplyNetworkParams implements MATSimAppCommand { @CommandLine.Option(names = "--factor-bounds", split = ",", description = "Speed factor limits (lower,upper bound)", defaultValue = NetworkParamsOpt.DEFAULT_FACTOR_BOUNDS) private double[] speedFactorBounds; + @CommandLine.Option(names = "--capacity-bounds", split = ",", description = "Relative capacity bounds against theoretical max (lower,upper bound)", defaultValue = "0.3,1.0") + private double[] capacityBounds; + + @CommandLine.Option(names = "--road-types", split = ",", description = "Road types to apply changes to") + private Set roadTypes; + + @CommandLine.Option(names = "--junction-types", split = ",", description = "Junction types to apply changes to") + private Set junctionTypes; + private NetworkModel model; private NetworkParams paramsOpt; @@ -104,6 +109,12 @@ public Integer call() throws Exception { for (Link link : network.getLinks().values()) { Feature ft = features.get(link.getId()); + if (roadTypes != null && !roadTypes.isEmpty() && !roadTypes.contains(ft.highwayType())) + continue; + + if (junctionTypes != null && !junctionTypes.isEmpty() && !junctionTypes.contains(ft.junctionType())) + continue; + try { applyChanges(link, ft); } catch (UnsupportedOperationException u) { @@ -130,7 +141,7 @@ private void applyChanges(Link link, Feature ft) { if (params.contains(NetworkAttribute.capacity)) { - FeatureRegressor capacity = model.capacity(ft.junctionType()); + FeatureRegressor capacity = model.capacity(ft.junctionType(), ft.highwayType()); if (capacity == null) { throw new UnsupportedOperationException("Capacity model not available for " + ft.junctionType()); } @@ -139,20 +150,17 @@ private void applyChanges(Link link, Feature ft) { double cap = capacityEstimate(ft.features().getDouble("speed")); - // Minimum thresholds - double threshold = switch (ft.junctionType()) { - // traffic light can reduce capacity at least to 50% (with equal green split) - case "traffic_light" -> 0.4; - case "right_before_left" -> 0.6; - // Motorways are kept at their max theoretical capacity - case "priority" -> ft.highwayType().startsWith("motorway") ? 1 : 0.8; - default -> 0; - }; - - if (perLane < cap * threshold) { + if (perLane < cap * speedFactorBounds[0]) { log.warn("Increasing capacity per lane on {} ({}, {}) from {} to {}", - link.getId(), ft.highwayType(), ft.junctionType(), perLane, cap * threshold); - perLane = cap * threshold; + link.getId(), ft.highwayType(), ft.junctionType(), perLane, cap * speedFactorBounds[0]); + perLane = cap * speedFactorBounds[0]; + modified = true; + } + + if (perLane > cap * speedFactorBounds[1]) { + log.warn("Reducing capacity per lane on {} ({}, {}) from {} to {}", + link.getId(), ft.highwayType(), ft.junctionType(), perLane, cap * speedFactorBounds[1]); + perLane = cap * speedFactorBounds[1]; modified = true; } @@ -163,7 +171,7 @@ private void applyChanges(Link link, Feature ft) { if (params.contains(NetworkAttribute.freespeed)) { double speedFactor = 1.0; - FeatureRegressor speedModel = model.speedFactor(ft.junctionType()); + FeatureRegressor speedModel = model.speedFactor(ft.junctionType(), ft.highwayType()); if (speedModel == null) { throw new UnsupportedOperationException("Speed model not available for " + ft.junctionType()); diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/EvalFreespeedParams.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/EvalFreespeedParams.java index e6f8d8e8393..8b90cc1a83e 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/EvalFreespeedParams.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/EvalFreespeedParams.java @@ -104,7 +104,7 @@ static Result applyAndEvaluateParams( continue; } - FeatureRegressor speedModel = model.speedFactor(ft.junctionType()); + FeatureRegressor speedModel = model.speedFactor(ft.junctionType(), ft.highwayType()); if (speedModel == null) { link.setFreespeed(allowedSpeed); diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/NetworkModel.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/NetworkModel.java index f98bb9bd41e..915735d61cd 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/NetworkModel.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/NetworkModel.java @@ -8,14 +8,14 @@ public interface NetworkModel { /** * Flow Capacity (per lane) */ - default FeatureRegressor capacity(String junctionType) { + default FeatureRegressor capacity(String junctionType, String highwayType) { return null; } /** * Speed factor (relative to free flow speed). */ - default FeatureRegressor speedFactor(String junctionType) { + default FeatureRegressor speedFactor(String junctionType, String highwayType) { return null; } diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HBSNetworkParams.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HBSNetworkParams.java index b83282fab31..7fe57a8450c 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HBSNetworkParams.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HBSNetworkParams.java @@ -9,14 +9,20 @@ */ public class HBSNetworkParams implements NetworkModel { - @Override - public FeatureRegressor capacity(String junctionType) { - // TODO: should depend on junction type and or road type - return new HBSRoadCapacity(); - } + private static final FeatureRegressor MOTORWAY = new HSBMotorwayCapacity(); + private static final FeatureRegressor ROAD = new HBSRoadCapacity(); @Override - public FeatureRegressor speedFactor(String junctionType) { - throw new UnsupportedOperationException("Not implemented"); + public FeatureRegressor capacity(String junctionType, String highwayType) { + + if (highwayType.startsWith("motorway")) { + return MOTORWAY; + } else if (junctionType.startsWith("priority")) { + // All other roads + return ROAD; + } + + throw new UnsupportedOperationException("Unknown type: " + junctionType); } + } diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HBSRoadCapacity.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HBSRoadCapacity.java index 3cbc16f005f..1ef2fcdd2c7 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HBSRoadCapacity.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HBSRoadCapacity.java @@ -9,6 +9,8 @@ class HBSRoadCapacity implements FeatureRegressor { @Override public double predict(Object2DoubleMap ft) { + return 0; + } } diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HSBMotorwayCapacity.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HSBMotorwayCapacity.java new file mode 100644 index 00000000000..a7faca2319e --- /dev/null +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/hbs/HSBMotorwayCapacity.java @@ -0,0 +1,14 @@ +package org.matsim.application.prepare.network.params.hbs; + +import it.unimi.dsi.fastutil.objects.Object2DoubleMap; +import org.matsim.application.prepare.network.params.FeatureRegressor; + +/** + * Capacity for motorways. + */ +public class HSBMotorwayCapacity implements FeatureRegressor { + @Override + public double predict(Object2DoubleMap ft) { + return 0; + } +} diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ref/DecisionTreeParams.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ref/DecisionTreeParams.java index 4bb84fa6577..7dab8d49135 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ref/DecisionTreeParams.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ref/DecisionTreeParams.java @@ -18,7 +18,7 @@ public final class DecisionTreeParams implements NetworkModel { private static final FeatureRegressor INSTANCE = new Model(); @Override - public FeatureRegressor speedFactor(String junctionType) { + public FeatureRegressor speedFactor(String junctionType, String highwayType) { return INSTANCE; } diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ref/GermanyNetworkParams.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ref/GermanyNetworkParams.java index ffe7250b2f4..80541a82a7e 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ref/GermanyNetworkParams.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/ref/GermanyNetworkParams.java @@ -9,7 +9,7 @@ */ public final class GermanyNetworkParams implements NetworkModel { @Override - public FeatureRegressor capacity(String junctionType) { + public FeatureRegressor capacity(String junctionType, String highwayType) { return switch (junctionType) { case "traffic_light" -> GermanyNetworkParams_capacity_traffic_light.INSTANCE; case "right_before_left" -> GermanyNetworkParams_capacity_right_before_left.INSTANCE; @@ -19,7 +19,7 @@ public FeatureRegressor capacity(String junctionType) { } @Override - public FeatureRegressor speedFactor(String junctionType) { + public FeatureRegressor speedFactor(String junctionType, String highwayType) { return switch (junctionType) { case "traffic_light" -> GermanyNetworkParams_speedRelative_traffic_light.INSTANCE; case "right_before_left" -> GermanyNetworkParams_speedRelative_right_before_left.INSTANCE;