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://github.com/peter-knoppers">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 interval
33       * @param detectors 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      @Override
45      public Duration getInterval()
46      {
47          return this.interval;
48      }
49  
50      /**
51       * Returns the mean speed over the detectors.
52       * @return mean speed over the detectors
53       */
54      protected final Speed meanSpeed()
55      {
56          int n = 0;
57          double value = 0.0;
58          for (LoopDetector detector : this.detectors)
59          {
60              if (detector.hasLastValue())
61              {
62                  value += detector.getLastValue(LoopDetector.MEAN_SPEED).si;
63                  n++;
64              }
65          }
66          return Speed.instantiateSI(value / n);
67      }
68  
69      /**
70       * Returns the mean flow over the detectors.
71       * @return mean flow over the detectors
72       */
73      protected final Frequency meanFlow()
74      {
75          return totalFlow().divide(this.detectors.size());
76      }
77  
78      /**
79       * Returns the total flow over the detectors.
80       * @return total flow over the detectors
81       */
82      protected final Frequency totalFlow()
83      {
84          double value = 0.0;
85          for (LoopDetector detector : this.detectors)
86          {
87              if (detector.hasLastValue())
88              {
89                  value += detector.getLastFlow().si;
90              }
91          }
92          return Frequency.instantiateSI(value);
93      }
94  
95  }