View Javadoc
1   package org.opentrafficsim.road.gtu.generator;
2   
3   import java.util.Set;
4   import java.util.SortedSet;
5   
6   import org.djunits.unit.DurationUnit;
7   import org.djunits.unit.LengthUnit;
8   import org.djunits.value.vdouble.scalar.Duration;
9   import org.djunits.value.vdouble.scalar.Length;
10  import org.djunits.value.vdouble.scalar.Speed;
11  import org.opentrafficsim.core.gtu.GTUException;
12  import org.opentrafficsim.core.network.NetworkException;
13  import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator.Placement;
14  import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator.RoomChecker;
15  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristics;
16  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
17  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
18  
19  /**
20   * Room checker based on time-to-collision. The room is considered ok if:
21   * <ol>
22   * <li>The headway is larger than speed*1.0s + 3m</li>
23   * <li>time-to-collision &lt; value
24   * </ol>
25   * where 'value' is a given value in the constructor.
26   * <p>
27   * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
28   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
29   * <p>
30   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 17 nov. 2016 <br>
31   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
32   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
33   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
34   */
35  public class TTCRoomChecker implements RoomChecker
36  {
37  
38      /** Time to collision. */
39      private final Duration ttc;
40  
41      /**
42       * Constructor.
43       * @param ttc Duration; time to collision
44       */
45      public TTCRoomChecker(final Duration ttc)
46      {
47          this.ttc = ttc;
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public final Placement canPlace(final SortedSet<HeadwayGTU> leaders, final LaneBasedGTUCharacteristics characteristics,
53              final Duration since, final Set<DirectedLanePosition> initialPosition) throws NetworkException, GTUException
54      {
55          Speed speedLimit = initialPosition.iterator().next().getLane().getSpeedLimit(characteristics.getGTUType());
56          Speed desiredSpeedProxy = Speed.min(characteristics.getMaximumSpeed(), speedLimit);
57          if (leaders.isEmpty())
58          {
59              return new Placement(desiredSpeedProxy, initialPosition);
60          }
61          HeadwayGTU leader = leaders.first();
62          Speed speed = Speed.min(leader.getSpeed(), desiredSpeedProxy);
63          for (DirectedLanePosition dlp : initialPosition)
64          {
65              if (dlp.getLane().getLaneType().isCompatible(characteristics.getGTUType(), dlp.getGtuDirection()))
66              {
67                  speed = Speed.min(speed, dlp.getLane().getSpeedLimit(characteristics.getGTUType()));
68              }
69          }
70          if ((speed.le(leader.getSpeed()) || leader.getDistance().divide(speed.minus(leader.getSpeed())).gt(this.ttc))
71                  && leader.getDistance()
72                          .gt(speed.times(new Duration(1.0, DurationUnit.SI)).plus(new Length(3.0, LengthUnit.SI))))
73          {
74              return new Placement(speed, initialPosition);
75          }
76          return Placement.NO;
77      }
78      
79      /**
80       * Returns the TTC value.
81       * @return Duration; TTC value
82       */
83      public final Duration getTtc()
84      {
85          return this.ttc;
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public final String toString()
91      {
92          return "TTCRoomChecker [ttc=" + this.ttc + "]";
93      }
94  
95  }