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.opentrafficsim.road.network.lane.object.sensor.Detector;
12  
13  import nl.tudelft.simulation.dsol.logger.SimLogger;
14  
15  /**
16   * Switch implementing the RWS algorithm.
17   * <p>
18   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
20   * <p>
21   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 12 jun. 2019 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
25   */
26  public class RwsSwitch extends SingleCrossSectionSwitch
27  {
28  
29      /** Maximum cycle time. */
30      private static final Duration MAX_CYCLE_TIME = Duration.instantiateSI(15);
31  
32      /** Capacity. */
33      private final Frequency capacity;
34  
35      /** Flow threshold. */
36      private final Frequency flowThreshold;
37  
38      /** Speed threshold. */
39      private final Speed speedThreshold = new Speed(70, SpeedUnit.KM_PER_HOUR);
40  
41      /** Red time. */
42      private Duration cycleTime;
43  
44      /** Flow in previous time step. */
45      private Frequency lastFlow;
46  
47      /**
48       * @param detectors List&lt;Detector&gt;; detectors
49       */
50      public RwsSwitch(final List<Detector> detectors)
51      {
52          super(Duration.instantiateSI(60.0), detectors);
53          this.capacity = new Frequency(2000, FrequencyUnit.PER_HOUR).times(detectors.size());
54          this.flowThreshold = new Frequency(1500, FrequencyUnit.PER_HOUR).times(detectors.size());
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public boolean isEnabled()
60      {
61          Frequency flow = totalFlow();
62          SimLogger.always().info("Flow is " + flow.getInUnit(FrequencyUnit.PER_HOUR));
63          if (meanSpeed().le(this.speedThreshold)
64                  || (this.lastFlow != null && flow.gt(this.lastFlow) && flow.gt(this.flowThreshold)))
65          {
66              if (flow.lt(this.capacity))
67              {
68                  this.cycleTime = Duration.instantiateSI(1.0 / this.capacity.minus(flow).si);
69                  this.cycleTime = Duration.min(this.cycleTime, MAX_CYCLE_TIME);
70              }
71              else
72              {
73                  this.cycleTime = MAX_CYCLE_TIME;
74              }
75              this.lastFlow = flow;
76              return true;
77          }
78          this.lastFlow = flow;
79          return false;
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public Duration getCycleTime()
85      {
86          Throw.whenNull(this.cycleTime, "The method isEnabled() in a RwsSwitch should set a cycle time.");
87          return this.cycleTime;
88      }
89  
90  }