View Javadoc
1   package org.opentrafficsim.road.network.control.rampmetering;
2   
3   import java.util.List;
4   
5   import org.djunits.value.vdouble.scalar.Duration;
6   import org.djunits.value.vdouble.scalar.Frequency;
7   import org.djunits.value.vdouble.scalar.Speed;
8   import org.djutils.exceptions.Throw;
9   import org.opentrafficsim.road.network.lane.object.detector.LoopDetector;
10  
11  /**
12   * Super class for feed-forward controller. This class contains some helper methods for sub-classes.
13   * <p>
14   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * </p>
17   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
19   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
20   */
21  public abstract class SingleCrossSectionSwitch implements RampMeteringSwitch
22  {
23  
24      /** Control interval. */
25      private final Duration interval;
26  
27      /** Detectors (on downstream section). */
28      private final List<LoopDetector> detectors;
29  
30      /**
31       * Constructor.
32       * @param interval Duration; interval
33       * @param detectors List&lt;Detector&gt;; detectors
34       */
35      public SingleCrossSectionSwitch(final Duration interval, final List<LoopDetector> detectors)
36      {
37          Throw.whenNull(interval, "Interval may not be null.");
38          Throw.when(detectors == null || detectors.size() == 0, IllegalArgumentException.class,
39                  "At least 1 detector is required.");
40          this.interval = interval;
41          this.detectors = detectors;
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public Duration getInterval()
47      {
48          return this.interval;
49      }
50  
51      /**
52       * Returns the mean speed over the detectors.
53       * @return Speed; mean speed over the detectors
54       */
55      protected final Speed meanSpeed()
56      {
57          int n = 0;
58          double value = 0.0;
59          for (LoopDetector detector : this.detectors)
60          {
61              if (detector.hasLastValue())
62              {
63                  value += detector.getLastValue(LoopDetector.MEAN_SPEED).si;
64                  n++;
65              }
66          }
67          return Speed.instantiateSI(value / n);
68      }
69  
70      /**
71       * Returns the mean flow over the detectors.
72       * @return Frequency; mean flow over the detectors
73       */
74      protected final Frequency meanFlow()
75      {
76          return totalFlow().divide(this.detectors.size());
77      }
78  
79      /**
80       * Returns the total flow over the detectors.
81       * @return Frequency; total flow over the detectors
82       */
83      protected final Frequency totalFlow()
84      {
85          double value = 0.0;
86          for (LoopDetector detector : this.detectors)
87          {
88              if (detector.hasLastValue())
89              {
90                  value += detector.getLastFlow().si;
91              }
92          }
93          return Frequency.instantiateSI(value);
94      }
95  
96  }