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