View Javadoc
1   package org.opentrafficsim.road.network.control.rampmetering;
2   
3   import java.util.List;
4   
5   import org.djunits.unit.FrequencyUnit;
6   import org.djunits.unit.SpeedUnit;
7   import org.djunits.value.vdouble.scalar.Duration;
8   import org.djunits.value.vdouble.scalar.Frequency;
9   import org.djunits.value.vdouble.scalar.Speed;
10  import org.djutils.exceptions.Throw;
11  import org.djutils.logger.CategoryLogger;
12  import org.opentrafficsim.road.network.lane.object.detector.LoopDetector;
13  
14  /**
15   * Switch implementing the RWS algorithm.
16   * <p>
17   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * </p>
20   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
22   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
23   */
24  public class RwsSwitch extends SingleCrossSectionSwitch
25  {
26  
27      /** Maximum cycle time. */
28      private static final Duration MAX_CYCLE_TIME = Duration.instantiateSI(15);
29  
30      /** Capacity. */
31      private final Frequency capacity;
32  
33      /** Flow threshold. */
34      private final Frequency flowThreshold;
35  
36      /** Speed threshold. */
37      private final Speed speedThreshold = new Speed(70, SpeedUnit.KM_PER_HOUR);
38  
39      /** Red time. */
40      private Duration cycleTime;
41  
42      /** Flow in previous time step. */
43      private Frequency lastFlow;
44  
45      /**
46       * @param detectors List&lt;Detector&gt;; detectors
47       */
48      public RwsSwitch(final List<LoopDetector> detectors)
49      {
50          super(Duration.instantiateSI(60.0), detectors);
51          this.capacity = new Frequency(2000, FrequencyUnit.PER_HOUR).times(detectors.size());
52          this.flowThreshold = new Frequency(1500, FrequencyUnit.PER_HOUR).times(detectors.size());
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public boolean isEnabled()
58      {
59          Frequency flow = totalFlow();
60          CategoryLogger.always().info("Flow is " + flow.getInUnit(FrequencyUnit.PER_HOUR));
61          if (meanSpeed().le(this.speedThreshold)
62                  || (this.lastFlow != null && flow.gt(this.lastFlow) && flow.gt(this.flowThreshold)))
63          {
64              if (flow.lt(this.capacity))
65              {
66                  this.cycleTime = Duration.instantiateSI(1.0 / this.capacity.minus(flow).si);
67                  this.cycleTime = Duration.min(this.cycleTime, MAX_CYCLE_TIME);
68              }
69              else
70              {
71                  this.cycleTime = MAX_CYCLE_TIME;
72              }
73              this.lastFlow = flow;
74              return true;
75          }
76          this.lastFlow = flow;
77          return false;
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public Duration getCycleTime()
83      {
84          Throw.whenNull(this.cycleTime, "The method isEnabled() in a RwsSwitch should set a cycle time.");
85          return this.cycleTime;
86      }
87  
88  }