TTCRoomChecker.java

  1. package org.opentrafficsim.road.gtu.generator;

  2. import java.util.Set;
  3. import java.util.SortedSet;

  4. import org.djunits.unit.DurationUnit;
  5. import org.djunits.unit.LengthUnit;
  6. import org.djunits.value.vdouble.scalar.Duration;
  7. import org.djunits.value.vdouble.scalar.Length;
  8. import org.djunits.value.vdouble.scalar.Speed;
  9. import org.opentrafficsim.core.gtu.GTUException;
  10. import org.opentrafficsim.core.network.NetworkException;
  11. import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator.Placement;
  12. import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator.RoomChecker;
  13. import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristics;
  14. import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
  15. import org.opentrafficsim.road.network.lane.DirectedLanePosition;

  16. /**
  17.  * Room checker based on time-to-collision. The room is considered ok if:
  18.  * <ol>
  19.  * <li>The headway is larger than speed*1.0s + 3m</li>
  20.  * <li>time-to-collision &lt; value
  21.  * </ol>
  22.  * where 'value' is a given value in the constructor.
  23.  * <p>
  24.  * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  25.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  26.  * <p>
  27.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 17 nov. 2016 <br>
  28.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  29.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  30.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  31.  */
  32. public class TTCRoomChecker implements RoomChecker
  33. {

  34.     /** Time to collision. */
  35.     private final Duration ttc;

  36.     /**
  37.      * Constructor.
  38.      * @param ttc Duration; time to collision
  39.      */
  40.     public TTCRoomChecker(final Duration ttc)
  41.     {
  42.         this.ttc = ttc;
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public final Placement canPlace(final SortedSet<HeadwayGTU> leaders, final LaneBasedGTUCharacteristics characteristics,
  47.             final Duration since, final Set<DirectedLanePosition> initialPosition) throws NetworkException, GTUException
  48.     {
  49.         Speed speedLimit = initialPosition.iterator().next().getLane().getSpeedLimit(characteristics.getGTUType());
  50.         Speed desiredSpeedProxy = Speed.min(characteristics.getMaximumSpeed(), speedLimit);
  51.         if (leaders.isEmpty())
  52.         {
  53.             return new Placement(desiredSpeedProxy, initialPosition);
  54.         }
  55.         HeadwayGTU leader = leaders.first();
  56.         Speed speed = Speed.min(leader.getSpeed(), desiredSpeedProxy);
  57.         for (DirectedLanePosition dlp : initialPosition)
  58.         {
  59.             if (dlp.getLane().getLaneType().isCompatible(characteristics.getGTUType(), dlp.getGtuDirection()))
  60.             {
  61.                 speed = Speed.min(speed, dlp.getLane().getSpeedLimit(characteristics.getGTUType()));
  62.             }
  63.         }
  64.         if ((speed.le(leader.getSpeed()) || leader.getDistance().divideBy(speed.minus(leader.getSpeed())).gt(this.ttc))
  65.                 && leader.getDistance()
  66.                         .gt(speed.multiplyBy(new Duration(1.0, DurationUnit.SI)).plus(new Length(3.0, LengthUnit.SI))))
  67.         {
  68.             return new Placement(speed, initialPosition);
  69.         }
  70.         return Placement.NO;
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public final String toString()
  75.     {
  76.         return "TTCRoomChecker [ttc=" + this.ttc + "]";
  77.     }

  78. }