AnticipationDensity.java

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

  2. import java.util.function.Supplier;

  3. import org.djunits.value.vdouble.scalar.Length;
  4. import org.djunits.value.vdouble.scalar.LinearDensity;
  5. import org.opentrafficsim.core.gtu.GTU;
  6. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.Intermediate;
  7. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionAccumulator;
  8. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionCollector;
  9. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionFinalizer;
  10. import org.opentrafficsim.road.gtu.lane.perception.categories.AnticipationDensity.CountAndDistance;

  11. /**
  12.  * Collector to determine density based on GTUs.
  13.  * <p>
  14.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  15.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  16.  * <p>
  17.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 13 mrt. 2018 <br>
  18.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  19.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  20.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  21.  */
  22. public class AnticipationDensity implements PerceptionCollector<LinearDensity, GTU, CountAndDistance>
  23. {

  24.     /** {@inheritDoc} */
  25.     @Override
  26.     public Supplier<CountAndDistance> getIdentity()
  27.     {
  28.         return new Supplier<CountAndDistance>()
  29.         {
  30.             /** {@inheritDoc} */
  31.             @Override
  32.             public CountAndDistance get()
  33.             {
  34.                 return new CountAndDistance();
  35.             }
  36.         };
  37.     }

  38.     /** {@inheritDoc} */
  39.     @Override
  40.     public PerceptionAccumulator<GTU, CountAndDistance> getAccumulator()
  41.     {
  42.         return new PerceptionAccumulator<GTU, CountAndDistance>()
  43.         {
  44.             /** {@inheritDoc} */
  45.             @Override
  46.             public Intermediate<CountAndDistance> accumulate(final Intermediate<CountAndDistance> intermediate,
  47.                     final GTU object, final Length distance)
  48.             {
  49.                 intermediate.getObject().increaseCount();
  50.                 intermediate.getObject().setDistance(distance);
  51.                 return intermediate;
  52.             }
  53.         };
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     public PerceptionFinalizer<LinearDensity, CountAndDistance> getFinalizer()
  58.     {
  59.         return new PerceptionFinalizer<LinearDensity, CountAndDistance>()
  60.         {
  61.             /** {@inheritDoc} */
  62.             @Override
  63.             public LinearDensity collect(final CountAndDistance intermediate)
  64.             {
  65.                 return LinearDensity.instantiateSI(intermediate.getDistance().si / intermediate.getCount());
  66.             }
  67.         };
  68.     }

  69.     /**
  70.      * Intermediate data to determine density.
  71.      * <p>
  72.      * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  73.      * <br>
  74.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  75.      * <p>
  76.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 13 mrt. 2018 <br>
  77.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  78.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  79.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  80.      */
  81.     public static class CountAndDistance
  82.     {

  83.         /** Count. */
  84.         private int count;

  85.         /** Distance. */
  86.         private Length distance;

  87.         /**
  88.          * @return count.
  89.          */
  90.         public int getCount()
  91.         {
  92.             return this.count;
  93.         }

  94.         /**
  95.          * Increases the GTU count by 1.
  96.          */
  97.         public void increaseCount()
  98.         {
  99.             this.count++;
  100.         }

  101.         /**
  102.          * @return distance.
  103.          */
  104.         public Length getDistance()
  105.         {
  106.             return this.distance;
  107.         }

  108.         /**
  109.          * @param distance Length; set distance.
  110.          */
  111.         public void setDistance(final Length distance)
  112.         {
  113.             this.distance = distance;
  114.         }
  115.     }

  116. }