-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcriteria.ts
More file actions
214 lines (195 loc) · 8 KB
/
criteria.ts
File metadata and controls
214 lines (195 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { Criterion, Id, InternalTimeInt, Journey, JourneyStep, Label, makeJSComparable, Time, TimeScal } from "./structures";
function isCriterionJourneyStepVehicle<TimeVal, SI extends Id, RI extends Id, V, CA extends [V, string][]>(
js: Parameters<Criterion<TimeVal, SI, RI, V, CA[number][1]>["update"]>[1],
): js is JourneyStep<TimeVal, SI, RI, V, CA, "VEHICLE"> {
return "route" in js;
}
function isCriterionJourneyStepFoot<TimeVal, SI extends Id, RI extends Id, V, CA extends [V, string][]>(
js: Parameters<Criterion<TimeVal, SI, RI, V, CA[number][1]>["update"]>[1],
): js is JourneyStep<TimeVal, SI, RI, V, CA, "FOOT"> {
return "transfer" in js;
}
/**
* Buffer Time criterion as described in *Round-Based Public Transit Routing* (Daniel Delling, Thomas Pajor, Renato F. Werneck, 2015).
*
* It computes the minimum of the free times between connections (i.e. the time before taking a vehicle) of the whole journey.
*/
const bufferTime: Criterion<unknown, Id, Id, number, "bufferTime"> = {
name: "bufferTime",
initialValue: -Infinity,
order: TimeScal.strict.order,
update: (prefixJourney, newJourneyStep, timeType) => {
const lastJourneyStep = prefixJourney.at(-1);
if (!lastJourneyStep) throw new Error("A journey should at least contain the DEPARTURE step.");
if (!isCriterionJourneyStepVehicle(newJourneyStep)) return lastJourneyStep.label.value("bufferTime");
const diff =
timeType.low(newJourneyStep.route.departureTime(newJourneyStep.tripIndex, newJourneyStep.route.stops.indexOf(newJourneyStep.boardedAt[0]))) -
timeType.up(lastJourneyStep.label.time);
if (diff < 0) return 0;
return Math.max(lastJourneyStep.label.value("bufferTime"), -diff, -(30 * 60 * 1000));
},
};
/**
* Foot distance criterion.
*
* It simply accumulates foot distance over the whole journey.
*/
const footDistance: Criterion<unknown, Id, Id, number, "footDistance"> = {
name: "footDistance",
initialValue: 0,
order: TimeScal.strict.order,
update: (prefixJourney, newJourneyStep) => {
const lastJourneyStep = prefixJourney.at(-1);
if (!lastJourneyStep) throw new Error("A journey should at least contain the DEPARTURE step.");
return lastJourneyStep.label.value("footDistance") + (isCriterionJourneyStepFoot(newJourneyStep) ? newJourneyStep.transfer.length : 0);
},
};
/**
* Success probability criterion -- interval time only.
*
* It computes the minimum success probability of the connection during the journey.
*/
const successProbaInt: Criterion<InternalTimeInt, Id, Id, number, "successProbaInt"> = {
name: "successProbaInt",
initialValue: -1,
order: TimeScal.strict.order,
update: (prefixJourney, newJourneyStep, timeType) => {
const lastJourneyStep = prefixJourney.at(-1);
if (!lastJourneyStep) throw new Error("A journey should at least contain the DEPARTURE step.");
if (!isCriterionJourneyStepVehicle(newJourneyStep))
// Probability of success is about feasibility of a boarding, i.e. taking a vehicle
// Going by foot is always 100% feasible
return lastJourneyStep.label.value("successProbaInt");
const prevLow = timeType.low(lastJourneyStep.label.time);
const prevUp = timeType.up(lastJourneyStep.label.time);
const tDep = newJourneyStep.route.departureTime(newJourneyStep.tripIndex, newJourneyStep.route.stops.indexOf(newJourneyStep.boardedAt[0]));
const newLow = timeType.low(tDep);
const newUp = timeType.up(tDep);
return (
lastJourneyStep.label.value("successProbaInt") *
(newLow > prevUp
? // No intersection, 100% feasible
1
: newUp < prevLow
? // No intersection, 0% feasible -- should never happen
0
: // Intersects somehow, add segments in absolute value
((prevLow < newLow
? // Feasible segment
// [ ...
// [ ...
1 * (newLow - prevLow)
: prevLow > newLow
? // Infeasible segment
// [ ...
// [ ...
0 * (prevLow - newLow)
: 0) +
(prevUp < newUp
? // Feasible segment
// ... ]
// ... ]
1 * (newUp - prevUp)
: prevUp > newUp
? // Infeasible segment
// ... ]
// ... ]
0 * (prevUp - newUp)
: 0) +
// Uncertain segment
// ... [ ] ...
// ... [ ] ...
0.5 * (Math.min(prevUp, newUp) - Math.max(prevLow, newLow) + 1)) /
// Divide by width to get relative value in [0,1]
(Math.max(prevUp, newUp) - Math.min(prevLow, newLow) + 1))
);
},
};
/**
* Parametrized criterion "Mean Risk" from *Practical Route Planning Under Delay Uncertainty: Stochastic Shortest Path Queries* (Sejoon Lim AND Christian Sommer AND Evdokia Nikolova AND Daniela Rus, 2012)
*
* Modification: it's not `mean + c * STD`, but `mean + c * radius` (`width / 2`)
* @param c Risk-aversion coefficient
* @returns Mean Risk criterion. The risk-aversion coefficient {@link c} is appended to the criterion name.
*/
const meanRiskInit = <C extends number>(c: C): Criterion<InternalTimeInt, Id, Id, number, `meanRisk-${C}`> => ({
initialValue: NaN,
name: `meanRisk-${c}`,
order: TimeScal.strict.order,
update: (_, __, timeType, time) => {
const timeRadius = (timeType.up(time) - timeType.low(time)) / 2;
const timeMean = timeType.low(time) + timeRadius;
return timeMean + c * timeRadius;
},
});
/**
* Run a criterion against an already computed journey.
* @param criterion Criterion to evaluate
* @param timeType Time type used to compute the journey
* @param journey Journey to measure
* @param pt Target stop
*/
function measureJourney<TimeVal, SI extends Id, RI extends Id, V, CA extends [V, string][], T, N extends string>(
criterion: Criterion<TimeVal, SI, RI, T, N>,
timeType: Time<TimeVal>,
journey: Journey<TimeVal, SI, RI, V, CA>,
pt: SI,
) {
return journey.reduce<Journey<TimeVal, SI, RI, V | T, [...CA, [T, N]]>>(
(acc, js, i) => {
const rebasedJS = {
...js,
...("boardedAt" in js
? // Not a DEPARTURE journey step
{
boardedAt:
// Need to provide previous journey step
[js.boardedAt, acc[i - 1]],
}
: {}),
label: js.label.criteria.reduce(
(acc, criterion) =>
(acc as Label<TimeVal, SI, RI, V, CA>).setValue(criterion.name, js.label.value(criterion.name)) as unknown as Label<
TimeVal,
SI,
RI,
V | T,
[...CA, [T, N]]
>,
new Label<TimeVal, SI, RI, V | T, [...CA, [T, N]]>(timeType, [...js.label.criteria, criterion], js.label.time),
),
};
return [
...acc,
i < 1
? makeJSComparable<TimeVal, SI, RI, V | T, [...CA, [T, N]]>({
...rebasedJS,
})
: {
...makeJSComparable({
...rebasedJS,
label: (rebasedJS.label as unknown as Label<TimeVal, SI, RI, T, [[T, N]]>).setValue(
criterion.name,
criterion.update(
acc as unknown as Journey<TimeVal, SI, RI, T, [[T, N]]>,
rebasedJS,
timeType,
rebasedJS.label.time,
i + 1 < journey.length ? (journey[i + 1] as JourneyStep<TimeVal, SI, RI, V, CA, "FOOT" | "VEHICLE", true>).boardedAt : pt,
),
) as unknown as Label<TimeVal, SI, RI, V | T, [...CA, [T, N]]>,
}),
...("boardedAt" in js
? // Not a DEPARTURE journey step
{
boardedAt: js.boardedAt,
}
: {}),
},
];
},
// Prefix
[],
);
}
export { bufferTime, footDistance, meanRiskInit, measureJourney, successProbaInt };