View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.categories;
2   
3   import org.djunits.value.vdouble.scalar.Acceleration;
4   import org.djunits.value.vdouble.scalar.Duration;
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djunits.value.vdouble.scalar.Speed;
7   
8   /**
9    * Form of anticipation.
10   * <p>
11   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
13   * <p>
14   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 24 feb. 2017 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
18   */
19  public interface Anticipation
20  {
21  
22      /** Assume no anticipation. */
23      public final static Anticipation NONE = new Anticipation()
24      {
25          @Override
26          public NeighborTriplet anticipate(final NeighborTriplet neighborTriplet, final Duration duration,
27                  final Length traveledDistance)
28          {
29              return neighborTriplet;
30          }
31  
32          @Override
33          public Length egoAnticipation(final Speed speed, final Acceleration acceleration, final Duration duration)
34          {
35              return Length.ZERO;
36          }
37      };
38  
39      /** Assume constant speed. */
40      public final static Anticipation CONSTANT_SPEED = new Anticipation()
41      {
42          @Override
43          public NeighborTriplet anticipate(final NeighborTriplet neighborTriplet, final Duration duration,
44                  final Length traveledDistance)
45          {
46              return new NeighborTriplet(
47                      neighborTriplet.getHeadway().plus(neighborTriplet.getSpeed().multiplyBy(duration)).minus(traveledDistance),
48                      neighborTriplet.getSpeed(), neighborTriplet.getAcceleration());
49          }
50  
51          @Override
52          public Length egoAnticipation(final Speed speed, final Acceleration acceleration, final Duration duration)
53          {
54              return speed.multiplyBy(duration);
55          }
56      };
57  
58      /** Assume constant acceleration. */
59      public final static Anticipation CONSTANT_ACCELERATION = new Anticipation()
60      {
61          @Override
62          public NeighborTriplet anticipate(final NeighborTriplet neighborTriplet, final Duration duration,
63                  final Length traveledDistance)
64          {
65              if (neighborTriplet.getSpeed().si < -neighborTriplet.getAcceleration().si * duration.si)
66              {
67                  // to stand still
68                  double t = neighborTriplet.getSpeed().si / -neighborTriplet.getAcceleration().si;
69                  double dx = neighborTriplet.getSpeed().si * t + .5 * neighborTriplet.getAcceleration().si * t * t;
70                  return new NeighborTriplet(Length.createSI(neighborTriplet.getHeadway().si + dx - traveledDistance.si),
71                          Speed.ZERO, Acceleration.ZERO);
72              }
73              double dx = neighborTriplet.getSpeed().si * duration.si
74                      + .5 * neighborTriplet.getAcceleration().si * duration.si * duration.si;
75              double dv = neighborTriplet.getAcceleration().si * duration.si;
76              return new NeighborTriplet(Length.createSI(neighborTriplet.getHeadway().si + dx - traveledDistance.si),
77                      Speed.createSI(neighborTriplet.getSpeed().si + dv), neighborTriplet.getAcceleration());
78          }
79  
80          @Override
81          public Length egoAnticipation(final Speed speed, final Acceleration acceleration, final Duration duration)
82          {
83              return speed.multiplyBy(duration);
84          }
85      };
86  
87      /**
88       * Anticipate movement.
89       * @param neighborTriplet headway, speed and acceleration
90       * @param duration duration
91       * @param traveledDistance distance the subject vehicle traveled during the anticipation time
92       * @return anticipated info
93       */
94      NeighborTriplet anticipate(NeighborTriplet neighborTriplet, Duration duration, Length traveledDistance);
95  
96      /**
97       * Anticipate own movement.
98       * @param speed current speed
99       * @param acceleration current acceleration
100      * @param duration anticipation time
101      * @return anticipated distance traveled
102      */
103     Length egoAnticipation(Speed speed, Acceleration acceleration, Duration duration);
104 
105     /**
106      * Results from anticipation.
107      * <p>
108      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
109      * <br>
110      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
111      * <p>
112      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 24 feb. 2017 <br>
113      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
114      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
115      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
116      */
117     public static class NeighborTriplet
118     {
119 
120         /** Headway. */
121         private final Length headway;
122 
123         /** Speed. */
124         private final Speed speed;
125 
126         /** Acceleration. */
127         private final Acceleration acceleration;
128 
129         /**
130          * @param headway headway
131          * @param speed speed
132          * @param acceleration acceleration
133          */
134         NeighborTriplet(final Length headway, final Speed speed, final Acceleration acceleration)
135         {
136             this.headway = headway;
137             this.speed = speed;
138             this.acceleration = acceleration;
139         }
140 
141         /**
142          * @return headway.
143          */
144         public Length getHeadway()
145         {
146             return this.headway;
147         }
148 
149         /**
150          * @return speed.
151          */
152         public Speed getSpeed()
153         {
154             return this.speed;
155         }
156 
157         /**
158          * @return acceleration.
159          */
160         public Acceleration getAcceleration()
161         {
162             return this.acceleration;
163         }
164 
165         /** {@inheritDoc} */
166         @Override
167         public final String toString()
168         {
169             return "NeighborTriplet [headway=" + this.headway + ", speed=" + this.speed + ", acceleration=" + this.acceleration
170                     + "]";
171         }
172     }
173 }