Skip to content

Commit

Permalink
Merge branch 'master' into 3513-freight-rename-carriercontrolerutils
Browse files Browse the repository at this point in the history
  • Loading branch information
kt86 authored Dec 2, 2024
2 parents 900ed64 + 166a09f commit 595e040
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 10 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.matsim.freight.carriers;

import org.matsim.utils.objectattributes.attributable.Attributable;

/**
* A job that a {@link Carrier} can do.
* <p>
* In a first step this is more or less a marker interface.
* <p>
* 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 {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.matsim.utils.objectattributes.attributable.Attributes;
import org.matsim.utils.objectattributes.attributable.AttributesImpl;

public final class CarrierService implements Attributable {
public final class CarrierService implements CarrierJob {

public static class Builder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @author sschroeder
*
*/
public final class CarrierShipment implements Attributable {
public final class CarrierShipment implements CarrierJob {

/**
* A builder that builds shipments.
Expand Down

0 comments on commit 595e040

Please sign in to comment.