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.sensor.Detector;
13  
14  /**
15   * Switch implementing the RWS algorithm.
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 12 jun. 2019 <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 class RwsSwitch extends SingleCrossSectionSwitch
26  {
27  
28      /** Maximum cycle time. */
29      private static final Duration MAX_CYCLE_TIME = Duration.instantiateSI(15);
30  
31      /** Capacity. */
32      private final Frequency capacity;
33  
34      /** Flow threshold. */
35      private final Frequency flowThreshold;
36  
37      /** Speed threshold. */
38      private final Speed speedThreshold = new Speed(70, SpeedUnit.KM_PER_HOUR);
39  
40      /** Red time. */
41      private Duration cycleTime;
42  
43      /** Flow in previous time step. */
44      private Frequency lastFlow;
45  
46      /**
47       * @param detectors List&lt;Detector&gt;; detectors
48       */
49      public RwsSwitch(final List<Detector> detectors)
50      {
51          super(Duration.instantiateSI(60.0), detectors);
52          this.capacity = new Frequency(2000, FrequencyUnit.PER_HOUR).times(detectors.size());
53          this.flowThreshold = new Frequency(1500, FrequencyUnit.PER_HOUR).times(detectors.size());
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public boolean isEnabled()
59      {
60          Frequency flow = totalFlow();
61          CategoryLogger.always().info("Flow is " + flow.getInUnit(FrequencyUnit.PER_HOUR));
62          if (meanSpeed().le(this.speedThreshold)
63                  || (this.lastFlow != null && flow.gt(this.lastFlow) && flow.gt(this.flowThreshold)))
64          {
65              if (flow.lt(this.capacity))
66              {
67                  this.cycleTime = Duration.instantiateSI(1.0 / this.capacity.minus(flow).si);
68                  this.cycleTime = Duration.min(this.cycleTime, MAX_CYCLE_TIME);
69              }
70              else
71              {
72                  this.cycleTime = MAX_CYCLE_TIME;
73              }
74              this.lastFlow = flow;
75              return true;
76          }
77          this.lastFlow = flow;
78          return false;
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public Duration getCycleTime()
84      {
85          Throw.whenNull(this.cycleTime, "The method isEnabled() in a RwsSwitch should set a cycle time.");
86          return this.cycleTime;
87      }
88  
89  }