From 05146bca0174e6ff717a5fda95e385f6480e1abc Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Wed, 27 Nov 2024 13:13:22 +0100 Subject: [PATCH 1/2] Add a CarrierJob interface and put CarrierService and CarrierShipment behind it. --- .../matsim/freight/carriers/CarrierJob.java | 19 +++++++++++++++++++ .../freight/carriers/CarrierService.java | 2 +- .../freight/carriers/CarrierShipment.java | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierJob.java diff --git a/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierJob.java b/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierJob.java new file mode 100644 index 00000000000..3654fbff36a --- /dev/null +++ b/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierJob.java @@ -0,0 +1,19 @@ +package org.matsim.freight.carriers; + +import org.matsim.utils.objectattributes.attributable.Attributable; + +/** + * A job that a {@link Carrier} can do. + *

+ * In a first step this is more or less a marker interface. + *

+ * In the next steps it will be extended, as follows + * 1) existing common methods of {@link CarrierShipment} and {@link + * CarrierService} where moved up here + * 2) some similiar, but differently named methods of {@link + * CarrierShipment} and {@link CarrierService} were renamed to the same name and moved up here + * ... + * future) It maybe gets generalized in way, that we only have one job definition with 1 or 2 + * location(s). This then defines, if jsprit takes the job as a service or as a shipment. + */ +public interface CarrierJob extends Attributable {} diff --git a/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierService.java b/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierService.java index b7106fa6de9..7500b10887d 100644 --- a/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierService.java +++ b/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierService.java @@ -28,7 +28,7 @@ import org.matsim.utils.objectattributes.attributable.AttributesImpl; -public final class CarrierService implements Attributable { +public final class CarrierService implements CarrierJob { public static class Builder { diff --git a/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierShipment.java b/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierShipment.java index 9472ffaac91..489657ba54e 100644 --- a/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierShipment.java +++ b/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierShipment.java @@ -36,7 +36,7 @@ * @author sschroeder * */ -public final class CarrierShipment implements Attributable { +public final class CarrierShipment implements CarrierJob { /** * A builder that builds shipments. From b5c1e5b4fc9b05cb149e2746caa47acb2b629cec Mon Sep 17 00:00:00 2001 From: rakow Date: Mon, 2 Dec 2024 10:51:17 +0100 Subject: [PATCH 2/2] Allow absolute bounds in freespeed calibration (#3615) * support negative freespeed bounds * improve description * improve description * fix sign of absolute bounds --- .../network/params/ApplyNetworkParams.java | 26 ++++++++++++++++--- .../network/params/EvalFreespeedParams.java | 21 ++++++++++++--- .../network/params/FreespeedOptServer.java | 3 ++- 3 files changed, 42 insertions(+), 8 deletions(-) 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 120f9711ba6..8d8e341154d 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 @@ -53,7 +53,8 @@ public class ApplyNetworkParams implements MATSimAppCommand { @CommandLine.Option(names = "--model", description = "Reference to the network model class", required = true) private Class modelClazz; - @CommandLine.Option(names = "--factor-bounds", split = ",", description = "Speed factor limits (lower,upper bound)", defaultValue = NetworkParamsOpt.DEFAULT_FACTOR_BOUNDS) + @CommandLine.Option(names = "--factor-bounds", split = ",", description = "Speed factor limits (lower, upper bound). " + + "Can be negative to indicate absolute speed bounds (in km/h)", defaultValue = NetworkParamsOpt.DEFAULT_FACTOR_BOUNDS) private double[] speedFactorBounds; @CommandLine.Option(names = "--capacity-bounds", split = ",", defaultValue = "0.4,0.6,0.8", @@ -234,23 +235,40 @@ private boolean applyFreeSpeed(Link link, Feature ft) { return false; } + double allowedSpeed = (double) link.getAttributes().getAttribute("allowed_speed"); + double freeSpeed = allowedSpeed * speedFactor; + boolean modified = false; - if (speedFactor > speedFactorBounds[1]) { + if (speedFactor > speedFactorBounds[1] && speedFactorBounds[1] >= 0) { log.warn("Reducing speed factor on {} from {} to {}", link.getId(), speedFactor, speedFactorBounds[1]); speedFactor = speedFactorBounds[1]; modified = true; } + // Use absolute bound for speed + if (freeSpeed > -speedFactorBounds[1]/3.6 && speedFactorBounds[1] < 0) { + log.warn("Reducing speed on {} from {} to {}", link.getId(), freeSpeed, -speedFactorBounds[1]/3.6); + speedFactor = (-speedFactorBounds[1] / 3.6) / allowedSpeed; + modified = true; + } + // Threshold for very low speed factors - if (speedFactor < speedFactorBounds[0]) { + if (speedFactor < speedFactorBounds[0] && speedFactorBounds[0] >= 0) { log.warn("Increasing speed factor on {} from {} to {}", link, speedFactor, speedFactorBounds[0]); speedFactor = speedFactorBounds[0]; modified = true; } - double freeSpeed = (double) link.getAttributes().getAttribute("allowed_speed") * speedFactor; + // Absolute negative speed factor + if (freeSpeed < -speedFactorBounds[0]/3.6 && speedFactorBounds[0] < 0) { + log.warn("Increasing speed on {} from {} to {}", link, freeSpeed, -speedFactorBounds[0]/3.6); + speedFactor = (-speedFactorBounds[0] / 3.6) / allowedSpeed; + modified = true; + } + // Recalculate with updated speed factor + freeSpeed = allowedSpeed * speedFactor; freeSpeed = BigDecimal.valueOf(freeSpeed).setScale(3, RoundingMode.HALF_EVEN).doubleValue(); if (decrease && freeSpeed > link.getFreespeed()) 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 cd2ba4e016a..68cd6099058 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 @@ -121,12 +121,27 @@ static Result applyAndEvaluateParams( speedFactor = speedModel.predict(ft.features(), ft.categories()); // apply lower and upper bound - speedFactor = Math.max(speedFactorBounds[0], speedFactor); - speedFactor = Math.min(speedFactorBounds[1], speedFactor); + if (speedFactorBounds[0] >= 0) + speedFactor = Math.max(speedFactorBounds[0], speedFactor); + + if (speedFactorBounds[1] >= 0) + speedFactor = Math.min(speedFactorBounds[1], speedFactor); attributes.put(link.getId(), speedModel.getData(ft.features(), ft.categories())); - link.setFreespeed(allowedSpeed * speedFactor); + double freespeed = allowedSpeed * speedFactor; + + // Check absolute bounds on the freespeed + if (speedFactorBounds[0] < 0 && freespeed < -speedFactorBounds[0]/3.6) { + freespeed = -speedFactorBounds[0]/3.6; + speedFactor = freespeed / allowedSpeed; + } + if (speedFactorBounds[1] < 0 && freespeed > -speedFactorBounds[1]/3.6) { + freespeed = -speedFactorBounds[1]/3.6; + speedFactor = freespeed / allowedSpeed; + } + + link.setFreespeed(freespeed); link.getAttributes().putAttribute("speed_factor", speedFactor); } else diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/FreespeedOptServer.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/FreespeedOptServer.java index fef19850305..c17189b3a95 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/params/FreespeedOptServer.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/params/FreespeedOptServer.java @@ -57,7 +57,8 @@ public class FreespeedOptServer implements MATSimAppCommand { @CommandLine.Parameters(arity = "1..*", description = "Input validation files loaded from APIs") private List validationFiles; - @CommandLine.Option(names = "--factor-bounds", split = ",", description = "Speed factor limits (lower,upper bound)", defaultValue = NetworkParamsOpt.DEFAULT_FACTOR_BOUNDS) + @CommandLine.Option(names = "--factor-bounds", split = ",", description = "Speed factor limits (lower, upper bound). " + + "Can be negative to indicate absolute speed bounds (in km/h)", defaultValue = NetworkParamsOpt.DEFAULT_FACTOR_BOUNDS) private double[] speedFactorBounds; @CommandLine.Option(names = "--ref-hours", description = "Reference hours", defaultValue = "3,21", split = ",")