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