1 package org.opentrafficsim.road.gtu.strategical.od;
2
3 import org.djunits.unit.FrequencyUnit;
4 import org.djunits.unit.TimeUnit;
5 import org.djunits.value.ValueException;
6 import org.djunits.value.vdouble.scalar.Frequency;
7 import org.djunits.value.vdouble.scalar.Time;
8 import org.djunits.value.vdouble.vector.FrequencyVector;
9 import org.djunits.value.vdouble.vector.TimeVector;
10
11
12
13
14
15
16
17
18
19
20
21
22 public enum Interpolation
23 {
24
25
26 STEPWISE
27 {
28
29 @Override
30 Frequency interpolate(final Frequency frequency0, final Time time0, final Frequency frequency1, final Time time1,
31 final Time time)
32 {
33 return frequency0;
34 }
35
36
37 @Override
38 int integrate(final Frequency frequency0, final Time time0, final Frequency frequency1, final Time time1)
39 {
40 return (int) (frequency0.getInUnit(FrequencyUnit.PER_HOUR)
41 * (time1.getInUnit(TimeUnit.BASE_HOUR) - time0.getInUnit(TimeUnit.BASE_HOUR)));
42 }
43 },
44
45
46 LINEAR
47 {
48
49 @Override
50 Frequency interpolate(final Frequency frequency0, final Time time0, final Frequency frequency1, final Time time1,
51 final Time time)
52 {
53 return Frequency.interpolate(frequency0, frequency1, (time.si - time0.si) / (time1.si - time0.si));
54 }
55
56
57 @Override
58 int integrate(final Frequency frequency0, final Time time0, final Frequency frequency1, final Time time1)
59 {
60 return (int) (0.5 * (frequency0.getInUnit(FrequencyUnit.PER_HOUR) + frequency1.getInUnit(FrequencyUnit.PER_HOUR))
61 * (time1.getInUnit(TimeUnit.BASE_HOUR) - time0.getInUnit(TimeUnit.BASE_HOUR)));
62 }
63 };
64
65
66
67
68
69
70
71
72
73
74 abstract Frequency interpolate(Frequency frequency0, Time time0, Frequency frequency1, Time time1, Time time);
75
76
77
78
79
80
81
82
83
84 abstract int integrate(Frequency frequency0, Time time0, Frequency frequency1, Time time1);
85
86
87
88
89 public boolean isStepWise()
90 {
91 return this.equals(STEPWISE);
92 }
93
94
95
96
97 public boolean isLinear()
98 {
99 return this.equals(LINEAR);
100 }
101
102
103
104
105
106
107
108
109
110 public final Frequency interpolateVector(final Time time, final FrequencyVector demandVector, final TimeVector timeVector,
111 final boolean sliceStart)
112 {
113 try
114 {
115
116
117
118
119
120 if (timeVector.size() == 0 || (sliceStart ? time.lt(timeVector.get(0)) : time.le(timeVector.get(0))) || (sliceStart
121 ? time.ge(timeVector.get(timeVector.size() - 1)) : time.gt(timeVector.get(timeVector.size() - 1))))
122 {
123 return new Frequency(0.0, FrequencyUnit.PER_HOUR);
124 }
125
126 for (int i = 0; i < timeVector.size() - 1; i++)
127 {
128
129
130
131 if (sliceStart ? timeVector.get(i + 1).gt(time) : timeVector.get(i + 1).ge(time))
132 {
133 return interpolate(demandVector.get(i), timeVector.get(i), demandVector.get(i + 1), timeVector.get(i + 1),
134 time);
135 }
136 }
137 }
138 catch (ValueException ve)
139 {
140
141 throw new RuntimeException("Index out of bounds.", ve);
142 }
143
144 throw new RuntimeException("Demand interpolation failed.");
145 }
146
147 }