MATSIM
ChooseRandomSingleLegMode.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ChangeLegMode.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.population.algorithms;
22 
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Random;
36 
44 public final class ChooseRandomSingleLegMode implements PlanAlgorithm {
45 
46  private final String[] possibleModes;
47  private boolean ignoreCarAvailability = true;
49  private final Random rng;
50  private final List<String> possibleFromModes = new ArrayList<>();
51 
52 
59  public ChooseRandomSingleLegMode(final String[] possibleModes, final Random rng, boolean allowSwitchFromListedModesOnly) {
60  this.possibleModes = possibleModes.clone();
61  this.allowSwitchFromListedModesOnly=allowSwitchFromListedModesOnly;
62  if (allowSwitchFromListedModesOnly){
63  this.possibleFromModes.addAll(Arrays.asList(possibleModes));
64  }
65  this.rng = rng;
66  }
67 
68  public void setIgnoreCarAvailability(final boolean ignoreCarAvailability) {
69  this.ignoreCarAvailability = ignoreCarAvailability;
70  }
71 
72  @Override
73  public void run(final Plan plan) {
74  boolean forbidCar = false;
75  if (!this.ignoreCarAvailability) {
76  String carAvail = PersonUtils.getCarAvail(plan.getPerson());
77  if ("never".equals(carAvail)) {
78  forbidCar = true;
79  }
80  }
81 
82  ArrayList<Leg> legs = new ArrayList<>();
83  int cnt = 0;
84  for (PlanElement pe : plan.getPlanElements()) {
85  if (pe instanceof Leg) {
86  if (allowSwitchFromListedModesOnly){
87  if (this.possibleFromModes.contains(((Leg) pe).getMode())) {
88  legs.add((Leg) pe);
89  cnt++;
90  }
91  }
92  else {
93  legs.add((Leg) pe);
94  cnt++;
95  }
96  }
97  }
98  if (cnt == 0) {
99  return;
100  }
101  int rndIdx = this.rng.nextInt(cnt);
102  setRandomLegMode(legs.get(rndIdx), forbidCar);
103  }
104 
105  private void setRandomLegMode(final Leg leg, final boolean forbidCar) {
106  String newMode = chooseModeOtherThan(leg.getMode(), forbidCar);
107  leg.setMode(newMode);
108  TripStructureUtils.setRoutingMode(leg, newMode);
109  Route route = leg.getRoute() ;
110  if ( route != null && route instanceof NetworkRoute) {
111  ((NetworkRoute)route).setVehicleId(null);
112  }
113  }
114 
115  private String chooseModeOtherThan(final String currentMode, final boolean forbidCar) {
116  String newMode;
117  while (true) {
118  int newModeIdx = this.rng.nextInt(this.possibleModes.length - 1);
119  for (int i = 0; i <= newModeIdx; i++) {
120  if (this.possibleModes[i].equals(currentMode)) {
121  /* if the new Mode is after the currentMode in the list of possible
122  * modes, go one further, as we have to ignore the current mode in
123  * the list of possible modes. */
124  newModeIdx++;
125  break;
126  }
127  }
128  newMode = this.possibleModes[newModeIdx];
129  if (!(forbidCar && TransportMode.car.equals(newMode))) {
130  break;
131  } else {
132  if (this.possibleModes.length == 2) {
133  newMode = currentMode; // there is no other mode available
134  break;
135  }
136  }
137  }
138  return newMode;
139  }
140 
141 }
ChooseRandomSingleLegMode(final String[] possibleModes, final Random rng, boolean allowSwitchFromListedModesOnly)
static String getCarAvail(Person person)
String chooseModeOtherThan(final String currentMode, final boolean forbidCar)
static void setRoutingMode(Leg leg, String mode)
List< PlanElement > getPlanElements()