MATSIM
Classes | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour Class Reference
Inheritance diagram for org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour:
Inheritance graph
[legend]

Classes

class  Candidate
 

Public Member Functions

 ChooseRandomLegModeForSubtour (final MainModeIdentifier mainModeIdentifier, final PermissibleModesCalculator permissibleModesCalculator, final String[] modes, final String[] chainBasedModes, final Random rng, SubtourModeChoice.Behavior behavior, double probaForChooseRandomSingleTripMode)
 
 ChooseRandomLegModeForSubtour (final MainModeIdentifier mainModeIdentifier, final PermissibleModesCalculator permissibleModesCalculator, final String[] modes, final String[] chainBasedModes, final Random rng, SubtourModeChoice.Behavior behavior, double probaForChooseRandomSingleTripMode, double coordDist)
 
void setSingleTripSubtourModes (final String[] singleTripSubtourModes)
 
List< CandidatedetermineChoiceSet (final Plan plan)
 
boolean isMassConserving (final Subtour subtour)
 
void run (final Plan plan)
 

Private Member Functions

boolean hasSingleTripChoice (List< Trip > trips)
 
List< CandidatedetermineChoiceSet (final Id<? extends BasicLocation > homeLocation, final Plan plan, final List< Trip > trips, final Collection< String > permissibleModesForThisPerson)
 
boolean containsUnknownMode (final Subtour subtour)
 
boolean isMassConserving (final Subtour subtour, final String mode)
 
Id<? extends BasicLocationgetLocationId (Activity activity)
 
boolean atSameLocation (Activity firstLegUsingMode, Activity lastLegUsingMode)
 
Activity findLastDestinationOfMode (final List< Trip > tripsToSearch, final String mode)
 
Activity findFirstOriginOfMode (final List< Trip > tripsToSearch, final String mode)
 
String getTransportMode (final Subtour subtour)
 
void applyChange (final Candidate whatToDo, final Plan plan)
 

Private Attributes

Collection< String > modes
 
final Collection< String > chainBasedModes
 
final SubtourModeChoice.Behavior behavior
 
Collection< String > singleTripSubtourModes
 
final MainModeIdentifier mainModeIdentifier
 
final Random rng
 
PermissibleModesCalculator permissibleModesCalculator
 
final double probaForChangeSingleTripMode
 
TripsToLegsAlgorithm tripsToLegs = null
 
ChooseRandomSingleLegMode changeSingleLegMode = null
 
double coordDist = 0
 

Static Private Attributes

static final Logger logger = LogManager.getLogger(ChooseRandomLegModeForSubtour.class)
 

Detailed Description

Changes the transportation mode of one random non-empty subtour in a plan to a randomly chosen different mode given a list of possible modes, considering that the means of transport follows the law of mass conservation.

Author
michaz
See also
SubtourModeChoice

Definition at line 49 of file ChooseRandomLegModeForSubtour.java.

Constructor & Destructor Documentation

◆ ChooseRandomLegModeForSubtour() [1/2]

org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.ChooseRandomLegModeForSubtour ( final MainModeIdentifier  mainModeIdentifier,
final PermissibleModesCalculator  permissibleModesCalculator,
final String []  modes,
final String []  chainBasedModes,
final Random  rng,
SubtourModeChoice.Behavior  behavior,
double  probaForChooseRandomSingleTripMode 
)

Definition at line 112 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.behavior, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.chainBasedModes, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.mainModeIdentifier, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.modes, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.permissibleModesCalculator, and org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.rng.

◆ ChooseRandomLegModeForSubtour() [2/2]

org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.ChooseRandomLegModeForSubtour ( final MainModeIdentifier  mainModeIdentifier,
final PermissibleModesCalculator  permissibleModesCalculator,
final String []  modes,
final String []  chainBasedModes,
final Random  rng,
SubtourModeChoice.Behavior  behavior,
double  probaForChooseRandomSingleTripMode,
double  coordDist 
)

Definition at line 121 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.behavior, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.chainBasedModes, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.coordDist, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.mainModeIdentifier, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.modes, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.permissibleModesCalculator, and org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.rng.

