Skip to content

Commit

Permalink
more flexible reference models
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed May 8, 2024
1 parent bca2bff commit fa11208
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<NetworkAttribute> params;
Expand All @@ -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<String> roadTypes;

@CommandLine.Option(names = "--junction-types", split = ",", description = "Junction types to apply changes to")
private Set<String> junctionTypes;

private NetworkModel model;
private NetworkParams paramsOpt;

Expand Down Expand Up @@ -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) {
Expand All @@ -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());
}
Expand All @@ -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;
}

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
class HBSRoadCapacity implements FeatureRegressor {
@Override
public double predict(Object2DoubleMap<String> ft) {

return 0;

}
}
Original file line number Diff line number Diff line change
@@ -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<String> ft) {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit fa11208

Please sign in to comment.