View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.categories;
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.opentrafficsim.base.parameters.ParameterException;
7   import org.opentrafficsim.base.parameters.ParameterTypes;
8   import org.opentrafficsim.core.gtu.GTUException;
9   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
10  import org.opentrafficsim.road.gtu.lane.perception.categories.Anticipation.NeighborTriplet;
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-2018 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      public final static HeadwayGtuType WRAP = new HeadwayGtuType()
32      {
33          @Override
34          public HeadwayGTUReal createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
35                  final Length distance, final boolean downstream) throws GTUException
36          {
37              return new HeadwayGTUReal(perceivedGtu, distance, true);
38          }
39  
40          @Override
41          public HeadwayGTUReal createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
42                  final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
43          {
44              return new HeadwayGTUReal(perceivedGtu, overlapFront, overlap, overlapRear, true);
45          }
46      };
47  
48      /** Info regarding the GTU is copied. */
49      public final static HeadwayGtuType COPY = new HeadwayGtuType()
50      {
51          @Override
52          public HeadwayGTURealCopy createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
53                  final Length distance, final boolean downstream) throws GTUException
54          {
55              return new HeadwayGTURealCopy(perceivedGtu, distance);
56          }
57  
58          @Override
59          public HeadwayGTURealCopy createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
60                  final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
61          {
62              return new HeadwayGTURealCopy(perceivedGtu, overlapFront, overlap, overlapRear);
63          }
64      };
65  
66      /**
67       * Creates a headway object from a GTU, downstream or upstream.
68       * @param perceivingGtu perceiving GTU
69       * @param perceivedGtu perceived GTU
70       * @param distance distance
71       * @param downstream downstream (or upstream) neighbor
72       * @return headway object from a gtu
73       * @throws GTUException when headway object cannot be created
74       * @throws ParameterException on invalid parameter value or missing parameter
75       */
76      HeadwayGTU createHeadwayGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length distance, boolean downstream)
77              throws GTUException, ParameterException;
78  
79      /**
80       * Creates a headway object from a GTU, parallel.
81       * @param perceivingGtu perceiving GTU
82       * @param perceivedGtu perceived GTU
83       * @param overlapFront front overlap
84       * @param overlap overlap
85       * @param overlapRear rear overlap
86       * @return headway object from a gtu
87       * @throws GTUException when headway object cannot be created
88       */
89      HeadwayGTU createHeadwayGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length overlapFront, Length overlap,
90              Length overlapRear) throws GTUException;
91  
92      /**
93       * Class for perceived neighbors. Adjacent neighbors are perceived exactly.
94       * <p>
95       * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
96       * <br>
97       * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
98       * <p>
99       * @version $Revision$, $LastChangedDate$, by $Author$, initial version 6 apr. 2018 <br>
100      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
101      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
102      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
103      */
104     class PerceivedHeadwayGtuType implements HeadwayGtuType
105     {
106 
107         /** Estimation. */
108         private final Estimation estimation;
109 
110         /** Anticipation. */
111         private final Anticipation anticipation;
112 
113         /** Last update time. */
114         private Time updateTime = null;
115 
116         /** Reaction time at update time. */
117         private Duration tr;
118 
119         /** Historical moment considered at update time. */
120         private Time when;
121 
122         /** Traveled distance during reaction time at update time. */
123         private Length traveledDistance;
124 
125         /**
126          * Constructor.
127          * @param estimation Estimation; estimation
128          * @param anticipation Anticipation; anticipation
129          */
130         public PerceivedHeadwayGtuType(final Estimation estimation, final Anticipation anticipation)
131         {
132             this.estimation = estimation;
133             this.anticipation = anticipation;
134         }
135 
136         /** {@inheritDoc} */
137         @Override
138         public HeadwayGTU createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
139                 final Length distance, final boolean downstream) throws GTUException, ParameterException
140         {
141             Time now = perceivedGtu.getSimulator().getSimulatorTime();
142             if (this.updateTime == null || now.si > this.updateTime.si)
143             {
144                 this.updateTime = now;
145                 this.tr = perceivingGtu.getParameters().getParameter(ParameterTypes.TR);
146                 Time whenTemp = now.minus(this.tr);
147                 if (this.when == null || (this.when != null && whenTemp.si > this.when.si))
148                 {
149                     // never go backwards in time if the reaction time increases
150                     this.when = whenTemp;
151                 }
152                 this.traveledDistance = perceivingGtu.getOdometer().minus(perceivingGtu.getOdometer(this.when));
153             }
154             NeighborTriplet triplet = this.estimation.estimate(perceivingGtu, perceivedGtu, distance, downstream, this.when);
155             triplet = this.anticipation.anticipate(triplet, this.tr, this.traveledDistance);
156             return new HeadwayGTUPerceived(perceivedGtu, triplet.getHeadway(), triplet.getSpeed(), triplet.getAcceleration());
157         }
158 
159         /** {@inheritDoc} */
160         @Override
161         public HeadwayGTU createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
162                 final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
163         {
164             return new HeadwayGTUPerceived(perceivedGtu, overlapFront, overlap, overlapRear, perceivedGtu.getSpeed(),
165                     perceivedGtu.getAcceleration());
166         }
167 
168     }
169 
170 }