View Javadoc
1   package org.opentrafficsim.road.gtu.generator.characteristics;
2   
3   import java.util.LinkedHashMap;
4   import java.util.LinkedHashSet;
5   import java.util.Map;
6   import java.util.Optional;
7   import java.util.Set;
8   import java.util.function.BiFunction;
9   import java.util.function.Supplier;
10  
11  import org.djutils.exceptions.Throw;
12  import org.opentrafficsim.core.definitions.Defaults;
13  import org.opentrafficsim.core.definitions.DefaultsNl;
14  import org.opentrafficsim.core.gtu.GtuCharacteristics;
15  import org.opentrafficsim.core.gtu.GtuException;
16  import org.opentrafficsim.core.gtu.GtuTemplate;
17  import org.opentrafficsim.core.gtu.GtuType;
18  import org.opentrafficsim.core.network.Node;
19  import org.opentrafficsim.core.network.route.Route;
20  import org.opentrafficsim.road.gtu.VehicleModel;
21  import org.opentrafficsim.road.gtu.VehicleModelFactory;
22  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
23  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlannerFactory;
24  import org.opentrafficsim.road.gtu.tactical.lmrs.Lmrs;
25  import org.opentrafficsim.road.gtu.tactical.lmrs.LmrsFactory;
26  import org.opentrafficsim.road.gtu.tactical.lmrs.LmrsFactory.Setting;
27  import org.opentrafficsim.road.od.Categorization;
28  import org.opentrafficsim.road.od.Category;
29  
30  import nl.tudelft.simulation.jstats.streams.StreamInterface;
31  
32  /**
33   * Default generator for {@code LaneBasedGtuCharacteristics} in a context with OD information.
34   * <p>
35   * Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
36   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
37   * </p>
38   * @author Alexander Verbraeck
39   * @author Peter Knoppers
40   * @author Wouter Schakel
41   */
42  public final class DefaultLaneBasedGtuCharacteristicsGeneratorOd implements LaneBasedGtuCharacteristicsGeneratorOd
43  {
44      /** GTU type generator. */
45      private Supplier<GtuType> gtuTypeGenerator = null;
46  
47      /** Templates. */
48      private final Map<GtuType, GtuTemplate> templates = new LinkedHashMap<>();
49  
50      /** Supplies a strategical factory. */
51      private final LaneBasedStrategicalPlannerFactory<?> factory;
52  
53      /** Vehicle factory. */
54      private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
55  
56      /** Template function. */
57      private BiFunction<GtuType, StreamInterface, Optional<GtuTemplate>> templateFunction;
58  
59      /**
60       * Constructor using route supplier, provided GTU templates and provided strategical planner factory supplier.
61       * @param gtuTypeGenerator GTU type supplier
62       * @param templates templates
63       * @param factory strategical factory supplier
64       * @param vehicleModelFactory vehicle model factory
65       * @param templateFunction template function
66       */
67      private DefaultLaneBasedGtuCharacteristicsGeneratorOd(final Supplier<GtuType> gtuTypeGenerator,
68              final Set<GtuTemplate> templates, final LaneBasedStrategicalPlannerFactory<?> factory,
69              final VehicleModelFactory vehicleModelFactory,
70              final BiFunction<GtuType, StreamInterface, Optional<GtuTemplate>> templateFunction)
71      {
72          Throw.whenNull(factory, "Strategical planner factory may not be null.");
73          this.gtuTypeGenerator = gtuTypeGenerator;
74          if (templates != null)
75          {
76              for (GtuTemplate template : templates)
77              {
78                  this.templates.put(template.getGtuType(), template);
79              }
80          }
81          this.factory = factory;
82          if (vehicleModelFactory == null)
83          {
84              this.vehicleModelFactory = VehicleModelFactory.MINMAX;
85          }
86          else
87          {
88              this.vehicleModelFactory = vehicleModelFactory;
89          }
90          this.templateFunction = templateFunction;
91      }
92  
93      @Override
94      public LaneBasedGtuCharacteristics draw(final Node origin, final Node destination, final Category category,
95              final StreamInterface randomStream) throws GtuException
96      {
97          Categorization categorization = category.getCategorization();
98          // GTU characteristics
99          GtuType gtuType;
100         if (categorization.entails(GtuType.class))
101         {
102             gtuType = category.get(GtuType.class);
103         }
104         else if (this.gtuTypeGenerator != null)
105         {
106             gtuType = this.gtuTypeGenerator.get();
107         }
108         else
109         {
110             gtuType = DefaultsNl.CAR;
111         }
112         GtuCharacteristics gtuCharacteristics;
113         if (this.templates.containsKey(gtuType))
114         {
115             gtuCharacteristics = this.templates.get(gtuType).get();
116         }
117         else
118         {
119             gtuCharacteristics = this.templateFunction.apply(gtuType, randomStream).get().get();
120         }
121         Route route = categorization.entails(Route.class) ? category.get(Route.class) : null;
122         VehicleModel vehicleModel = this.vehicleModelFactory.create(gtuType);
123 
124         return new LaneBasedGtuCharacteristics(gtuCharacteristics, this.factory, route, origin, destination, vehicleModel);
125     }
126 
127     /**
128      * Returns a strategical model factory for a standard LMRS model, to be used in {@code Factory}.
129      * @param stream random number stream.
130      * @return factory for a standard LMRS model.
131      */
132     public static LaneBasedStrategicalRoutePlannerFactory defaultLmrs(final StreamInterface stream)
133     {
134         return new LaneBasedStrategicalRoutePlannerFactory(
135                 new LmrsFactory<>(Lmrs::new).setStream(stream).set(Setting.ACCELERATION_TRAFFIC_LIGHTS, true)
136                         .set(Setting.ACCELERATION_CONFLICTS, true).set(Setting.ACCELERATION_SPEED_LIMIT_TRANSITION, true));
137     }
138 
139     /**
140      * Factory for {@code DefaultGtuCharacteristicsGeneratorOD}.
141      * <p>
142      * Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
143      * <br>
144      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
145      * </p>
146      * @author Alexander Verbraeck
147      * @author Peter Knoppers
148      * @author Wouter Schakel
149      */
150     @SuppressWarnings("hiddenfield")
151     public static class Factory
152     {
153         /** GTU type. */
154         private Supplier<GtuType> gtuTypeGenerator = null;
155 
156         /** Templates. */
157         private Set<GtuTemplate> templates = new LinkedHashSet<>();
158 
159         /** Supplies a strategical factory. */
160         private final LaneBasedStrategicalPlannerFactory<?> factory;
161 
162         /** Vehicle factory. */
163         private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
164 
165         /** Default templates. */
166         private BiFunction<GtuType, StreamInterface, Optional<GtuTemplate>> templateFunction = Defaults.NL;
167 
168         /**
169          * Constructor.
170          * @param factory set factorySupplier.
171          */
172         public Factory(final LaneBasedStrategicalPlannerFactory<?> factory)
173         {
174             this.factory = factory;
175         }
176 
177         /**
178          * Set GTU type generator.
179          * @param gtuTypeGenerator set gtuTypeGenerator.
180          * @return this factory for method chaining
181          */
182         public Factory setGtuTypeGenerator(final Supplier<GtuType> gtuTypeGenerator)
183         {
184             this.gtuTypeGenerator = gtuTypeGenerator;
185             return this;
186         }
187 
188         /**
189          * Set templates.
190          * @param templates set templates.
191          * @return this factory for method chaining
192          */
193         public Factory setTemplates(final Set<GtuTemplate> templates)
194         {
195             this.templates = templates;
196             return this;
197         }
198 
199         /**
200          * Set vehicle model generator.
201          * @param vehicleModelFactory set vehicleModelFactory.
202          * @return this factory for method chaining
203          */
204         public Factory setVehicleModelGenerator(final VehicleModelFactory vehicleModelFactory)
205         {
206             this.vehicleModelFactory = vehicleModelFactory;
207             return this;
208         }
209 
210         /**
211          * Set GTU template function.
212          * @param templateFunction template function
213          * @return this factory for method chaining
214          */
215         public Factory setGtuTemplateFunction(
216                 final BiFunction<GtuType, StreamInterface, Optional<GtuTemplate>> templateFunction)
217         {
218             this.templateFunction = templateFunction;
219             return this;
220         }
221 
222         /**
223          * Creates the default GTU characteristics generator based on OD information.
224          * @return default GTU characteristics generator based on OD information
225          */
226         @SuppressWarnings("synthetic-access")
227         public DefaultLaneBasedGtuCharacteristicsGeneratorOd create()
228         {
229             return new DefaultLaneBasedGtuCharacteristicsGeneratorOd(this.gtuTypeGenerator, this.templates, this.factory,
230                     this.vehicleModelFactory, this.templateFunction);
231         }
232     }
233 
234 }