AnticipationTrafficPerception.java

  1. package org.opentrafficsim.road.gtu.lane.perception.categories;

  2. import java.util.LinkedHashMap;
  3. import java.util.Map;

  4. import org.djunits.value.vdouble.scalar.LinearDensity;
  5. import org.djunits.value.vdouble.scalar.Speed;
  6. import org.djunits.value.vdouble.scalar.Time;
  7. import org.djutils.exceptions.Try;
  8. import org.opentrafficsim.base.TimeStampedObject;
  9. import org.opentrafficsim.base.parameters.ParameterException;
  10. import org.opentrafficsim.base.parameters.ParameterTypeLength;
  11. import org.opentrafficsim.base.parameters.ParameterTypes;
  12. import org.opentrafficsim.base.parameters.Parameters;
  13. import org.opentrafficsim.core.gtu.GTUException;
  14. import org.opentrafficsim.core.network.NetworkException;
  15. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  16. import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
  17. import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
  18. import org.opentrafficsim.road.gtu.lane.perception.categories.AnticipationSpeed.SpeedSet;
  19. import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;

  20. /**
  21.  * Traffic perception using neighbors perception. Speed is anticipated as in the LMRS. Density is simply distance of the
  22.  * farthest leader divided by the number of leaders.
  23.  * <p>
  24.  * Copyright (c) 2013-2020 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 13 mrt. 2018 <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 AnticipationTrafficPerception extends LaneBasedAbstractPerceptionCategory implements TrafficPerception
  33. {

  34.     /** */
  35.     private static final long serialVersionUID = 20180313L;

  36.     /** Look ahead parameter type. */
  37.     protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;

  38.     /** Last time speed was updated. */
  39.     private Time lastSpeedTime = null;

  40.     /** Anticipated speed by vehicles in the left lane. */
  41.     private Map<RelativeLane, Double> antFromLeft = new LinkedHashMap<>();

  42.     /** Anticipated speed by vehicles in the lane. */
  43.     private Map<RelativeLane, Double> antInLane = new LinkedHashMap<>();

  44.     /** Anticipated speed by vehicles in the right lane. */
  45.     private Map<RelativeLane, Double> antFromRight = new LinkedHashMap<>();

  46.     /** Anticipated speed combined. */
  47.     private Map<RelativeLane, Speed> speed = new LinkedHashMap<>();

  48.     /** Anticipated density combined. */
  49.     private Map<RelativeLane, TimeStampedObject<LinearDensity>> density = new LinkedHashMap<>();

  50.     /** Density collector. */
  51.     private static final AnticipationDensity DENSITY = new AnticipationDensity();

  52.     /**
  53.      * Constructor.
  54.      * @param perception LanePerception; perception
  55.      */
  56.     public AnticipationTrafficPerception(final LanePerception perception)
  57.     {
  58.         super(perception);
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public Speed getSpeed(final RelativeLane lane) throws ParameterException
  63.     {
  64.         Time now = Try.assign(() -> getTimestamp(), "");
  65.         if (this.lastSpeedTime == null || this.lastSpeedTime.si < now.si)
  66.         {
  67.             // due to lane interdependency, we clear all
  68.             this.antFromLeft.clear();
  69.             this.antInLane.clear();
  70.             this.antFromRight.clear();
  71.             this.speed.clear();
  72.             this.lastSpeedTime = now;
  73.         }
  74.         Speed vAnt = this.speed.get(lane);
  75.         if (vAnt == null)
  76.         {
  77.             LaneBasedGTU gtu = Try.assign(() -> getPerception().getGtu(), "");
  78.             Speed desiredSpeed = gtu.getDesiredSpeed();
  79.             vAnt = anticipationSpeed(lane, gtu.getParameters(),
  80.                     getPerception().getPerceptionCategoryOrNull(NeighborsPerception.class),
  81.                     getPerception().getPerceptionCategoryOrNull(InfrastructurePerception.class), desiredSpeed);
  82.             this.speed.put(lane, vAnt);
  83.         }
  84.         return vAnt;
  85.     }

  86.     /**
  87.      * Returns the anticipation speed in a lane. GTU's in adjacent lanes with their indicator towards the given lane are
  88.      * included in the evaluation.
  89.      * @param lane RelativeLane; lane to assess
  90.      * @param params Parameters; parameters
  91.      * @param neighbors NeighborsPerception; neighbors perception
  92.      * @param infra InfrastructurePerception; infrastructure perception
  93.      * @param desiredSpeed Speed; desired speed
  94.      * @return Speed; anticipation speed in lane
  95.      * @throws ParameterException on missing parameter
  96.      */
  97.     private Speed anticipationSpeed(final RelativeLane lane, final Parameters params, final NeighborsPerception neighbors,
  98.             final InfrastructurePerception infra, final Speed desiredSpeed) throws ParameterException
  99.     {
  100.         if (!this.antInLane.containsKey(lane))
  101.         {
  102.             anticipateSpeedFromLane(lane, params, neighbors, desiredSpeed);
  103.         }
  104.         double v = this.antInLane.get(lane);
  105.         if (infra.getCrossSection().contains(lane.getLeft()))
  106.         {
  107.             if (!this.antFromLeft.containsKey(lane))
  108.             {
  109.                 anticipateSpeedFromLane(lane.getLeft(), params, neighbors, desiredSpeed);
  110.             }
  111.             double fromLeft = this.antFromLeft.get(lane);
  112.             v = v < fromLeft ? v : fromLeft;
  113.         }
  114.         if (infra.getCrossSection().contains(lane.getRight()))
  115.         {
  116.             if (!this.antFromRight.containsKey(lane))
  117.             {
  118.                 anticipateSpeedFromLane(lane.getRight(), params, neighbors, desiredSpeed);
  119.             }
  120.             double fromRight = this.antFromRight.get(lane);
  121.             v = v < fromRight ? v : fromRight;
  122.         }
  123.         return Speed.instantiateSI(v);
  124.     }

  125.     /**
  126.      * Anticipate speed from the GTUs in one lane. This affects up to 3 lanes, all this information is stored.
  127.      * @param lane RelativeLane; lane to assess
  128.      * @param params Parameters; parameters
  129.      * @param neighbors NeighborsPerception; neighbors perception
  130.      * @param desiredSpeed Speed; desired speed
  131.      * @throws ParameterException on missing parameter
  132.      */
  133.     private void anticipateSpeedFromLane(final RelativeLane lane, final Parameters params, final NeighborsPerception neighbors,
  134.             final Speed desiredSpeed) throws ParameterException
  135.     {
  136.         AnticipationSpeed anticipationSpeed = new AnticipationSpeed(desiredSpeed, params.getParameter(LOOKAHEAD), lane);
  137.         SpeedSet speedSet = neighbors.getLeaders(lane).collect(anticipationSpeed);
  138.         this.antFromLeft.put(lane.getRight(), speedSet.getRight().si);
  139.         this.antInLane.put(lane, speedSet.getCurrent().si);
  140.         this.antFromRight.put(lane.getLeft(), speedSet.getLeft().si);
  141.     }

  142.     /** {@inheritDoc} */
  143.     @Override
  144.     public LinearDensity getDensity(final RelativeLane lane)
  145.     {
  146.         Time now = Try.assign(() -> getTimestamp(), "");
  147.         TimeStampedObject<LinearDensity> tK = this.density.get(lane);
  148.         if (tK == null || tK.getTimestamp().si < now.si)
  149.         {
  150.             LinearDensity k =
  151.                     getPerception().getPerceptionCategoryOrNull(NeighborsPerception.class).getLeaders(lane).collect(DENSITY);
  152.             this.density.put(lane, new TimeStampedObject<>(k, now));
  153.             return k;
  154.         }
  155.         else
  156.         {
  157.             return tK.getObject();
  158.         }
  159.     }

  160.     /** {@inheritDoc} */
  161.     @Override
  162.     public void updateAll() throws GTUException, NetworkException, ParameterException
  163.     {
  164.         // lazy evaluation
  165.     }

  166.     /** {@inheritDoc} */
  167.     @Override
  168.     public String toString()
  169.     {
  170.         return "AnticipationTrafficPerception [lastSpeedTime=" + this.lastSpeedTime + ", speed=" + this.speed + ", density="
  171.                 + this.density + "]";
  172.     }

  173. }