View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;
2   
3   import java.util.LinkedHashMap;
4   import java.util.LinkedHashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
9   import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
10  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
11  import org.opentrafficsim.road.gtu.lane.tactical.DesireBased;
12  import org.opentrafficsim.road.gtu.lane.tactical.Synchronizable;
13  
14  /**
15   * Keeps data for LMRS for a specific GTU.
16   * <p>
17   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 8 nov. 2016 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public final class LmrsData implements DesireBased, Synchronizable
26  {
27  
28      /** Form of synchronization. */
29      private final Synchronization synchronization;
30  
31      /** Form of cooperation. */
32      private final Cooperation cooperation;
33  
34      /** Form of gap-acceptance. */
35      private final GapAcceptance gapAcceptance;
36  
37      /** Form of tail gating. */
38      private final Tailgating tailgating;
39  
40      /** Most recent leaders. */
41      private final Set<String> leaders = new LinkedHashSet<>();
42  
43      /** Current leaders. */
44      private final Set<String> tempLeaders = new LinkedHashSet<>();
45  
46      /** Latest desire value for visualization. */
47      private final Map<Class<? extends Incentive>, Desire> desireMap = new LinkedHashMap<>();
48  
49      /** Synchronization state. */
50      private Synchronizable.State synchronizationState = Synchronizable.State.NONE;
51  
52      /** Vehicle that is being synchronized to. */
53      private String syncVehicle;
54  
55      /** Whether the longitudinal control is human. */
56      private boolean humanLongitudinalControl = true;
57  
58      /**
59       * @param synchronization Synchronization; synchronization
60       * @param cooperation Cooperation; cooperation
61       * @param gapAcceptance GapAcceptance; gap-acceptance
62       * @param tailgating Tailgating; tail gating
63       */
64      public LmrsData(final Synchronization synchronization, final Cooperation cooperation, final GapAcceptance gapAcceptance,
65              final Tailgating tailgating)
66      {
67          this.synchronization = synchronization;
68          this.cooperation = cooperation;
69          this.gapAcceptance = gapAcceptance;
70          this.tailgating = tailgating;
71      }
72  
73      /**
74       * Checks if the given leader is a new leader.
75       * @param gtu HeadwayGTU; gtu to check
76       * @return whether the gtu is a new leader
77       */
78      boolean isNewLeader(final HeadwayGTU gtu)
79      {
80          this.tempLeaders.add(gtu.getId());
81          return !this.leaders.contains(gtu.getId());
82      }
83  
84      /**
85       * Remembers the leaders of the current time step (those forwarded to isNewLeader()) for the next time step.
86       */
87      void finalizeStep()
88      {
89          this.leaders.clear();
90          this.leaders.addAll(this.tempLeaders);
91          this.tempLeaders.clear();
92      }
93  
94      /**
95       * Remembers the gtu that is synchronized to.
96       * @param gtu HeadwayGTU; gtu that is synchronized to
97       */
98      void setSyncVehicle(final HeadwayGTU gtu)
99      {
100         this.syncVehicle = gtu == null ? null : gtu.getId();
101     }
102 
103     /**
104      * Returns whether the provided gtu is the gtu that is synchronized to.
105      * @param gtu HeadwayGTU; gtu to inquiry
106      * @return whether the provided gtu is the gtu that is synchronized to
107      */
108     boolean isSyncVehicle(final HeadwayGTU gtu)
109     {
110         return this.syncVehicle == null ? false : gtu.getId().equals(this.syncVehicle);
111     }
112 
113     /**
114      * 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
115      * in the set.
116      * @param adjLeaders PerceptionCollectable&lt;HeadwayGTU,LaneBasedGTU&gt;; leaders in adjacent lane
117      * @return gtu from the set that is the current sync vehicle
118      */
119     HeadwayGTU getSyncVehicle(final PerceptionCollectable<HeadwayGTU, LaneBasedGTU> adjLeaders)
120     {
121         if (this.syncVehicle == null)
122         {
123             return null;
124         }
125         for (HeadwayGTU leader : adjLeaders)
126         {
127             if (leader.getId().equals(this.syncVehicle))
128             {
129                 return leader;
130             }
131         }
132         return null;
133     }
134 
135     /**
136      * Returns the synchronization.
137      * @return synchronization
138      */
139     Synchronization getSynchronization()
140     {
141         return this.synchronization;
142     }
143 
144     /**
145      * Returns the cooperation.
146      * @return cooperation
147      */
148     Cooperation getCooperation()
149     {
150         return this.cooperation;
151     }
152 
153     /**
154      * Return the gap-acceptance.
155      * @return gap-acceptance
156      */
157     GapAcceptance getGapAcceptance()
158     {
159         return this.gapAcceptance;
160     }
161 
162     /**
163      * Return the tail gating.
164      * @return gap-acceptance
165      */
166     Tailgating getTailgating()
167     {
168         return this.tailgating;
169     }
170 
171     /** {@inheritDoc} */
172     @Override
173     public Desire getLatestDesire(final Class<? extends Incentive> incentiveClass)
174     {
175         return this.desireMap.get(incentiveClass);
176     }
177     
178     /**
179      * Returns the desire map.
180      * @return Map&lt;Class&lt;? extends Incentive&gt;, Desire&gt;; desire map
181      */
182     Map<Class<? extends Incentive>, Desire> getDesireMap()
183     {
184         return this.desireMap;
185     }
186 
187     /**
188      * Sets the synchronization state.
189      * @param synchronizationState Synchronizable.State; synchronization step
190      */
191     void setSynchronizationState(final Synchronizable.State synchronizationState)
192     {
193         this.synchronizationState = synchronizationState;
194     }
195 
196     /** {@inheritDoc} */
197     @Override
198     public Synchronizable.State getSynchronizationState()
199     {
200         return this.synchronizationState;
201     }
202 
203     /**
204      * @return humanLongitudinalControl.
205      */
206     boolean isHumanLongitudinalControl()
207     {
208         return this.humanLongitudinalControl;
209     }
210 
211     /**
212      * @param humanLongitudinalControl boolean; set humanLongitudinalControl.
213      */
214     public void setHumanLongitudinalControl(final boolean humanLongitudinalControl)
215     {
216         this.humanLongitudinalControl = humanLongitudinalControl;
217     }
218 
219     /** {@inheritDoc} */
220     @Override
221     public String toString()
222     {
223         return "LmrsData [synchronization=" + this.synchronization + ", leaders=" + this.leaders + ", tempLeaders="
224                 + this.tempLeaders + ", syncVehicle=" + this.syncVehicle + "]";
225     }
226 
227 }