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.LanePosition;
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-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
28   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
29   * </p>
30   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
31   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
32   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
33   */
34  public class TtcRoomChecker implements RoomChecker
35  {
36  
37      /** Time to collision. */
38      private final Duration ttc;
39  
40      /**
41       * Constructor.
42       * @param ttc Duration; time to collision
43       */
44      public TtcRoomChecker(final Duration ttc)
45      {
46          this.ttc = ttc;
47      }
48  
49      /** {@inheritDoc} */
50      @Override
51      public final Placement canPlace(final SortedSet<HeadwayGtu> leaders, final LaneBasedGtuCharacteristics characteristics,
52              final Duration since, final Set<LanePosition> initialPosition) throws NetworkException, GtuException
53      {
54          Speed speedLimit = initialPosition.iterator().next().getLane().getSpeedLimit(characteristics.getGtuType());
55          Speed desiredSpeedProxy = Speed.min(characteristics.getMaximumSpeed(), speedLimit);
56          if (leaders.isEmpty())
57          {
58              return new Placement(desiredSpeedProxy, initialPosition);
59          }
60          HeadwayGtu leader = leaders.first();
61          Speed speed = Speed.min(leader.getSpeed(), desiredSpeedProxy);
62          for (LanePosition dlp : initialPosition)
63          {
64              if (dlp.getLane().getType().isCompatible(characteristics.getGtuType()))
65              {
66                  speed = Speed.min(speed, dlp.getLane().getSpeedLimit(characteristics.getGtuType()));
67              }
68          }
69          if ((speed.le(leader.getSpeed()) || leader.getDistance().divide(speed.minus(leader.getSpeed())).gt(this.ttc)) && leader
70                  .getDistance().gt(speed.times(new Duration(1.0, DurationUnit.SI)).plus(new Length(3.0, LengthUnit.SI))))
71          {
72              return new Placement(speed, initialPosition);
73          }
74          return Placement.NO;
75      }
76  
77      /**
78       * Returns the TTC value.
79       * @return Duration; TTC value
80       */
81      public final Duration getTtc()
82      {
83          return this.ttc;
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public final String toString()
89      {
90          return "TTCRoomChecker [ttc=" + this.ttc + "]";
91      }
92  
93  }