From 1fb5a37de322710aff0070d46903d7d987e281e1 Mon Sep 17 00:00:00 2001 From: steffenaxer <26229392+steffenaxer@users.noreply.github.com> Date: Sat, 20 Jan 2024 12:06:09 +0100 Subject: [PATCH] validateGroups --- .../companions/DrtCompanionRideGenerator.java | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/contribs/drt-extensions/src/main/java/org/matsim/contrib/drt/extension/companions/DrtCompanionRideGenerator.java b/contribs/drt-extensions/src/main/java/org/matsim/contrib/drt/extension/companions/DrtCompanionRideGenerator.java index e7bfc82fa0a..d7fa06f2693 100644 --- a/contribs/drt-extensions/src/main/java/org/matsim/contrib/drt/extension/companions/DrtCompanionRideGenerator.java +++ b/contribs/drt-extensions/src/main/java/org/matsim/contrib/drt/extension/companions/DrtCompanionRideGenerator.java @@ -19,17 +19,15 @@ package org.matsim.contrib.drt.extension.companions; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; +import com.google.common.base.Preconditions; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.matsim.api.core.v01.Id; +import org.matsim.api.core.v01.Identifiable; import org.matsim.api.core.v01.Scenario; +import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.population.Activity; import org.matsim.api.core.v01.population.Person; import org.matsim.api.core.v01.population.Plan; @@ -42,6 +40,7 @@ import org.matsim.core.controler.events.BeforeMobsimEvent; import org.matsim.core.controler.listener.AfterMobsimListener; import org.matsim.core.controler.listener.BeforeMobsimListener; +import org.matsim.core.mobsim.framework.MobsimPassengerAgent; import org.matsim.core.population.PopulationUtils; import org.matsim.core.router.MainModeIdentifier; import org.matsim.core.router.TripStructureUtils; @@ -64,6 +63,8 @@ final class DrtCompanionRideGenerator implements BeforeMobsimListener, AfterMobs private final Set> companionAgentIds = new HashSet<>(); private WeightedRandomSelection sampler; + private final Map,List> passengerGroups = new HashMap<>(); + private int passengerGroupIdentifier = 0; // Should be unique over the entire simulation DrtCompanionRideGenerator(final String drtMode, final MainModeIdentifier mainModeIdentifier, @@ -103,6 +104,8 @@ private Id getGroupIdentifier() return Id.create(this.passengerGroupIdentifier, PassengerGroupIdentifier.PassengerGroup.class); } + record GroupTrip(TripStructureUtils.Trip trip, Id personId) {} + private void addCompanionAgents() { int personIdentifierSuffix = 0; int drtCompanionAgents = 0; @@ -114,7 +117,11 @@ private void addCompanionAgents() { int additionalCompanions = sampler.select(); // Initial person travels now in a group - DrtCompanionUtils.setPassengerGroupIdentifier(person, getGroupIdentifier()); + Id currentGroupIdentifier = getGroupIdentifier(); + DrtCompanionUtils.setPassengerGroupIdentifier(person, currentGroupIdentifier); + + // Add person to group map + this.passengerGroups.computeIfAbsent(currentGroupIdentifier, k-> new ArrayList<>()).add(new GroupTrip(trip,person.getId())); int groupSize = additionalCompanions + 1; int currentGroupSize = 1; // Initial person @@ -128,11 +135,12 @@ private void addCompanionAgents() { { passengerGroupIdentifier++; currentGroupSize=0; + currentGroupIdentifier = getGroupIdentifier(); } // Bypass passengerGroupIdentifierId to each group member companions.add(createCompanionAgent(mainMode, person, trip, trip.getOriginActivity(), - trip.getDestinationActivity(), groupPart, groupSize, personIdentifierSuffix, getGroupIdentifier())); + trip.getDestinationActivity(), groupPart, groupSize, personIdentifierSuffix, currentGroupIdentifier)); personIdentifierSuffix++; drtCompanionAgents++; currentGroupSize++; @@ -146,13 +154,23 @@ private void addCompanionAgents() { this.companionAgentIds.add(p.getId()); }); - + validateGroups(); LOG.info("Added # {} drt companion agents for mode {}", drtCompanionAgents, this.drtMode); + } + private void validateGroups() { + this.passengerGroups.values().forEach(g -> { + GroupTrip representative = g.get(0); + Id fromLinkId = representative.trip.getOriginActivity().getLinkId(); + Id toLinkId = representative.trip.getDestinationActivity().getLinkId(); + Preconditions.checkArgument(g.stream().allMatch(a -> a.trip.getOriginActivity().getLinkId().equals(fromLinkId))); + Preconditions.checkArgument(g.stream().allMatch(a -> a.trip.getDestinationActivity().getLinkId().equals(toLinkId))); + Preconditions.checkArgument(g.size() <= this.maxCapacity); + }); } - private Person createCompanionAgent(String drtMode, Person originalPerson, TripStructureUtils.Trip trip, - Activity fromActivity, Activity toActivity, int groupPart, int groupSize, int personIdentifier, Id passengerGroupIdentifierId) { + private Person createCompanionAgent(final String drtMode, final Person originalPerson, final TripStructureUtils.Trip trip, + final Activity fromActivity, final Activity toActivity, final int groupPart, final int groupSize, final int personIdentifier, final Id passengerGroupIdentifierId) { String prefix = getCompanionPrefix(drtMode); String companionId = prefix + "_" + originalPerson.getId().toString() + "_" + personIdentifier; Person person = PopulationUtils.getFactory().createPerson(Id.createPersonId(companionId)); @@ -179,6 +197,8 @@ private Person createCompanionAgent(String drtMode, Person originalPerson, TripS DrtCompanionUtils.setAdditionalGroupSize(person, groupSize); DrtCompanionUtils.setPassengerGroupIdentifier(person, passengerGroupIdentifierId); + // Add companion to group map + this.passengerGroups.computeIfAbsent(passengerGroupIdentifierId, k-> new ArrayList<>()).add(new GroupTrip(trip,person.getId())); return person; } @@ -190,6 +210,7 @@ private void removeCompanionAgents() { counter++; } companionAgentIds.clear(); + passengerGroups.clear(); LOG.info("Removed # {} drt companion agents", counter); }