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
45 @Override
46 public Duration getInterval()
47 {
48 return this.interval;
49 }
50
51
52
53
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
72
73
74 protected final Frequency meanFlow()
75 {
76 return totalFlow().divide(this.detectors.size());
77 }
78
79
80
81
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 }