126  {
129  this.modes = Arrays.asList(modes);
130  this.chainBasedModes = Arrays.asList(chainBasedModes);
131  this.behavior = behavior;
132  this.probaForChangeSingleTripMode = probaForChooseRandomSingleTripMode;
134  this.coordDist = coordDist;
135 
136  this.rng = rng;
137  logger.info("Chain based modes: " + this.chainBasedModes.toString());
138 
139  // also set up the standard change single leg mode, in order to alternatively randomize modes in
140  // subtours. kai, may'18
141  if (this.probaForChangeSingleTripMode > 0.) {
142  Collection<String> notChainBasedModes = new ArrayList<>(this.modes);
143  notChainBasedModes.removeAll(this.chainBasedModes);
144  final String[] possibleModes = notChainBasedModes.toArray(new String[]{});
145  if (possibleModes.length >= 2) {
146  // nothing to choose if there is only one mode! I also don't think that we can base this on size, since
147  // mode strings might be registered multiple times (these are not sets). kai, may'18
148 
149  this.tripsToLegs = new TripsToLegsAlgorithm(this.mainModeIdentifier);
150 
151  // change single leg would not respect car availability, if car is not a chain based mode -- cr, aug'22
152  this.changeSingleLegMode = new ChooseRandomSingleLegMode(possibleModes, rng, true);
153  }
154  }
155  }

Member Function Documentation

◆ setSingleTripSubtourModes()

void org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.setSingleTripSubtourModes ( final String []  singleTripSubtourModes)

Some subtour consist of only a single trip, e.g. if a trip starts and ends on the same link or facility. By default, for those modes, the normal chain based modes are available. But in certain cases, not all the modes should be available for such trips (e.g. car-sharing does not make much sense for such a trip), thus the list of modes available for single-trip subtours can be specified independently. As mentioned, it is initialized by the constructor to the full list of chain based modes.

Parameters
singleTripSubtourModes

Definition at line 166 of file ChooseRandomLegModeForSubtour.java.

◆ determineChoiceSet() [1/2]

List<Candidate> org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.determineChoiceSet ( final Plan  plan)

Return all possible choices for a certain plan.

Definition at line 173 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.population.algorithms.PermissibleModesCalculator.getPermissibleModes(), org.matsim.api.core.v01.population.Plan.getPlanElements(), and org.matsim.core.router.TripStructureUtils.getTrips().

Referenced by org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.run().

173  {
174 
175  final Id<? extends BasicLocation> homeLocation = ((Activity) plan.getPlanElements().get(0)).getFacilityId() != null ?
176  ((Activity) plan.getPlanElements().get(0)).getFacilityId() :
177  ((Activity) plan.getPlanElements().get(0)).getLinkId();
178  Collection<String> permissibleModesForThisPlan = permissibleModesCalculator.getPermissibleModes(plan);
179 
180  return determineChoiceSet(
181  homeLocation,
182  plan,
183  TripStructureUtils.getTrips(plan),
184  permissibleModesForThisPlan);
185  }
Here is the call graph for this function:

◆ isMassConserving() [1/2]

boolean org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.isMassConserving ( final Subtour  subtour)

Return whether a subtour is mass conserving according to configuration. Ignoring if subtours are closed.

Definition at line 190 of file ChooseRandomLegModeForSubtour.java.

Referenced by org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.determineChoiceSet().

190  {
191 
192  for (String mode : chainBasedModes) {
193  if (!isMassConserving(subtour, mode)) {
194  return false;
195  }
196  }
197  return true;
198  }

◆ run()

void org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.run ( final Plan  plan)

Implements org.matsim.core.population.algorithms.PlanAlgorithm.

Definition at line 201 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.applyChange(), org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.determineChoiceSet(), org.matsim.core.population.algorithms.PermissibleModesCalculator.getPermissibleModes(), org.matsim.api.core.v01.population.Plan.getPlanElements(), org.matsim.core.router.TripStructureUtils.getTrips(), org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.hasSingleTripChoice(), org.matsim.core.population.algorithms.TripsToLegsAlgorithm.run(), and org.matsim.core.population.algorithms.ChooseRandomSingleLegMode.run().

