Skip to content

Commit

Permalink
Merge remote-tracking branch 'sasha/master' into eventfp
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Mar 18, 2024
2 parents 6ef8a30 + 3fa4e38 commit 93e83b1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Person;
import org.matsim.contrib.dynagent.DynAgent;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.controler.MatsimServices;
Expand Down Expand Up @@ -94,22 +95,25 @@ public boolean handleDeparture(double now, MobsimAgent agent, Id<Link> linkId) {
Leg currentLeg = (Leg) planAgent.getCurrentPlanElement();
Gbl.assertIf(this.fissConfigGroup.sampledModes.contains(currentLeg.getMode()));
NetworkRoute networkRoute = (NetworkRoute) currentLeg.getRoute();
Person person = planAgent.getCurrentPlan().getPerson();
Vehicle vehicle = this.matsimServices.getScenario().getVehicles().getVehicles()
.get(networkRoute.getVehicleId());

// update travel time with travel times of last iteration
double newTravelTime = 0.0;
// start and end link are not consideres in NetworkRoutingModule for travel time
// start and end link are not considered in NetworkRoutingModule for travel time
for (Id<Link> routeLinkId : networkRoute.getLinkIds()) {
newTravelTime += this.travelTime.getLinkTravelTime(network.getLinks().get(routeLinkId),
now + newTravelTime, null, null);
now + newTravelTime, person, vehicle);
}
LOG.debug("New travelTime: {}, was {}", newTravelTime,
networkRoute.getTravelTime().orElseGet(() -> Double.NaN));
networkRoute.setTravelTime(newTravelTime);
}
// remove vehicle of teleported agent from parking spot
QVehicle removedVehicle = null;
if (agent instanceof MobsimDriverAgent) {
Id<Vehicle> vehicleId = ((MobsimDriverAgent) agent).getPlannedVehicleId();
if (agent instanceof MobsimDriverAgent driverAgent) {
Id<Vehicle> vehicleId = driverAgent.getPlannedVehicleId();
QVehicle vehicle = qNetsimEngine.getVehicles().get(vehicleId);
QLinkI qLinkI = (QLinkI) this.qNetsimEngine.getNetsimNetwork().getNetsimLink(linkId);
removedVehicle = qLinkI.removeParkedVehicle(vehicleId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class RaptorParameters {
private double transferPenaltyPerTravelTimeHour = 0.0;
private double transferPenaltyMinimum = Double.NEGATIVE_INFINITY;
private double transferPenaltyMaximum = Double.POSITIVE_INFINITY;

private boolean useTransportModeUtilities = false;

private final SwissRailRaptorConfigGroup config;

Expand Down Expand Up @@ -159,5 +161,13 @@ public double getTransferPenaltyMaximum() {
public void setTransferPenaltyMaximum(double transferPenaltyMaximum) {
this.transferPenaltyMaximum = transferPenaltyMaximum;
}

public boolean isUseTransportModeUtilities() {
return useTransportModeUtilities;
}

public void setUseTransportModeUtilities(boolean useTransportModeUtilities) {
this.useTransportModeUtilities = useTransportModeUtilities;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ private void exploreRoutes(RaptorParameters parameters, Person person) {
CachingTransferProvider transferProvider = this.data.new CachingTransferProvider();

double marginalUtilityOfWaitingPt_utl_s = parameters.getMarginalUtilityOfWaitingPt_utl_s();
boolean useTransportModeUtilities = parameters.isUseTransportModeUtilities();

int routeIndex = -1;
for (int firstRouteStopIndex = this.improvedRouteStopIndices.nextSetBit(0); firstRouteStopIndex >= 0; firstRouteStopIndex = this.improvedRouteStopIndices.nextSetBit(firstRouteStopIndex+1)) {
Expand Down Expand Up @@ -627,7 +628,8 @@ private void exploreRoutes(RaptorParameters parameters, Person person) {
routeIndex = tmpRouteIndex;
int firstDepartureTime = (boardingPE.firstDepartureTime == TIME_UNDEFINED) ? currentAgentBoardingTime : boardingPE.firstDepartureTime;

double marginalUtilityOfTravelTime_utl_s = parameters.getMarginalUtilityOfTravelTime_utl_s(boardingPE.toRouteStop.mode);
double marginalUtilityOfTravelTime_utl_s = parameters.getMarginalUtilityOfTravelTime_utl_s(
!useTransportModeUtilities ? boardingPE.toRouteStop.mode : boardingPE.toRouteStop.route.getTransportMode());
transferProvider.reset(boardingPE.transfer);

for (int toRouteStopIndex = firstRouteStopIndex + 1; toRouteStopIndex < route.indexFirstRouteStop + route.countRouteStops; toRouteStopIndex++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@
*/
public class StringConverter implements AttributeConverter<String> {
private final Map<String, String> stringCache = new ConcurrentHashMap<>(1000);

@Override
public String convert(String value) {
String s = this.stringCache.get(value);
if (s == null) {
s = value;
this.stringCache.put(s, s);
}
return s;
return stringCache.computeIfAbsent(value, k -> k);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.matsim.api.core.v01.network.Node;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
Expand Down Expand Up @@ -337,7 +338,7 @@ void testLineChange() {
assertEquals(Math.ceil(expectedTravelTime), actualTravelTime, MatsimTestUtils.EPSILON);
}
@Test
void testLineChangeWithDifferentUtils() {
void testLineChangeWithDifferentModeChangeUtils() {
Fixture f = new Fixture();
f.init();
f.blueLine.getRoutes().values().forEach(transitRoute -> transitRoute.setTransportMode(TransportMode.ship));
Expand All @@ -355,6 +356,48 @@ void testLineChangeWithDifferentUtils() {
//changing from train to ship is so expensive that direct walk is cheaper
assertNull(legs);
}

@Test
void testLineChangeWithDifferentTravelTimeUtils() {
Fixture f = new Fixture();
f.init();
f.blueLine.getRoutes().values().forEach(transitRoute -> transitRoute.setTransportMode("bus"));
SwissRailRaptorConfigGroup swissRailRaptorConfigGroup = ConfigUtils.addOrGetModule(f.config, SwissRailRaptorConfigGroup.class);
swissRailRaptorConfigGroup.setTransferWalkMargin(0);
RaptorParameters raptorParams = RaptorUtils.createParameters(f.config);
SwissRailRaptorData data = SwissRailRaptorData.create(f.schedule, null, RaptorUtils.createStaticConfig(f.config), f.network, null);
TransitRouter router = new SwissRailRaptor.Builder(data, f.config).with(new RaptorParametersForPerson() {
@Override
public RaptorParameters getRaptorParameters(Person person) {
return raptorParams;
}
}).build();

// from C to G (see Fixture), competing between red line (express) and blue line (regular)
Coord fromCoord = new Coord(12000, 5000);
Coord toCoord = new Coord(28000, 5000);

// default case
List<? extends PlanElement> legs = router.calcRoute(DefaultRoutingRequest.withoutAttributes(new FakeFacility(fromCoord), new FakeFacility(toCoord), 6.0*3600 - 60.0, null));
assertEquals(3, legs.size());
assertEquals("red", ((TransitPassengerRoute) ((Leg) legs.get(1)).getRoute()).getLineId().toString());

// routing by transport mode, same costs, choose red (train) again
raptorParams.setUseTransportModeUtilities(true);
raptorParams.setMarginalUtilityOfTravelTime_utl_s("train", -1e-3);
raptorParams.setMarginalUtilityOfTravelTime_utl_s("bus", -1e-3);
legs = router.calcRoute(DefaultRoutingRequest.withoutAttributes(new FakeFacility(fromCoord), new FakeFacility(toCoord), 6.0*3600 - 60.0, null));
assertEquals(3, legs.size());
assertEquals("red", ((TransitPassengerRoute) ((Leg) legs.get(1)).getRoute()).getLineId().toString());

// routing by transport mode, train is quicker, but more costly now, so choose blue (bus)
raptorParams.setUseTransportModeUtilities(true);
raptorParams.setMarginalUtilityOfTravelTime_utl_s("train", -1e-2);
raptorParams.setMarginalUtilityOfTravelTime_utl_s("bus", -1e-3);
legs = router.calcRoute(DefaultRoutingRequest.withoutAttributes(new FakeFacility(fromCoord), new FakeFacility(toCoord), 6.0*3600 - 60.0, null));
assertEquals(3, legs.size());
assertEquals("blue", ((TransitPassengerRoute) ((Leg) legs.get(1)).getRoute()).getLineId().toString());
}

@Test
void testFasterAlternative() {
Expand Down

0 comments on commit 93e83b1

Please sign in to comment.