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://github.com/peter-knoppers">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 GTU type generator
57       * @param templates templates
58       * @param factory strategical factory supplier
59       * @param 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      @Override
86      public LaneBasedGtuCharacteristics draw(final Node origin, final Node destination, final Category category,
87              final StreamInterface randomStream) throws GtuException
88      {
89          Categorization categorization = category.getCategorization();
90          // GTU characteristics
91          GtuType gtuType;
92          if (categorization.entails(GtuType.class))
93          {
94              gtuType = category.get(GtuType.class);
95          }
96          else if (this.gtuTypeGenerator != null)
97          {
98              gtuType = Try.assign(() -> this.gtuTypeGenerator.draw(), GtuException.class, "Parameter while drawing GTU type.");
99          }
100         else
101         {
102             gtuType = DefaultsNl.CAR;
103         }
104         GtuCharacteristics gtuCharacteristics;
105         if (this.templates.containsKey(gtuType))
106         {
107             gtuCharacteristics =
108                     Try.assign(() -> this.templates.get(gtuType).draw(), "Exception while drawing GTU characteristics.");
109         }
110         else
111         {
112             gtuCharacteristics = Try.assign(() -> GtuType.defaultCharacteristics(gtuType, origin.getNetwork(), randomStream),
113                     "Exception while applying default GTU characteristics.");
114         }
115         Route route = categorization.entails(Route.class) ? category.get(Route.class) : null;
116         VehicleModel vehicleModel = this.vehicleModelFactory.create(gtuType);
117 
118         return new LaneBasedGtuCharacteristics(gtuCharacteristics, this.factory, route, origin, destination, vehicleModel);
119     }
120 
121     /**
122      * Returns a strategical model factory for a standard LMRS model, to be used in {@code Factory}.
123      * @param stream random number stream.
124      * @return factory for a standard LMRS model.
125      */
126     public static LaneBasedStrategicalRoutePlannerFactory defaultLmrs(final StreamInterface stream)
127     {
128         return new LaneBasedStrategicalRoutePlannerFactory(
129                 new LmrsFactory(new IdmPlusFactory(stream), new DefaultLmrsPerceptionFactory()));
130     }
131 
132     /**
133      * Factory for {@code DefaultGtuCharacteristicsGeneratorOD}.
134      * <p>
135      * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
136      * <br>
137      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
138      * </p>
139      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
140      * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
141      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
142      */
143     @SuppressWarnings("hiddenfield")
144     public static class Factory
145     {
146         /** GTU type. */
147         private Generator<GtuType> gtuTypeGenerator = null;
148 
149         /** Templates. */
150         private Set<GtuTemplate> templates = new LinkedHashSet<>();
151 
152         /** Supplies a strategical factory. */
153         private final LaneBasedStrategicalPlannerFactory<?> factory;
154 
155         /** Vehicle factory. */
156         private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
157 
158         /**
159          * Constructor.
160          * @param factory set factorySupplier.
161          */
162         public Factory(final LaneBasedStrategicalPlannerFactory<?> factory)
163         {
164             this.factory = factory;
165         }
166 
167         /**
168          * @param gtuTypeGenerator set gtuTypeGenerator.
169          * @return this factory for method chaining
170          */
171         public Factory setGtuTypeGenerator(final Generator<GtuType> gtuTypeGenerator)
172         {
173             this.gtuTypeGenerator = gtuTypeGenerator;
174             return this;
175         }
176 
177         /**
178          * @param templates set templates.
179          * @return this factory for method chaining
180          */
181         public Factory setTemplates(final Set<GtuTemplate> templates)
182         {
183             this.templates = templates;
184             return this;
185         }
186 
187         /**
188          * @param vehicleModelFactory set vehicleModelFactory.
189          * @return this factory for method chaining
190          */
191         public Factory setVehicleModelGenerator(final VehicleModelFactory vehicleModelFactory)
192         {
193             this.vehicleModelFactory = vehicleModelFactory;
194             return this;
195         }
196 
197         /**
198          * Creates the default GTU characteristics generator based on OD information.
199          * @return default GTU characteristics generator based on OD information
200          */
201         @SuppressWarnings("synthetic-access")
202         public DefaultLaneBasedGtuCharacteristicsGeneratorOd create()
203         {
204             return new DefaultLaneBasedGtuCharacteristicsGeneratorOd(this.gtuTypeGenerator, this.templates, this.factory,
205                     this.vehicleModelFactory);
206         }
207     }
208 
209 }