201  {
202  if (plan.getPlanElements().size() <= 1) {
203  return;
204  }
205 
206  // trips are used multiple times
207  List<Trip> trips = TripStructureUtils.getTrips(plan);
208 
209  // with certain proba, do standard single leg mode choice for not-chain-based modes:
210  // if a plan consist of only chain based modes, change single trip won't be able to do anything, in this case it will be ignored
211  if (this.changeSingleLegMode != null && rng.nextDouble() < this.probaForChangeSingleTripMode && hasSingleTripChoice(trips)) {
212  // (the null check is for computational efficiency)
213 
214  this.tripsToLegs.run(plan);
215  this.changeSingleLegMode.run(plan);
216  return;
217  }
218 
219  final Id<? extends BasicLocation> homeLocation = ((Activity) plan.getPlanElements().get(0)).getFacilityId() != null ?
220  ((Activity) plan.getPlanElements().get(0)).getFacilityId() :
221  ((Activity) plan.getPlanElements().get(0)).getLinkId();
222 
223  Collection<String> permissibleModesForThisPlan = permissibleModesCalculator.getPermissibleModes(plan);
224  // (modes that agent can in principle use; e.g. cannot use car sharing if not member)
225 
226 
227  List<Candidate> choiceSet = determineChoiceSet(homeLocation, plan, trips, permissibleModesForThisPlan);
228 
229  if (!choiceSet.isEmpty()) {
230  Candidate whatToDo = choiceSet.get(rng.nextInt(choiceSet.size()));
231  // (means that in the end we are changing modes only for one subtour)
232 
233  applyChange(whatToDo, plan);
234  }
235  }
Here is the call graph for this function:

◆ hasSingleTripChoice()

boolean org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.hasSingleTripChoice ( List< Trip trips)
private

Checks if change single trip mode would have an option to choose from. That is the case, when not all trips are chain based.

Definition at line 240 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.router.MainModeIdentifier.identifyMainMode().

Referenced by org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.run().

240  {
241  return !trips.stream().map(
242  t -> mainModeIdentifier.identifyMainMode(t.getTripElements())
243  ).allMatch(chainBasedModes::contains);
244  }
String identifyMainMode(List<? extends PlanElement > tripElements)
Here is the call graph for this function:

◆ determineChoiceSet() [2/2]

List<Candidate> org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.determineChoiceSet ( final Id<? extends BasicLocation homeLocation,
final Plan  plan,
final List< Trip trips,
final Collection< String >  permissibleModesForThisPerson 
)
private

Definition at line 246 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.behavior, org.matsim.core.replanning.modules.SubtourModeChoice.Behavior.betweenAllAndFewerConstraints, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.Candidate.Candidate(), org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.chainBasedModes, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.containsUnknownMode(), org.matsim.api.core.v01.Id< T >.equals(), org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.findLastDestinationOfMode(), org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.getLocationId(), org.matsim.core.router.TripStructureUtils.Subtour.getParent(), org.matsim.core.router.TripStructureUtils.getSubtours(), org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.getTransportMode(), org.matsim.core.router.TripStructureUtils.Subtour.getTrips(), org.matsim.core.router.TripStructureUtils.getUnclosedRootSubtour(), org.matsim.core.router.MainModeIdentifier.identifyMainMode(), org.matsim.core.router.TripStructureUtils.Subtour.isClosed, org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.isMassConserving(), org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.singleTripSubtourModes, and org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.Candidate.subtour.

