LmrsData.java

  1. package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;

  2. import java.util.LinkedHashMap;
  3. import java.util.LinkedHashSet;
  4. import java.util.Map;
  5. import java.util.Set;

  6. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  7. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
  8. import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
  9. import org.opentrafficsim.road.gtu.lane.tactical.DesireBased;
  10. import org.opentrafficsim.road.gtu.lane.tactical.Synchronizable;

  11. /**
  12.  * Keeps data for LMRS for a specific GTU.
  13.  * <p>
  14.  * Copyright (c) 2013-2020 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 8 nov. 2016 <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 final class LmrsData implements DesireBased, Synchronizable
  23. {

  24.     /** Form of synchronization. */
  25.     private final Synchronization synchronization;

  26.     /** Form of cooperation. */
  27.     private final Cooperation cooperation;

  28.     /** Form of gap-acceptance. */
  29.     private final GapAcceptance gapAcceptance;

  30.     /** Form of tail gating. */
  31.     private final Tailgating tailgating;

  32.     /** Most recent leaders. */
  33.     private final Set<String> leaders = new LinkedHashSet<>();

  34.     /** Current leaders. */
  35.     private final Set<String> tempLeaders = new LinkedHashSet<>();

  36.     /** Latest desire value for visualization. */
  37.     private final Map<Class<? extends Incentive>, Desire> desireMap = new LinkedHashMap<>();

  38.     /** Synchronization state. */
  39.     private Synchronizable.State synchronizationState = Synchronizable.State.NONE;

  40.     /** Vehicle that is being synchronized to. */
  41.     private String syncVehicle;

  42.     /** Whether the longitudinal control is human. */
  43.     private boolean humanLongitudinalControl = true;

  44.     /**
  45.      * @param synchronization Synchronization; synchronization
  46.      * @param cooperation Cooperation; cooperation
  47.      * @param gapAcceptance GapAcceptance; gap-acceptance
  48.      * @param tailgating Tailgating; tail gating
  49.      */
  50.     public LmrsData(final Synchronization synchronization, final Cooperation cooperation, final GapAcceptance gapAcceptance,
  51.             final Tailgating tailgating)
  52.     {
  53.         this.synchronization = synchronization;
  54.         this.cooperation = cooperation;
  55.         this.gapAcceptance = gapAcceptance;
  56.         this.tailgating = tailgating;
  57.     }

  58.     /**
  59.      * Checks if the given leader is a new leader.
  60.      * @param gtu HeadwayGTU; gtu to check
  61.      * @return whether the gtu is a new leader
  62.      */
  63.     boolean isNewLeader(final HeadwayGTU gtu)
  64.     {
  65.         this.tempLeaders.add(gtu.getId());
  66.         return !this.leaders.contains(gtu.getId());
  67.     }

  68.     /**
  69.      * Remembers the leaders of the current time step (those forwarded to isNewLeader()) for the next time step.
  70.      */
  71.     void finalizeStep()
  72.     {
  73.         this.leaders.clear();
  74.         this.leaders.addAll(this.tempLeaders);
  75.         this.tempLeaders.clear();
  76.     }

  77.     /**
  78.      * Remembers the gtu that is synchronized to.
  79.      * @param gtu HeadwayGTU; gtu that is synchronized to
  80.      */
  81.     void setSyncVehicle(final HeadwayGTU gtu)
  82.     {
  83.         this.syncVehicle = gtu == null ? null : gtu.getId();
  84.     }

  85.     /**
  86.      * Returns whether the provided gtu is the gtu that is synchronized to.
  87.      * @param gtu HeadwayGTU; gtu to inquiry
  88.      * @return whether the provided gtu is the gtu that is synchronized to
  89.      */
  90.     boolean isSyncVehicle(final HeadwayGTU gtu)
  91.     {
  92.         return this.syncVehicle == null ? false : gtu.getId().equals(this.syncVehicle);
  93.     }

  94.     /**
  95.      * Returns the gtu from the set that is the current sync vehicle, or {@code null} of there is no sync vehicle or it is not
  96.      * in the set.
  97.      * @param adjLeaders PerceptionCollectable&lt;HeadwayGTU,LaneBasedGTU&gt;; leaders in adjacent lane
  98.      * @return gtu from the set that is the current sync vehicle
  99.      */
  100.     HeadwayGTU getSyncVehicle(final PerceptionCollectable<HeadwayGTU, LaneBasedGTU> adjLeaders)
  101.     {
  102.         if (this.syncVehicle == null)
  103.         {
  104.             return null;
  105.         }
  106.         for (HeadwayGTU leader : adjLeaders)
  107.         {
  108.             if (leader.getId().equals(this.syncVehicle))
  109.             {
  110.                 return leader;
  111.             }
  112.         }
  113.         return null;
  114.     }

  115.     /**
  116.      * Returns the synchronization.
  117.      * @return synchronization
  118.      */
  119.     Synchronization getSynchronization()
  120.     {
  121.         return this.synchronization;
  122.     }

  123.     /**
  124.      * Returns the cooperation.
  125.      * @return cooperation
  126.      */
  127.     Cooperation getCooperation()
  128.     {
  129.         return this.cooperation;
  130.     }

  131.     /**
  132.      * Return the gap-acceptance.
  133.      * @return gap-acceptance
  134.      */
  135.     GapAcceptance getGapAcceptance()
  136.     {
  137.         return this.gapAcceptance;
  138.     }

  139.     /**
  140.      * Return the tail gating.
  141.      * @return gap-acceptance
  142.      */
  143.     Tailgating getTailgating()
  144.     {
  145.         return this.tailgating;
  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     public Desire getLatestDesire(final Class<? extends Incentive> incentiveClass)
  150.     {
  151.         return this.desireMap.get(incentiveClass);
  152.     }
  153.    
  154.     /**
  155.      * Returns the desire map.
  156.      * @return Map&lt;Class&lt;? extends Incentive&gt;, Desire&gt;; desire map
  157.      */
  158.     Map<Class<? extends Incentive>, Desire> getDesireMap()
  159.     {
  160.         return this.desireMap;
  161.     }

  162.     /**
  163.      * Sets the synchronization state.
  164.      * @param synchronizationState Synchronizable.State; synchronization step
  165.      */
  166.     void setSynchronizationState(final Synchronizable.State synchronizationState)
  167.     {
  168.         this.synchronizationState = synchronizationState;
  169.     }

  170.     /** {@inheritDoc} */
  171.     @Override
  172.     public Synchronizable.State getSynchronizationState()
  173.     {
  174.         return this.synchronizationState;
  175.     }

  176.     /**
  177.      * @return humanLongitudinalControl.
  178.      */
  179.     boolean isHumanLongitudinalControl()
  180.     {
  181.         return this.humanLongitudinalControl;
  182.     }

  183.     /**
  184.      * @param humanLongitudinalControl boolean; set humanLongitudinalControl.
  185.      */
  186.     public void setHumanLongitudinalControl(final boolean humanLongitudinalControl)
  187.     {
  188.         this.humanLongitudinalControl = humanLongitudinalControl;
  189.     }

  190.     /** {@inheritDoc} */
  191.     @Override
  192.     public String toString()
  193.     {
  194.         return "LmrsData [synchronization=" + this.synchronization + ", leaders=" + this.leaders + ", tempLeaders="
  195.                 + this.tempLeaders + ", syncVehicle=" + this.syncVehicle + "]";
  196.     }

  197. }