View Javadoc
1   package org.opentrafficsim.road.gtu.generator;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djunits.value.vdouble.scalar.Speed;
5   import org.djutils.exceptions.Try;
6   import org.opentrafficsim.core.definitions.Defaults;
7   import org.opentrafficsim.core.gtu.GtuCharacteristics;
8   import org.opentrafficsim.core.gtu.GtuErrorHandler;
9   import org.opentrafficsim.core.gtu.GtuException;
10  import org.opentrafficsim.core.gtu.GtuType;
11  import org.opentrafficsim.core.network.NetworkException;
12  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGtuCharacteristics;
13  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
14  import org.opentrafficsim.road.network.RoadNetwork;
15  import org.opentrafficsim.road.network.lane.LanePosition;
16  
17  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
18  import nl.tudelft.simulation.jstats.streams.StreamInterface;
19  
20  /**
21   * Simple class to spawn GTUs.
22   * <p>
23   * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
25   * </p>
26   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
27   * @author Christoph Thees
28   */
29  public class GtuSpawner
30  {
31  
32      /** Use default NL GTU templates. */
33      private boolean useDefaultGtuTemplate = true;
34  
35      /** Random stream. */
36      private StreamInterface stream = new MersenneTwister(123L);
37  
38      /** Length over which no lane changes will happen. */
39      private Length noLaneChangeDistance = Length.instantiateSI(100.0);
40  
41      /** Instantaneous lane changes. */
42      private boolean instantaneousLaneChanges = false;
43  
44      /** Error handler. */
45      private GtuErrorHandler errorHandler = GtuErrorHandler.THROW;
46  
47      /**
48       * Sets the use of default NL GTU templates.
49       * @param useDefaultGtuTemplate use of default NL GTU templates.
50       * @return for method chaining.
51       */
52      public GtuSpawner setUseDefaultGtuTemplate(final boolean useDefaultGtuTemplate)
53      {
54          this.useDefaultGtuTemplate = useDefaultGtuTemplate;
55          return this;
56      }
57  
58      /**
59       * Sets random stream.
60       * @param stream random stream.
61       * @return for method chaining.
62       */
63      public GtuSpawner setStream(final StreamInterface stream)
64      {
65          this.stream = stream;
66          return this;
67      }
68  
69      /**
70       * Sets no lane change distance.
71       * @param noLaneChangeDistance no lane change distance.
72       * @return for method chaining.
73       */
74      public GtuSpawner setNoLaneChangeDistance(final Length noLaneChangeDistance)
75      {
76          this.noLaneChangeDistance = noLaneChangeDistance;
77          return this;
78      }
79  
80      /**
81       * Sets instantaneous lane changes.
82       * @param instantaneousLaneChanges instantaneous lane changes.
83       * @return for method chaining.
84       */
85      public GtuSpawner setInstantaneousLaneChanges(final boolean instantaneousLaneChanges)
86      {
87          this.instantaneousLaneChanges = instantaneousLaneChanges;
88          return this;
89      }
90  
91      /**
92       * Sets the error handler.
93       * @param errorHandler error handler.
94       * @return for method chaining.
95       */
96      public GtuSpawner setErrorHandler(final GtuErrorHandler errorHandler)
97      {
98          this.errorHandler = errorHandler;
99          return this;
100     }
101 
102     /**
103      * Create a single GTU.
104      * @param id id.
105      * @param templateGtuType characteristics.
106      * @param network network.
107      * @param speed speed.
108      * @param position position.
109      * @throws GtuException when initial GTU values are not correct
110      * @throws NetworkException when the GTU cannot be placed on the given lane
111      */
112     public void spawnGtu(final String id, final LaneBasedGtuCharacteristics templateGtuType, final RoadNetwork network,
113             final Speed speed, final LanePosition position) throws GtuException, NetworkException
114     {
115 
116         if (this.useDefaultGtuTemplate)
117         {
118             GtuType.registerTemplateSupplier(templateGtuType.getGtuType(), Defaults.NL);
119         }
120         GtuCharacteristics defaultCharacteristics =
121                 Try.assign(() -> GtuType.defaultCharacteristics(templateGtuType.getGtuType(), network, this.stream),
122                         "Failed getting default Characteristics");
123 
124         LaneBasedGtu gtu =
125                 new LaneBasedGtu(id, templateGtuType.getGtuType(), templateGtuType.getLength(), templateGtuType.getWidth(),
126                         defaultCharacteristics.getMaximumSpeed(), templateGtuType.getLength().divide(2), network);
127 
128         gtu.setMaximumAcceleration(defaultCharacteristics.getMaximumAcceleration());
129         gtu.setMaximumDeceleration(defaultCharacteristics.getMaximumDeceleration());
130         gtu.setVehicleModel(templateGtuType.getVehicleModel());
131         gtu.setNoLaneChangeDistance(this.noLaneChangeDistance);
132         gtu.setInstantaneousLaneChange(this.instantaneousLaneChanges);
133         gtu.setErrorHandler(this.errorHandler);
134 
135         gtu.init(templateGtuType.getStrategicalPlannerFactory().create(gtu, templateGtuType.getRoute(),
136                 templateGtuType.getOrigin(), templateGtuType.getDestination()), position, speed);
137     }
138 
139 }