250  {
251 
252  final List<Subtour> subtours = new ArrayList<>(TripStructureUtils.getSubtours(plan, coordDist));
253  final ArrayList<Candidate> choiceSet = new ArrayList<>();
254 
255  // there is no subtour containing all trips, so it will be added
256  if (behavior == SubtourModeChoice.Behavior.betweenAllAndFewerConstraints && trips.size() > 0 && subtours.stream().noneMatch(s -> s.getTrips().equals(trips))) {
257  subtours.add(0, TripStructureUtils.getUnclosedRootSubtour(plan));
258  }
259 
260  for (Subtour subtour : subtours) {
261 
262  // If subtour is not closed, it will still be considered if constrains are disabled
263  if (!subtour.isClosed() && behavior != SubtourModeChoice.Behavior.betweenAllAndFewerConstraints) {
264  continue;
265  }
266 
267  if (containsUnknownMode(subtour)) {
268  continue;
269  }
270 
271  final Id<? extends BasicLocation> subtourStartLocation =
272 // anchorAtFacilities ?
273  subtour.getTrips().get(0).getOriginActivity().getFacilityId() != null ?
274  subtour.getTrips().get(0).getOriginActivity().getFacilityId() :
275  subtour.getTrips().get(0).getOriginActivity().getLinkId();
276 
277  final Collection<String> testingModes =
278  subtour.getTrips().size() == 1 ?
281  // I am not sure what the singleTripSubtourModes thing means. But apart from that ...
282 
283  // ... test whether a vehicle was brought to the subtourStartLocation:
284  final Set<String> usableChainBasedModes = new LinkedHashSet<>();
285  for (String mode : testingModes) {
286  Id<? extends BasicLocation> vehicleLocation = homeLocation;
287  Activity lastDestination =
289  trips.subList(
290  0,
291  trips.indexOf(subtour.getTrips().get(0))),
292  mode);
293  if (lastDestination != null) {
294  vehicleLocation = getLocationId(lastDestination);
295  }
296  if (vehicleLocation.equals(subtourStartLocation)) {
297  usableChainBasedModes.add(mode);
298  // can have more than one mode here when subtour starts at home.
299  }
300  }
301 
302  // yy My intuition is that with the above condition, a switch of an all-car plan with at least
303  // one explicit sub-tour could switch to an all-bicycle plan only via first changing the
304  // explicit sub-tour first to a non-chain-based mode. kai, jul'18
305 
306  // The top-level subtour may use all chain-based modes freely for the whole plan
307 
308  Set<String> usableModes = new LinkedHashSet<>();
309  if (isMassConserving(subtour) || (behavior == SubtourModeChoice.Behavior.betweenAllAndFewerConstraints && subtour.getParent() == null)) { // We can only replace a subtour if it doesn't itself move a vehicle from one place to another
310  for (String candidate : permissibleModesForThisPerson) {
311  if (chainBasedModes.contains(candidate)) {
312  // for chain-based modes, only add if vehicle is available:
313  if (usableChainBasedModes.contains(candidate)) {
314  usableModes.add(candidate);
315  }
316  } else {
317  // for non-chain-based modes, always add:
318  usableModes.add(candidate);
319  }
320  }
321  }
322 
323  if (behavior == SubtourModeChoice.Behavior.betweenAllAndFewerConstraints) {
324 
325  // only remove candidates if the whole trip uses the same mode
326  usableModes.removeIf(mode -> subtour.getTrips().stream().allMatch(
327  trip-> mode.equals(mainModeIdentifier.identifyMainMode(trip.getTripElements()))
328  ));
329 
330  } else {
331  // The old behaviour removes candidates that could change trips to a different mode
332  usableModes.remove(getTransportMode(subtour));
333  }
334 
335 
336  // (remove current mode so we don't get it again; note that the parent plan is kept anyways)
337  for (String transportMode : usableModes) {
338  choiceSet.add(
339  new Candidate(trips,
340  subtour,
341  transportMode));
342  }
343  }
344  return choiceSet;
345  }
String identifyMainMode(List<? extends PlanElement > tripElements)
Activity findLastDestinationOfMode(final List< Trip > tripsToSearch, final String mode)
Here is the call graph for this function:

◆ containsUnknownMode()

boolean org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.containsUnknownMode ( final Subtour  subtour)
private

Definition at line 347 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.router.TripStructureUtils.Subtour.getTrips(), and org.matsim.core.router.MainModeIdentifier.identifyMainMode().

Referenced by org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.determineChoiceSet().

347  {
348  for (Trip trip : subtour.getTrips()) {
349  if (!modes.contains(mainModeIdentifier.identifyMainMode(trip.getTripElements()))) {
350  return true;
351  }
352  }
353  return false;
354  }
String identifyMainMode(List<? extends PlanElement > tripElements)
Here is the call graph for this function:

◆ isMassConserving() [2/2]

boolean org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.isMassConserving ( final Subtour  subtour,
final String  mode 
)
private

Definition at line 356 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.atSameLocation(), org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.findFirstOriginOfMode(), org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.findLastDestinationOfMode(), and org.matsim.core.router.TripStructureUtils.Subtour.getTrips().

