View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.categories.neighbors;
2   
3   import org.djunits.value.vdouble.scalar.Duration;
4   import org.djunits.value.vdouble.scalar.Length;
5   import org.djunits.value.vdouble.scalar.Time;
6   import org.djutils.exceptions.Throw;
7   import org.opentrafficsim.base.parameters.ParameterException;
8   import org.opentrafficsim.base.parameters.ParameterTypes;
9   import org.opentrafficsim.core.gtu.GTUException;
10  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
11  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
12  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTUPerceived;
13  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTUReal;
14  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTURealCopy;
15  
16  /**
17   * Whether a GTU needs to be wrapped, or information should be copied for later and unaltered use.
18   * <p>
19   * Copyright (c) 2013-2019 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/node/13">OpenTrafficSim License</a>.
21   * <p>
22   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 24 mrt. 2017 <br>
23   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
25   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
26   */
27  public interface HeadwayGtuType
28  {
29  
30      /** The GTU is wrapped, and info is taken directly from it. */
31      HeadwayGtuType WRAP = new HeadwayGtuType()
32      {
33          @Override
34          public HeadwayGTUReal createDownstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
35                  final Length distance) throws GTUException
36          {
37              return new HeadwayGTUReal(perceivedGtu, distance, true);
38          }
39  
40          @Override
41          public HeadwayGTUReal createUpstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
42                  final Length distance) throws GTUException
43          {
44              return new HeadwayGTUReal(perceivedGtu, distance, true);
45          }
46  
47          @Override
48          public HeadwayGTUReal createParallelGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
49                  final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
50          {
51              return new HeadwayGTUReal(perceivedGtu, overlapFront, overlap, overlapRear, true);
52          }
53      };
54  
55      /** Info regarding the GTU is copied. */
56      HeadwayGtuType COPY = new HeadwayGtuType()
57      {
58          @Override
59          public HeadwayGTURealCopy createDownstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
60                  final Length distance) throws GTUException
61          {
62              return new HeadwayGTURealCopy(perceivedGtu, distance);
63          }
64  
65          @Override
66          public HeadwayGTURealCopy createUpstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
67                  final Length distance) throws GTUException
68          {
69              return new HeadwayGTURealCopy(perceivedGtu, distance);
70          }
71  
72          @Override
73          public HeadwayGTURealCopy createParallelGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
74                  final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
75          {
76              return new HeadwayGTURealCopy(perceivedGtu, overlapFront, overlap, overlapRear);
77          }
78      };
79  
80      /**
81       * Creates a headway object from a GTU, downstream or upstream. The default implementation figures out from possible
82       * negative distance whether a parallel GTU should be created.
83       * @param perceivingGtu LaneBasedGTU; perceiving GTU
84       * @param perceivedGtu LaneBasedGTU; perceived GTU
85       * @param distance Length; distance
86       * @param downstream boolean; downstream (or upstream) neighbor
87       * @return headway object from a gtu
88       * @throws GTUException when headway object cannot be created
89       * @throws ParameterException on invalid parameter value or missing parameter
90       */
91      default HeadwayGTU createHeadwayGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length distance,
92              boolean downstream) throws GTUException, ParameterException
93      {
94          if (distance.ge0())
95          {
96              if (downstream)
97              {
98                  return createDownstreamGtu(perceivingGtu, perceivedGtu, distance);
99              }
100             return createUpstreamGtu(perceivingGtu, perceivedGtu, distance);
101         }
102         Throw.when(-distance.si > perceivingGtu.getLength().si + perceivedGtu.getLength().si, IllegalStateException.class,
103                 "A GTU that is supposedly %s is actually %s.", downstream ? "downstream" : "upstream",
104                 downstream ? "upstream" : "downstream");
105         Length overlapRear = distance.plus(perceivingGtu.getLength());
106         Length overlap = distance.neg();
107         Length overlapFront = distance.plus(perceivedGtu.getLength());
108         if (overlapRear.lt0())
109         {
110             overlap = overlap.plus(overlapRear);
111         }
112         if (overlapFront.lt0())
113         {
114             overlap = overlap.plus(overlapFront);
115         }
116         return createParallelGtu(perceivingGtu, perceivedGtu, overlapFront, overlap, overlapRear);
117     }
118 
119     /**
120      * Creates a headway object from a GTU, downstream.
121      * @param perceivingGtu LaneBasedGTU; perceiving GTU
122      * @param perceivedGtu LaneBasedGTU; perceived GTU
123      * @param distance Length; distance
124      * @return headway object from a gtu
125      * @throws GTUException when headway object cannot be created
126      * @throws ParameterException on invalid parameter value or missing parameter
127      */
128     HeadwayGTU createDownstreamGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length distance)
129             throws GTUException, ParameterException;
130 
131     /**
132      * Creates a headway object from a GTU, downstream.
133      * @param perceivingGtu LaneBasedGTU; perceiving GTU
134      * @param perceivedGtu LaneBasedGTU; perceived GTU
135      * @param distance Length; distance
136      * @return headway object from a gtu
137      * @throws GTUException when headway object cannot be created
138      * @throws ParameterException on invalid parameter value or missing parameter
139      */
140     HeadwayGTU createUpstreamGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length distance)
141             throws GTUException, ParameterException;
142 
143     /**
144      * Creates a headway object from a GTU, parallel.
145      * @param perceivingGtu LaneBasedGTU; perceiving GTU
146      * @param perceivedGtu LaneBasedGTU; perceived GTU
147      * @param overlapFront Length; front overlap
148      * @param overlap Length; overlap
149      * @param overlapRear Length; rear overlap
150      * @return headway object from a gtu
151      * @throws GTUException when headway object cannot be created
152      */
153     HeadwayGTU createParallelGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length overlapFront, Length overlap,
154             Length overlapRear) throws GTUException;
155 
156     /**
157      * Class for perceived neighbors. Adjacent neighbors are perceived exactly.
158      * <p>
159      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
160      * <br>
161      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
162      * <p>
163      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 6 apr. 2018 <br>
164      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
165      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
166      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
167      */
168     class PerceivedHeadwayGtuType implements HeadwayGtuType
169     {
170 
171         /** Estimation. */
172         private final Estimation estimation;
173 
174         /** Anticipation. */
175         private final Anticipation anticipation;
176 
177         /** Last update time. */
178         private Time updateTime = null;
179 
180         /** Reaction time at update time. */
181         private Duration tr;
182 
183         /** Historical moment considered at update time. */
184         private Time when;
185 
186         /** Traveled distance during reaction time at update time. */
187         private Length traveledDistance;
188 
189         /**
190          * Constructor.
191          * @param estimation Estimation; estimation
192          * @param anticipation Anticipation; anticipation
193          */
194         public PerceivedHeadwayGtuType(final Estimation estimation, final Anticipation anticipation)
195         {
196             this.estimation = estimation;
197             this.anticipation = anticipation;
198         }
199 
200         /** {@inheritDoc} */
201         @Override
202         public HeadwayGTU createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
203                 final Length distance, final boolean downstream) throws GTUException, ParameterException
204         {
205             Time now = perceivedGtu.getSimulator().getSimulatorTime();
206             if (this.updateTime == null || now.si > this.updateTime.si)
207             {
208                 this.updateTime = now;
209                 this.tr = perceivingGtu.getParameters().getParameter(ParameterTypes.TR);
210                 Time whenTemp = now.minus(this.tr);
211                 if (this.when == null || (this.when != null && whenTemp.si > this.when.si))
212                 {
213                     // never go backwards in time if the reaction time increases
214                     this.when = whenTemp;
215                 }
216                 this.traveledDistance = perceivingGtu.getOdometer().minus(perceivingGtu.getOdometer(this.when));
217             }
218             NeighborTriplet triplet = this.estimation.estimate(perceivingGtu, perceivedGtu, distance, downstream, this.when);
219             triplet = this.anticipation.anticipate(triplet, this.tr, this.traveledDistance, downstream);
220             return new HeadwayGTUPerceived(perceivedGtu, triplet.getHeadway(), triplet.getSpeed(), triplet.getAcceleration());
221         }
222 
223         /** {@inheritDoc} */
224         @Override
225         public HeadwayGTU createDownstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
226                 final Length distance) throws GTUException, ParameterException
227         {
228             return createHeadwayGtu(perceivingGtu, perceivedGtu, distance, true);
229         }
230 
231         /** {@inheritDoc} */
232         @Override
233         public HeadwayGTU createUpstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
234                 final Length distance) throws GTUException, ParameterException
235         {
236             return createHeadwayGtu(perceivingGtu, perceivedGtu, distance, false);
237         }
238 
239         /** {@inheritDoc} */
240         @Override
241         public HeadwayGTU createParallelGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
242                 final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
243         {
244             return new HeadwayGTUPerceived(perceivedGtu, overlapFront, overlap, overlapRear, perceivedGtu.getSpeed(),
245                     perceivedGtu.getAcceleration());
246         }
247 
248     }
249 
250 }