Skip to content

Commit

Permalink
Allow absolute bounds in freespeed calibration (#3615)
Browse files Browse the repository at this point in the history
* support negative freespeed bounds

* improve description

* improve description

* fix sign of absolute bounds
  • Loading branch information
rakow authored Dec 2, 2024
1 parent adadbe4 commit b5c1e5b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public class ApplyNetworkParams implements MATSimAppCommand {
@CommandLine.Option(names = "--model", description = "Reference to the network model class", required = true)
private Class<? extends NetworkModel> 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",
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public class FreespeedOptServer implements MATSimAppCommand {
@CommandLine.Parameters(arity = "1..*", description = "Input validation files loaded from APIs")
private List<String> 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 = ",")
Expand Down

0 comments on commit b5c1e5b

Please sign in to comment.