View Javadoc
1   package org.opentrafficsim.road.gtu.generator.od;
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.distributions.Generator;
11  import org.opentrafficsim.core.gtu.GTUCharacteristics;
12  import org.opentrafficsim.core.gtu.GTUException;
13  import org.opentrafficsim.core.gtu.GTUType;
14  import org.opentrafficsim.core.gtu.TemplateGTUType;
15  import org.opentrafficsim.core.network.Node;
16  import org.opentrafficsim.core.network.route.Route;
17  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristics;
18  import org.opentrafficsim.road.gtu.lane.VehicleModel;
19  import org.opentrafficsim.road.gtu.lane.VehicleModelFactory;
20  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
21  import org.opentrafficsim.road.gtu.strategical.od.Categorization;
22  import org.opentrafficsim.road.gtu.strategical.od.Category;
23  import org.opentrafficsim.road.gtu.strategical.route.RouteGeneratorOD;
24  
25  import nl.tudelft.simulation.jstats.streams.StreamInterface;
26  
27  /**
28   * Default generator for {@code LaneBasedGTUCharacteristics}.
29   * <p>
30   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
31   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
32   * <p>
33   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 10 dec. 2017 <br>
34   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
35   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
36   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
37   */
38  public final class DefaultGTUCharacteristicsGeneratorOD implements GTUCharacteristicsGeneratorOD
39  {
40      /** GTU type generator. */
41      private Generator<GTUType> gtuTypeGenerator = null;
42  
43      /** Templates. */
44      private final Map<GTUType, TemplateGTUType> templates = new LinkedHashMap<>();
45  
46      /** Route generator. */
47      private final RouteGeneratorOD routeGenerator;
48  
49      /** Supplies a strategical factory. */
50      private final StrategicalPlannerFactorySupplierOD factorySupplier;
51  
52      /** Vehicle factory. */
53      private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
54  
55      /**
56       * Constructor using null-routes, default GTU characteristics and LMRS.
57       */
58      public DefaultGTUCharacteristicsGeneratorOD()
59      {
60          this(null, RouteGeneratorOD.NULL, new LinkedHashSet<>(), StrategicalPlannerFactorySupplierOD.lmrs());
61      }
62  
63      /**
64       * Constructor using route generator, default GTU characteristics and LMRS.
65       * @param routeGenerator RouteGeneratorOD; route generator
66       */
67      public DefaultGTUCharacteristicsGeneratorOD(final RouteGeneratorOD routeGenerator)
68      {
69          this(null, routeGenerator, new LinkedHashSet<>(), StrategicalPlannerFactorySupplierOD.lmrs());
70      }
71  
72      /**
73       * Constructor using route supplier, provided GTU templates and LMRS.
74       * @param routeSupplier RouteGeneratorOD; route supplier
75       * @param templates Set&lt;TemplateGTUType&gt;; templates
76       */
77      public DefaultGTUCharacteristicsGeneratorOD(final RouteGeneratorOD routeSupplier, final Set<TemplateGTUType> templates)
78      {
79          this(null, routeSupplier, templates, StrategicalPlannerFactorySupplierOD.lmrs());
80      }
81  
82      /**
83       * Constructor using route supplier, default GTU characteristics and provided strategical planner factory supplier.
84       * @param routeGenerator RouteGeneratorOD; route generator
85       * @param factorySupplier StrategicalPlannerFactorySupplierOD; strategical factory supplier
86       */
87      public DefaultGTUCharacteristicsGeneratorOD(final RouteGeneratorOD routeGenerator,
88              final StrategicalPlannerFactorySupplierOD factorySupplier)
89      {
90          this(null, routeGenerator, new LinkedHashSet<>(), factorySupplier);
91      }
92  
93      /**
94       * Constructor using route supplier, provided GTU templates and provided strategical planner factory supplier.
95       * @param gtuTypeGenerator Generator&lt;GTUType&gt;; GTU type generator
96       * @param routeGenerator RouteGeneratorOD; route generator
97       * @param templates Set&lt;TemplateGTUType&gt;; templates
98       * @param factorySupplier StrategicalPlannerFactorySupplierOD; strategical factory supplier
99       */
100     public DefaultGTUCharacteristicsGeneratorOD(final Generator<GTUType> gtuTypeGenerator,
101             final RouteGeneratorOD routeGenerator, final Set<TemplateGTUType> templates,
102             final StrategicalPlannerFactorySupplierOD factorySupplier)
103     {
104         Throw.whenNull(factorySupplier, "Strategical factory supplier may not be null.");
105         this.gtuTypeGenerator = gtuTypeGenerator;
106         if (routeGenerator == null)
107         {
108             this.routeGenerator = RouteGeneratorOD.NULL;
109         }
110         else
111         {
112             this.routeGenerator = routeGenerator;
113         }
114         if (templates != null)
115         {
116             for (TemplateGTUType template : templates)
117             {
118                 this.templates.put(template.getGTUType(), template);
119             }
120         }
121         this.factorySupplier = factorySupplier;
122     }
123 
124     /**
125      * Constructor using null-routes, provided GTU templates and LMRS.
126      * @param templates Set&lt;TemplateGTUType&gt;; templates
127      */
128     public DefaultGTUCharacteristicsGeneratorOD(final Set<TemplateGTUType> templates)
129     {
130         this(null, RouteGeneratorOD.NULL, templates, StrategicalPlannerFactorySupplierOD.lmrs());
131     }
132 
133     /**
134      * Constructor using null-routes, provided GTU templates and provided strategical planner factory supplier.
135      * @param templates Set&lt;TemplateGTUType&gt;; templates
136      * @param factorySupplier StrategicalPlannerFactorySupplierOD; strategical factory supplier
137      */
138     public DefaultGTUCharacteristicsGeneratorOD(final Set<TemplateGTUType> templates,
139             final StrategicalPlannerFactorySupplierOD factorySupplier)
140     {
141         this(null, RouteGeneratorOD.NULL, templates, factorySupplier);
142     }
143 
144     /**
145      * Constructor using using null-routes, default GTU characteristics and provided GTU templates and provided strategical
146      * planner factory supplier.
147      * @param factorySupplier StrategicalPlannerFactorySupplierOD; strategical factory supplier
148      */
149     public DefaultGTUCharacteristicsGeneratorOD(final StrategicalPlannerFactorySupplierOD factorySupplier)
150     {
151         this(null, RouteGeneratorOD.NULL, new LinkedHashSet<>(), factorySupplier);
152     }
153 
154     // TODO: remove above constructors and use factory always
155 
156     /**
157      * Constructor using route supplier, provided GTU templates and provided strategical planner factory supplier.
158      * @param gtuTypeGenerator Generator&lt;GTUType&gt;; GTU type generator
159      * @param routeSupplier RouteGeneratorOD; route supplier
160      * @param templates Set&lt;TemplateGTUType&gt;; templates
161      * @param factorySupplier StrategicalPlannerFactorySupplierOD; strategical factory supplier
162      * @param vehicleModelFactory VehicleModelFactory; vehicle model factory
163      */
164     private DefaultGTUCharacteristicsGeneratorOD(final Generator<GTUType> gtuTypeGenerator,
165             final RouteGeneratorOD routeSupplier, final Set<TemplateGTUType> templates,
166             final StrategicalPlannerFactorySupplierOD factorySupplier, final VehicleModelFactory vehicleModelFactory)
167     {
168         Throw.whenNull(factorySupplier, "Strategical factory supplier may not be null.");
169         this.gtuTypeGenerator = gtuTypeGenerator;
170         if (routeSupplier == null)
171         {
172             this.routeGenerator = RouteGeneratorOD.NULL;
173         }
174         else
175         {
176             this.routeGenerator = routeSupplier;
177         }
178         if (templates != null)
179         {
180             for (TemplateGTUType template : templates)
181             {
182                 this.templates.put(template.getGTUType(), template);
183             }
184         }
185         this.factorySupplier = factorySupplier;
186         if (vehicleModelFactory == null)
187         {
188             this.vehicleModelFactory = VehicleModelFactory.MINMAX;
189         }
190         else
191         {
192             this.vehicleModelFactory = vehicleModelFactory;
193         }
194     }
195 
196     /** {@inheritDoc} */
197     @Override
198     public LaneBasedGTUCharacteristics draw(final Node origin, final Node destination, final Category category,
199             final StreamInterface randomStream) throws GTUException
200     {
201         Categorization categorization = category.getCategorization();
202         // GTU characteristics
203         GTUType gtuType;
204         if (categorization.entails(GTUType.class))
205         {
206             gtuType = category.get(GTUType.class);
207         }
208         else if (this.gtuTypeGenerator != null)
209         {
210             gtuType = Try.assign(() -> this.gtuTypeGenerator.draw(), GTUException.class, "Parameter while drawing GTU type.");
211         }
212         else
213         {
214             gtuType = origin.getNetwork().getGtuType(GTUType.DEFAULTS.CAR);
215         }
216         GTUCharacteristics gtuCharacteristics;
217         if (this.templates.containsKey(gtuType))
218         {
219             gtuCharacteristics =
220                     Try.assign(() -> this.templates.get(gtuType).draw(), "Exception while drawing GTU characteristics.");
221         }
222         else
223         {
224             gtuCharacteristics = Try.assign(() -> GTUType.defaultCharacteristics(gtuType, origin.getNetwork(), randomStream),
225                     "Exception while applying default GTU characteristics.");
226         }
227         // strategical factory
228         LaneBasedStrategicalPlannerFactory<?> laneBasedStrategicalPlannerFactory =
229                 this.factorySupplier.getFactory(origin, destination, category, randomStream);
230         // route
231         Route route;
232         if (categorization.entails(Route.class))
233         {
234             route = category.get(Route.class);
235         }
236         else
237         {
238             // get route from supplier
239             // XXX typically gets the route from RouteGeneratorOD.getRoute(...)
240             route = this.routeGenerator.getRoute(origin, destination, gtuType);
241         }
242         // vehicle model
243         VehicleModel vehicleModel = this.vehicleModelFactory.create(gtuType);
244 
245         return new LaneBasedGTUCharacteristics(gtuCharacteristics, laneBasedStrategicalPlannerFactory, route, origin,
246                 destination, vehicleModel);
247     }
248 
249     /**
250      * Factory for {@code DefaultGTUCharacteristicsGeneratorOD}.
251      * <p>
252      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
253      * <br>
254      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
255      * <p>
256      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 8 jan. 2019 <br>
257      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
258      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
259      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
260      */
261     @SuppressWarnings("hiddenfield")
262     public static class Factory
263     {
264         /** GTU type. */
265         private Generator<GTUType> gtuTypeGenerator = null;
266 
267         /** Templates. */
268         private Set<TemplateGTUType> templates = new LinkedHashSet<>();
269 
270         /** Route supplier. */
271         private RouteGeneratorOD routeGenerator = RouteGeneratorOD.NULL;
272 
273         /** Supplies a strategical factory. */
274         private StrategicalPlannerFactorySupplierOD factorySupplier = StrategicalPlannerFactorySupplierOD.lmrs();
275 
276         /** Vehicle factory. */
277         private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;
278 
279         /**
280          * @param gtuTypeGenerator Generator&lt;GTUType&gt;; set gtuTypeGenerator.
281          * @return Factory; this factory for method chaining
282          */
283         public Factory setGtuTypeGenerator(final Generator<GTUType> gtuTypeGenerator)
284         {
285             this.gtuTypeGenerator = gtuTypeGenerator;
286             return this;
287         }
288 
289         /**
290          * @param templates Set&lt;TemplateGTUType&gt;; set templates.
291          * @return Factory; this factory for method chaining
292          */
293         public Factory setTemplates(final Set<TemplateGTUType> templates)
294         {
295             this.templates = templates;
296             return this;
297         }
298 
299         /**
300          * @param routeSupplier RouteGeneratorOD; set routeSupplier.
301          * @return Factory; this factory for method chaining
302          */
303         public Factory setRouteSupplier(final RouteGeneratorOD routeSupplier)
304         {
305             this.routeGenerator = routeSupplier;
306             return this;
307         }
308 
309         /**
310          * @param factorySupplier StrategicalPlannerFactorySupplierOD; set factorySupplier.
311          * @return Factory; this factory for method chaining
312          */
313         public Factory setFactorySupplier(final StrategicalPlannerFactorySupplierOD factorySupplier)
314         {
315             this.factorySupplier = factorySupplier;
316             return this;
317         }
318 
319         /**
320          * @param vehicleModelFactory VehicleModelFactory; set vehicleModelFactory.
321          * @return Factory; this factory for method chaining
322          */
323         public Factory setVehicleModelGenerator(final VehicleModelFactory vehicleModelFactory)
324         {
325             this.vehicleModelFactory = vehicleModelFactory;
326             return this;
327         }
328 
329         /**
330          * Creates the default GTU characteristics generator based on OD information.
331          * @return default GTU characteristics generator based on OD information
332          */
333         @SuppressWarnings("synthetic-access")
334         public DefaultGTUCharacteristicsGeneratorOD create()
335         {
336             return new DefaultGTUCharacteristicsGeneratorOD(this.gtuTypeGenerator, this.routeGenerator, this.templates,
337                     this.factorySupplier, this.vehicleModelFactory);
338         }
339     }
340 
341 }