358  {
359  final Activity firstOrigin =
361  subtour.getTrips(),
362  mode);
363 
364  if (firstOrigin == null) {
365  return true;
366  }
367 
368  final Activity lastDestination =
370  subtour.getTrips(),
371  mode);
372 
373  return atSameLocation(firstOrigin, lastDestination);
374  }
boolean atSameLocation(Activity firstLegUsingMode, Activity lastLegUsingMode)
Activity findFirstOriginOfMode(final List< Trip > tripsToSearch, final String mode)
Activity findLastDestinationOfMode(final List< Trip > tripsToSearch, final String mode)
Here is the call graph for this function:

◆ getLocationId()

Id<? extends BasicLocation> org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.getLocationId ( Activity  activity)
private

Definition at line 376 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.api.core.v01.population.Activity.getFacilityId(), and org.matsim.api.core.v01.population.Activity.getLinkId().

Referenced by org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.determineChoiceSet().

376  {
377  return activity.getFacilityId() != null ?
378  activity.getFacilityId() :
379  activity.getLinkId();
380  }
Here is the call graph for this function:

◆ atSameLocation()

boolean org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.atSameLocation ( Activity  firstLegUsingMode,
Activity  lastLegUsingMode 
)
private

Definition at line 382 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.utils.geometry.CoordUtils.calcEuclideanDistance(), org.matsim.api.core.v01.population.Activity.getCoord(), org.matsim.api.core.v01.population.Activity.getFacilityId(), and org.matsim.api.core.v01.population.Activity.getLinkId().

Referenced by org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.isMassConserving().

383  {
384  if (firstLegUsingMode.getFacilityId() != null)
385  return firstLegUsingMode.getFacilityId().equals(lastLegUsingMode.getFacilityId());
386  else if ( coordDist > 0 && firstLegUsingMode.getCoord() != null && lastLegUsingMode.getCoord() != null)
387  return CoordUtils.calcEuclideanDistance(firstLegUsingMode.getCoord(), lastLegUsingMode.getCoord()) <= coordDist;
388  else
389  return firstLegUsingMode.getLinkId().equals(lastLegUsingMode.getLinkId());
390  }
Here is the call graph for this function:

◆ findLastDestinationOfMode()

Activity org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.findLastDestinationOfMode ( final List< Trip tripsToSearch,
final String  mode 
)
private

Definition at line 392 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.router.MainModeIdentifier.identifyMainMode().

Referenced by org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.determineChoiceSet(), and org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.isMassConserving().

394  {
395  final List<Trip> reversed = new ArrayList<>(tripsToSearch);
396  Collections.reverse(reversed);
397  for (Trip trip : reversed) {
398  if (mode.equals(mainModeIdentifier.identifyMainMode(trip.getTripElements()))) {
399  return trip.getDestinationActivity();
400  }
401  }
402  return null;
403  }
String identifyMainMode(List<? extends PlanElement > tripElements)
Here is the call graph for this function:

◆ findFirstOriginOfMode()

Activity org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.findFirstOriginOfMode ( final List< Trip tripsToSearch,
final String  mode 
)
private

Definition at line 405 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.router.MainModeIdentifier.identifyMainMode().

Referenced by org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.isMassConserving().

407  {
408  for (Trip trip : tripsToSearch) {
409  if (mode.equals(mainModeIdentifier.identifyMainMode(trip.getTripElements()))) {
410  return trip.getOriginActivity();
411  }
412  }
413  return null;
414  }
String identifyMainMode(List<? extends PlanElement > tripElements)
Here is the call graph for this function:

◆ getTransportMode()

String org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.getTransportMode ( final Subtour  subtour)
private

Definition at line 416 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.router.TripStructureUtils.Subtour.getTrips(), and org.matsim.core.router.MainModeIdentifier.identifyMainMode().

Referenced by org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.determineChoiceSet().

416  {
418  subtour.getTrips().get(0).getTripElements());
419  }
String identifyMainMode(List<? extends PlanElement > tripElements)
Here is the call graph for this function:

◆ applyChange()

void org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.applyChange ( final Candidate  whatToDo,
final Plan  plan 
)
private

Definition at line 421 of file ChooseRandomLegModeForSubtour.java.

