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
13
14
15
16
17
18
19
20
21 public abstract class SingleCrossSectionSwitch implements RampMeteringSwitch
22 {
23
24
25 private final Duration interval;
26
27
28 private final List<LoopDetector> detectors;
29
30
31
32
33
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
52
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
71
72
73 protected final Frequency meanFlow()
74 {
75 return totalFlow().divide(this.detectors.size());
76 }
77
78
79
80
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 }