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.djutils.exceptions.Throw;
8   import org.opentrafficsim.base.parameters.ParameterException;
9   import org.opentrafficsim.base.parameters.Parameters;
10  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
11  import org.opentrafficsim.road.gtu.lane.perception.PerceptionIterable;
12  import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
13  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
14  
15  /**
16   * Default implementation where desired speed and headway are pre-calculated for car-following.
17   * <p>
18   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
20   * </p>
21   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
22   */
23  public abstract class AbstractCarFollowingModel implements CarFollowingModel
24  {
25  
26      /** Desired headway model. */
27      private DesiredHeadwayModel desiredHeadwayModel;
28  
29      /** Desired speed model. */
30      private DesiredSpeedModel desiredSpeedModel;
31  
32      /**
33       * @param desiredHeadwayModel DesiredHeadwayModel; desired headway model
34       * @param desiredSpeedModel DesiredSpeedModel; desired speed model
35       */
36      public AbstractCarFollowingModel(final DesiredHeadwayModel desiredHeadwayModel, final DesiredSpeedModel desiredSpeedModel)
37      {
38          this.desiredHeadwayModel = desiredHeadwayModel;
39          this.desiredSpeedModel = desiredSpeedModel;
40      }
41  
42      /** {@inheritDoc} */
43      @Override
44      public final Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
45      {
46          Throw.whenNull(parameters, "Parameters may not be null.");
47          Throw.whenNull(speed, "Speed may not be null.");
48          return this.desiredHeadwayModel.desiredHeadway(parameters, speed);
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public final Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
54      {
55          Throw.whenNull(parameters, "Parameters may not be null.");
56          Throw.whenNull(speedInfo, "Speed limit info may not be null.");
57          return this.desiredSpeedModel.desiredSpeed(parameters, speedInfo);
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public final Acceleration followingAcceleration(final Parameters parameters, final Speed speed,
63              final SpeedLimitInfo speedLimitInfo, final PerceptionIterable<? extends Headway> leaders) throws ParameterException
64      {
65          Throw.whenNull(parameters, "Parameters may not be null.");
66          Throw.whenNull(speed, "Speed may not be null.");
67          Throw.whenNull(speedLimitInfo, "Speed limit info may not be null.");
68          Throw.whenNull(leaders, "Leaders may not be null.");
69          // Catch negative headway
70          if (!leaders.isEmpty() && leaders.first().getDistance().si <= 0)
71          {
72              return new Acceleration(Double.NEGATIVE_INFINITY, AccelerationUnit.SI);
73          }
74          // Forward to method with desired speed and headway predetermined by this car-following model.
75          Acceleration acc = followingAcceleration(parameters, speed, desiredSpeed(parameters, speedLimitInfo),
76                  desiredHeadway(parameters, speed), leaders);
77          return acc;
78      }
79  
80      /**
81       * Determination of car-following acceleration, possibly based on multiple leaders.
82       * @param parameters Parameters; parameters
83       * @param speed Speed; current speed
84       * @param desiredSpeed Speed; desired speed
85       * @param desiredHeadway Length; desired headway
86       * @param leaders PerceptionIterable&lt;? extends Headway&gt;; set of leader headways (guaranteed positive) and speeds,
87       *            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 }