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://github.com/peter-knoppers">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 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      @Override
56      public boolean isEnabled()
57      {
58          Frequency flow = totalFlow();
59          CategoryLogger.always().info("Flow is " + flow.getInUnit(FrequencyUnit.PER_HOUR));
60          if (meanSpeed().le(this.speedThreshold)
61                  || (this.lastFlow != null && flow.gt(this.lastFlow) && flow.gt(this.flowThreshold)))
62          {
63              if (flow.lt(this.capacity))
64              {
65                  this.cycleTime = Duration.instantiateSI(1.0 / this.capacity.minus(flow).si);
66                  this.cycleTime = Duration.min(this.cycleTime, MAX_CYCLE_TIME);
67              }
68              else
69              {
70                  this.cycleTime = MAX_CYCLE_TIME;
71              }
72              this.lastFlow = flow;
73              return true;
74          }
75          this.lastFlow = flow;
76          return false;
77      }
78  
79      @Override
80      public Duration getCycleTime()
81      {
82          Throw.whenNull(this.cycleTime, "The method isEnabled() in a RwsSwitch should set a cycle time.");
83          return this.cycleTime;
84      }
85  
86  }