View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;
2   
3   import static org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Tailgating.socialPressure;
4   
5   import org.djunits.value.vdouble.scalar.Duration;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djunits.value.vdouble.scalar.Speed;
8   import org.djutils.exceptions.Try;
9   import org.opentrafficsim.base.parameters.ParameterException;
10  import org.opentrafficsim.base.parameters.ParameterTypeDouble;
11  import org.opentrafficsim.base.parameters.ParameterTypes;
12  import org.opentrafficsim.base.parameters.Parameters;
13  import org.opentrafficsim.base.parameters.constraint.ConstraintInterface;
14  import org.opentrafficsim.core.gtu.perception.EgoPerception;
15  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
16  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
17  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
18  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
19  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
20  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
21  
22  /**
23   * Interface for LMRS tailgating behavior.
24   * <p>
25   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
27   * <p>
28   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 7 mrt. 2018 <br>
29   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
30   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
31   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
32   */
33  public interface Tailgating
34  {
35  
36      /** Social pressure applied to the leader. */
37      ParameterTypeDouble RHO = new ParameterTypeDouble("rho", "Social pressure", 0.0, ConstraintInterface.UNITINTERVAL);
38  
39      /** No tailgating. */
40      Tailgating NONE = new Tailgating()
41      {
42          /** {@inheritDoc} */
43          @Override
44          public void tailgate(final LanePerception perception, final Parameters parameters)
45          {
46              //
47          }
48      };
49  
50      /** No tailgating, but social pressure exists. */
51      Tailgating RHO_ONLY = new Tailgating()
52      {
53          /** {@inheritDoc} */
54          @Override
55          public void tailgate(final LanePerception perception, final Parameters parameters)
56          {
57              PerceptionCollectable<HeadwayGTU, LaneBasedGTU> leaders =
58                      perception.getPerceptionCategoryOrNull(NeighborsPerception.class).getLeaders(RelativeLane.CURRENT);
59              if (leaders == null || leaders.isEmpty())
60              {
61                  return;
62              }
63              try
64              {
65                  Speed speed = perception.getPerceptionCategoryOrNull(EgoPerception.class).getSpeed();
66                  Speed vCong = parameters.getParameter(ParameterTypes.VCONG);
67                  Length x0 = parameters.getParameter(ParameterTypes.LOOKAHEAD);
68                  Speed vGain = parameters.getParameter(LmrsParameters.VGAIN);
69                  HeadwayGTU leader = leaders.first();
70                  Speed desiredSpeed = Try.assign(() -> perception.getGtu().getDesiredSpeed(), "Could not obtain the GTU.");
71                  double rho = socialPressure(speed, vCong, desiredSpeed, leader.getSpeed(), vGain, leader.getDistance(), x0);
72                  parameters.setParameter(RHO, rho);
73              }
74              catch (ParameterException exception)
75              {
76                  throw new RuntimeException("Could not obtain or set parameter value.", exception);
77              }
78          }
79      };
80  
81      /** Tailgating based on speed pressure. */
82      Tailgating PRESSURE = new Tailgating()
83      {
84          /** {@inheritDoc} */
85          @Override
86          public void tailgate(final LanePerception perception, final Parameters parameters)
87          {
88              PerceptionCollectable<HeadwayGTU, LaneBasedGTU> leaders =
89                      perception.getPerceptionCategoryOrNull(NeighborsPerception.class).getLeaders(RelativeLane.CURRENT);
90              if (leaders == null || leaders.isEmpty())
91              {
92                  return;
93              }
94              try
95              {
96                  Speed speed = perception.getPerceptionCategoryOrNull(EgoPerception.class).getSpeed();
97                  Speed vCong = parameters.getParameter(ParameterTypes.VCONG);
98                  Duration t = parameters.getParameter(ParameterTypes.T);
99                  Duration tMin = parameters.getParameter(ParameterTypes.TMIN);
100                 Duration tMax = parameters.getParameter(ParameterTypes.TMAX);
101                 Length x0 = parameters.getParameter(ParameterTypes.LOOKAHEAD);
102                 Speed vGain = parameters.getParameter(LmrsParameters.VGAIN);
103                 HeadwayGTU leader = leaders.first();
104                 Speed desiredSpeed = Try.assign(() -> perception.getGtu().getDesiredSpeed(), "Could not obtain the GTU.");
105                 double rho = socialPressure(speed, vCong, desiredSpeed, leader.getSpeed(), vGain, leader.getDistance(), x0);
106                 parameters.setParameter(RHO, rho);
107                 double tNew = rho * tMin.si + (1.0 - rho) * tMax.si;
108                 if (tNew < t.si)
109                 {
110                     parameters.setParameter(ParameterTypes.T, Duration.createSI(tNew));
111                 }
112             }
113             catch (ParameterException exception)
114             {
115                 throw new RuntimeException("Could not obtain or set parameter value.", exception);
116             }
117         }
118     };
119 
120     /**
121      * Returns a normalized social pressure, equal to (vDesired - vLead) / vGain.
122      * @param speed Speed; speed
123      * @param vCong Speed; speed indicating congestion
124      * @param desiredSpeed Speed; desired speed
125      * @param leaderSpeed Speed; leader speed
126      * @param vGain Speed; vGain parameter
127      * @param headway Length; headway to the leader
128      * @param x0 Length; anticipation distance
129      * @return normalized social pressure
130      */
131     static double socialPressure(final Speed speed, final Speed vCong, final Speed desiredSpeed, final Speed leaderSpeed,
132             final Speed vGain, final Length headway, final Length x0)
133     {
134         double dv = desiredSpeed.si - leaderSpeed.si;
135         if (dv < 0)
136         {
137             return 0.0;
138         }
139         return 1.0 - Math.exp(-(dv / vGain.si) * (1.0 - (headway.si / x0.si)));
140     }
141 
142     /**
143      * Apply tailgating.
144      * @param perception LanePerception; perception
145      * @param parameters Parameters; parameters
146      */
147     void tailgate(LanePerception perception, Parameters parameters);
148 
149 }