References org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.behavior, org.matsim.core.replanning.modules.SubtourModeChoice.Behavior.betweenAllAndFewerConstraints, org.matsim.core.population.PopulationUtils.createLeg(), org.matsim.core.replanning.modules.SubtourModeChoice.Behavior.fromSpecifiedModesToSpecifiedModes, org.matsim.api.core.v01.population.Plan.getPerson(), org.matsim.core.router.TripStructureUtils.getTrips(), org.matsim.core.router.TripStructureUtils.Subtour.getTrips(), org.matsim.core.router.TripStructureUtils.Subtour.getTripsWithoutSubSubtours(), org.matsim.core.router.MainModeIdentifier.identifyMainMode(), org.matsim.core.router.TripRouter.insertTrip(), org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.Candidate.newTransportMode, and org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.Candidate.subtour.

Referenced by org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.run().

423  {
424 
425  boolean containsChainBasedMode = false;
426  Set<String> originalModes = new HashSet<>();
427 
428  // Trips that are not part of any child subtour
429  Set<Trip> childTrips = new HashSet<>(whatToDo.subtour.getTripsWithoutSubSubtours());
430 
431  for (Trip trip : whatToDo.subtour.getTrips()) {
432 
433  String mainMode = mainModeIdentifier.identifyMainMode(trip.getTripElements());
434 
435  if (childTrips.contains(trip) && chainBasedModes.contains(mainMode))
436  containsChainBasedMode = true;
437 
438  if (childTrips.contains(trip))
439  originalModes.add(mainMode);
440 
441  if (behavior == SubtourModeChoice.Behavior.fromSpecifiedModesToSpecifiedModes) {
442  if (!modes.contains(mainMode)) {
443  // (ignore trips with modes that are not in "modes". MATSIM-809)
444  continue;
445  }
446  }
447 
448  TripRouter.insertTrip(
449  plan,
450  trip.getOriginActivity(),
451  Collections.singletonList(PopulationUtils.createLeg(whatToDo.newTransportMode)),
452  trip.getDestinationActivity());
453  }
454 
455 
456  // If this is a subtour covering all trips, this constraint will be ignored
457  // this is because the whole plan is always added as a whole even if not a subtour
458  if (behavior == SubtourModeChoice.Behavior.betweenAllAndFewerConstraints
459  && containsChainBasedMode && originalModes.size() > 1
460  && TripStructureUtils.getTrips(plan).size() != whatToDo.subtour.getTrips().size()
461  ) {
462  logger.error("Subtour of person {} contains a mix of chain- and non-chainbased modes: {}. " +
463  "Subtour mode-choice won't be able to go back to this state. You need to adjust the input beforehand or disable 'betweenAllAndFewerConstraints'.", plan.getPerson().getId(), originalModes);
464  throw new IllegalStateException("Subtour contains a mix of chain- and non-chainbased modes.");
465  }
466 
467  }
String identifyMainMode(List<? extends PlanElement > tripElements)
Here is the call graph for this function:

Member Data Documentation

◆ logger

final Logger org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.logger = LogManager.getLogger(ChooseRandomLegModeForSubtour.class)
staticprivate

Definition at line 51 of file ChooseRandomLegModeForSubtour.java.

◆ modes

Collection<String> org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.modes
private

◆ chainBasedModes

final Collection<String> org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.chainBasedModes
private

◆ behavior

final SubtourModeChoice.Behavior org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.behavior
private

◆ singleTripSubtourModes

Collection<String> org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.singleTripSubtourModes
private

◆ mainModeIdentifier

final MainModeIdentifier org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.mainModeIdentifier
private

◆ rng

final Random org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.rng
private

◆ permissibleModesCalculator

PermissibleModesCalculator org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.permissibleModesCalculator
private

◆ probaForChangeSingleTripMode

final double org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.probaForChangeSingleTripMode
private

Definition at line 106 of file ChooseRandomLegModeForSubtour.java.

◆ tripsToLegs

TripsToLegsAlgorithm org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.tripsToLegs = null
private

Definition at line 107 of file ChooseRandomLegModeForSubtour.java.

◆ changeSingleLegMode

ChooseRandomSingleLegMode org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.changeSingleLegMode = null
private

Definition at line 108 of file ChooseRandomLegModeForSubtour.java.

◆ coordDist

double org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour.coordDist = 0
private

The documentation for this class was generated from the following file: