Skip to content

Commit

Permalink
Add a new estimator approach
Browse files Browse the repository at this point in the history
  • Loading branch information
luchengqi7 committed Apr 11, 2024
1 parent 0ad7c9f commit b14a382
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.matsim.contrib.drt.estimator.impl;

import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.drt.estimator.DrtEstimator;
import org.matsim.contrib.drt.routing.DrtRoute;
import org.matsim.core.utils.geometry.CoordUtils;
import org.matsim.core.utils.misc.OptionalTime;

import java.util.Random;

public class EuclideanDistanceBasedDrtEstimator implements DrtEstimator {
private final Network network;
/**
* For travel distance related scoring (e.g., marginal utility distance), we need estimated network distance:
* Estimated network distance = Euclidean distance * network distance factor
*/
private final double networkDistanceFactor;
/**
* Slope of the linear regression
*/
private final double slope;
/**
* Intercept of the linear regression
*/
private final double intercept;

private final double estimatedMeanWaitTime;

private final double waitTimeStd;

private final double mu;
private final double sigma;
private final Random random = new Random(1234);

/**
* We use log normal distribution to estimate the ride duration of each individual trip. The distribution
* is based on the linear regression.
* @params networkDistanceFactor: Estimated network distance = Euclidean distance * network distance factor
* @params slope: slope for the linear regression
* @params intercept: intercept for linear regression
* @params mu: mu for log normal distribution
* @params sigma: sigma for log normal distribution.
*/
public EuclideanDistanceBasedDrtEstimator(Network network, double networkDistanceFactor, double slope,
double intercept, double estimatedMeanWaitTime, double waitTimeStd,
double mu, double sigma) {
this.network = network;
this.networkDistanceFactor = networkDistanceFactor;
this.slope = slope;
this.intercept = intercept;
this.estimatedMeanWaitTime = estimatedMeanWaitTime;
this.waitTimeStd = waitTimeStd;
this.mu = mu;
this.sigma = sigma;
}

@Override
public Estimate estimate(DrtRoute route, OptionalTime departureTime) {
Coord fromCoord = network.getLinks().get(route.getStartLinkId()).getToNode().getCoord();
Coord toCoord = network.getLinks().get(route.getEndLinkId()).getToNode().getCoord();
double euclideanDistance = CoordUtils.calcEuclideanDistance(fromCoord, toCoord);
double typicalRideDuration = euclideanDistance * slope + intercept;
double typicalRideDistance = networkDistanceFactor * euclideanDistance;
double randomFactor = nextLogNormal(mu, sigma);
double waitTime = Math.max(estimatedMeanWaitTime * (1 + random.nextGaussian() * waitTimeStd), 0);

return new Estimate(typicalRideDistance * randomFactor, typicalRideDuration * randomFactor,
waitTime, 0);
}

public double nextLogNormal(double mu, double sigma) {
if (sigma == 0)
return Math.exp(mu);

return Math.exp(sigma * random.nextGaussian() + mu);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.drt.estimator.DrtEstimator;
import org.matsim.contrib.drt.estimator.impl.DetourBasedDrtEstimator;
import org.matsim.contrib.drt.estimator.impl.EuclideanDistanceBasedDrtEstimator;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.run.DrtControlerCreator;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
Expand Down Expand Up @@ -37,7 +39,7 @@ void testModeChoice() {
config.network().setInputFile("network.xml");
config.plans().setInputFile("plans_only_drt_4.0.xml.gz");
config.controller().setOutputDirectory(utils.getOutputDirectory());
config.controller().setLastIteration(100);
config.controller().setLastIteration(3);

config.replanning().setFractionOfIterationsToDisableInnovation(0.8);
config.replanning().setMaxAgentPlanMemorySize(3);
Expand All @@ -58,7 +60,7 @@ void testModeChoice() {
bikeModeParams.setMarginalUtilityOfTraveling(-6.);
config.scoring().addModeParams(bikeModeParams);
// Update change mode
config.changeMode().setModes( new String[] { TransportMode.drt, TransportMode.bike });
config.changeMode().setModes(new String[]{TransportMode.drt, TransportMode.bike});

// Setting DRT config group
DrtConfigGroup drtConfigGroup = DrtConfigGroup.getSingleModeDrtConfig(config);
Expand All @@ -70,8 +72,11 @@ void testModeChoice() {
controler.addOverridingModule(new AbstractDvrpModeModule(drtConfigGroup.mode) {
@Override
public void install() {
bindModal(DrtEstimator.class).toInstance(DetourBasedDrtEstimator.normalDistributed(1.2, 32,
0.3, 300, 0.4));
// bindModal(DrtEstimator.class).toInstance(DetourBasedDrtEstimator.normalDistributed(1.2, 32,
// 0.3, 300, 0.4));
bindModal(DrtEstimator.class).toProvider(modalProvider(getter -> new
EuclideanDistanceBasedDrtEstimator(getter.getModal(Network.class), 2.0, 0.1577493,
103.0972273, 120, 0.3, -0.1, 0.28)));
}
});

Expand Down

0 comments on commit b14a382

Please sign in to comment.