View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.following;
2   
3   import org.djunits.unit.AccelerationUnit;
4   import org.djunits.value.vdouble.scalar.Acceleration;
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djunits.value.vdouble.scalar.Speed;
7   import org.opentrafficsim.base.parameters.ParameterException;
8   import org.opentrafficsim.base.parameters.Parameters;
9   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
10  import org.opentrafficsim.road.gtu.lane.perception.PerceptionIterable;
11  import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
12  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
13  
14  import nl.tudelft.simulation.language.Throw;
15  
16  /**
17   * Default implementation where desired speed and headway are pre-calculated for car-following.
18   * <p>
19   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
21   * <p>
22   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 2016 <br>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public abstract class AbstractCarFollowingModel implements CarFollowingModel
26  {
27  
28      /** Desired headway model. */
29      private DesiredHeadwayModel desiredHeadwayModel;
30  
31      /** Desired speed model. */
32      private DesiredSpeedModel desiredSpeedModel;
33  
34      /**
35       * @param desiredHeadwayModel desired headway model
36       * @param desiredSpeedModel desired speed model
37       */
38      public AbstractCarFollowingModel(final DesiredHeadwayModel desiredHeadwayModel, final DesiredSpeedModel desiredSpeedModel)
39      {
40          this.desiredHeadwayModel = desiredHeadwayModel;
41          this.desiredSpeedModel = desiredSpeedModel;
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public final Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
47      {
48          Throw.whenNull(parameters, "Parameters may not be null.");
49          Throw.whenNull(speed, "Speed may not be null.");
50          return this.desiredHeadwayModel.desiredHeadway(parameters, speed);
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public final Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
56      {
57          Throw.whenNull(parameters, "Parameters may not be null.");
58          Throw.whenNull(speedInfo, "Speed limit info may not be null.");
59          return this.desiredSpeedModel.desiredSpeed(parameters, speedInfo);
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public final Acceleration followingAcceleration(final Parameters parameters, final Speed speed,
65              final SpeedLimitInfo speedLimitInfo, final PerceptionIterable<? extends Headway> leaders) throws ParameterException
66      {
67          Throw.whenNull(parameters, "Parameters may not be null.");
68          Throw.whenNull(speed, "Speed may not be null.");
69          Throw.whenNull(speedLimitInfo, "Speed limit info may not be null.");
70          Throw.whenNull(leaders, "Leaders may not be null.");
71          // Catch negative headway
72          if (!leaders.isEmpty() && leaders.first().getDistance().si <= 0)
73          {
74              return new Acceleration(Double.NEGATIVE_INFINITY, AccelerationUnit.SI);
75          }
76          // Forward to method with desired speed and headway predetermined by this car-following model.
77          return followingAcceleration(parameters, speed, desiredSpeed(parameters, speedLimitInfo),
78                  desiredHeadway(parameters, speed), leaders);
79      }
80  
81      /**
82       * Determination of car-following acceleration, possibly based on multiple leaders.
83       * @param parameters parameters
84       * @param speed current speed
85       * @param desiredSpeed desired speed
86       * @param desiredHeadway desired headway
87       * @param leaders set of leader headways (guaranteed positive) and speeds, ordered by headway (closest first)
88       * @return car-following acceleration
89       * @throws ParameterException if parameter exception occurs
90       */
91      protected abstract Acceleration followingAcceleration(Parameters parameters, Speed speed, Speed desiredSpeed,
92              Length desiredHeadway, PerceptionIterable<? extends Headway> leaders) throws ParameterException;
93  
94      /** {@inheritDoc} */
95      @SuppressWarnings("checkstyle:designforextension")
96      @Override
97      public String toString()
98      {
99          return getLongName();
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public final void init(final LaneBasedGTU gtu)
105     {
106         if (this.desiredHeadwayModel instanceof Initialisable)
107         {
108             ((Initialisable) this.desiredHeadwayModel).init(gtu);
109         }
110         if (this.desiredSpeedModel instanceof Initialisable)
111         {
112             ((Initialisable) this.desiredSpeedModel).init(gtu);
113         }
114     }
115 
116 }