1 package org.opentrafficsim.sim0mq.kpi;
2
3 import org.djunits.unit.SpeedUnit;
4 import org.djunits.value.vdouble.scalar.Speed;
5 import org.djunits.value.vdouble.scalar.Time;
6 import org.opentrafficsim.kpi.sampling.indicator.MeanSpeed;
7 import org.opentrafficsim.kpi.sampling.indicator.MeanTravelTimePerDistance;
8 import org.opentrafficsim.kpi.sampling.indicator.MeanTripLength;
9 import org.opentrafficsim.kpi.sampling.indicator.TotalDelay;
10 import org.opentrafficsim.kpi.sampling.indicator.TotalNumberOfStops;
11 import org.opentrafficsim.kpi.sampling.indicator.TotalTravelDistance;
12 import org.opentrafficsim.kpi.sampling.indicator.TotalTravelTime;
13
14 @SuppressWarnings("checkstyle:JavadocStyle")
15 /**
16 * OTS can publish messages about statistics or Key Performance Indicators (KPIs) of the relation between GTUs and a part of the
17 * network. Examples are average speeds, travel times, trip durations, and number of stops. The statistics can be transmitted
18 * via IMB at certain intervals. The statistics are based on the classes in the ots.road.sampling package. Most statistics
19 * consider a time interval (can be unbound) and a region in space (a collection of lanes; can be the entire network), filtered
20 * on metadata such as GTU type, origin, destination, or route.<br>
21 * When a statistic is published for the first time, a NEW message is sent to IMB to identify the type of statistic, the time
22 * interval and the lane(s) for which the statistic is calculated, and the metadata used to filter the GTUs. The CHANGE message
23 * is posted whenever an updated statistic is available, or when the sample frequency time is reached. When a statistic is no
24 * longer published, a DELETE event is posted. The Graph NEW messages are posted after the Network NEW, Node NEW, Link NEW, and
25 * Lane NEW messages are posted, as it has to be able to identify Lanes.
26 * <p>
27 * <style>table,th,td {border:1px solid grey; border-style:solid; text-align:left; border-collapse: collapse;}</style>
28 * <h2>NEW</h2>
29 * <table style="width:800px;">
30 * <caption> </caption> <thead>
31 * <tr>
32 * <th style="width:25%;">Variable</th>
33 * <th style="width:15%;">Type</th>
34 * <th style="width:60%;">Comments</th>
35 * </tr>
36 * </thead><tbody>
37 * <tr>
38 * <td>timestamp</td>
39 * <td>double</td>
40 * <td>time of the event, in simulation time seconds</td>
41 * </tr>
42 * <tr>
43 * <td>statisticId</td>
44 * <td>String</td>
45 * <td>a unique id for the statistic, e.g. a UUID string</td>
46 * </tr>
47 * <tr>
48 * <td>description</td>
49 * <td>String</td>
50 * <td>textual description of the statistic</td>
51 * </tr>
52 * <tr>
53 * <td>networkId</td>
54 * <td>String</td>
55 * <td>id of the Network for which the statistic is made</td>
56 * </tr>
57 * <tr>
58 * <td>numberMetadataEntries</td>
59 * <td>int</td>
60 * <td>number of metadata entries</td>
61 * </tr>
62 * <tr>
63 * <td>metadataId_1</td>
64 * <td>String</td>
65 * <td>id of the first metadata entry</td>
66 * </tr>
67 * <tr>
68 * <td>metadataType_1</td>
69 * <td>String</td>
70 * <td>type of metadata, one of GTUTYPE, ORIGIN, DESTINATION, ROUTE</td>
71 * </tr>
72 * <tr>
73 * <td>metadataValue_1</td>
74 * <td>String</td>
75 * <td>value of the first metadata entry</td>
76 * </tr>
77 * <tr>
78 * <td>...</td>
79 * <td> </td>
80 * <td> </td>
81 * </tr>
82 * <tr>
83 * <td>metadataId_n</td>
84 * <td>String</td>
85 * <td>id of the last metadata entry</td>
86 * </tr>
87 * <tr>
88 * <td>metadataType_n</td>
89 * <td>String</td>
90 * <td>type of metadata, one of GTUTYPE, ORIGIN, DESTINATION, ROUTE</td>
91 * </tr>
92 * <tr>
93 * <td>metadataValue_n</td>
94 * <td>String</td>
95 * <td>value of the last metadata entry</td>
96 * </tr>
97 * <td>numberSpaceTimeRegions</td>
98 * <td>int</td>
99 * <td>number of space-time regions for this statistic</td>
100 * </tr>
101 * <tr>
102 * <td>startTime_1</td>
103 * <td>double</td>
104 * <td>start time for the first space time region, in seconds</td>
105 * </tr>
106 * <tr>
107 * <td>endTime_1</td>
108 * <td>double</td>
109 * <td>end time for the first space time region, in seconds</td>
110 * </tr>
111 * <tr>
112 * <td>linkId_1</td>
113 * <td>String</td>
114 * <td>id of the first Link for the space-time region</td>
115 * </tr>
116 * <tr>
117 * <td>laneId_1</td>
118 * <td>String</td>
119 * <td>id of the first Lane in the link for the space-time region</td>
120 * </tr>
121 * <tr>
122 * <td>...</td>
123 * <td> </td>
124 * <td> </td>
125 * </tr>
126 * <tr>
127 * <td>startTime_n</td>
128 * <td>double</td>
129 * <td>start time for the last space time region, in seconds</td>
130 * </tr>
131 * <tr>
132 * <td>endTime_n</td>
133 * <td>double</td>
134 * <td>end time for the last space time region, in seconds</td>
135 * </tr>
136 * <tr>
137 * <td>linkId_n</td>
138 * <td>String</td>
139 * <td>id of the last Link for the space-time region</td>
140 * </tr>
141 * <tr>
142 * <td>laneId_n</td>
143 * <td>String</td>
144 * <td>id of the last Lane in the link for the space-time region</td>
145 * </tr>
146 * <tr>
147 * <td>connected</td>
148 * <td>boolean</td>
149 * <td>whether the lanes in the space-time regions are longitudinally connected or not</td>
150 * </tr>
151 * <tr>
152 * <td>transmissionInterval</td>
153 * <td>double</td>
154 * <td>transmission interval of the statistic in seconds</td>
155 * </tr>
156 * </tbody>
157 * </table>
158 * </p>
159 * <p>
160 * <h2>CHANGE</h2>
161 * <table style="width:800px;">
162 * <caption> </caption> <thead>
163 * <tr>
164 * <th style="width:25%;">Variable</th>
165 * <th style="width:15%;">Type</th>
166 * <th style="width:60%;">Comments</th>
167 * </tr>
168 * </thead><tbody>
169 * <tr>
170 * <td>timestamp</td>
171 * <td>double</td>
172 * <td>time of the event, in simulation time seconds</td>
173 * </tr>
174 * <tr>
175 * <td>statisticId</td>
176 * <td>String</td>
177 * <td>the unique id for the statistic, e.g. a UUID string</td>
178 * </tr>
179 * <tr>
180 * <td>totalGtuDistance</td>
181 * <td>double</td>
182 * <td>total distance traveled by filtered GTUs in the given time and space, in meters</td>
183 * </tr>
184 * <tr>
185 * <td>totalGtuTravelTime</td>
186 * <td>double</td>
187 * <td>total travel time by filtered GTUs in the given time and space, in seconds</td>
188 * </tr>
189 * <tr>
190 * <td>averageGtuSpeed</td>
191 * <td>double</td>
192 * <td>average filtered GTU speed in the given time and space, in meter/second</td>
193 * </tr>
194 * <tr>
195 * <td>averageGtuTravelTimePerKm</td>
196 * <td>double</td>
197 * <td>average filtered GTU travel time in the given time and space, in seconds per km</td>
198 * </tr>
199 * <tr>
200 * <td>totalGtuTimeDelay</td>
201 * <td>double</td>
202 * <td>total time delay incurred by the filtered GTUs in the given time and space, in seconds</td>
203 * </tr>
204 * <tr>
205 * <td>averageTripLength</td>
206 * <td>double</td>
207 * <td>average length of the trip of the filtered GTUs in the given time and space, in seconds</td>
208 * </tr>
209 * <tr>
210 * <td>totalNumberStops</td>
211 * <td>double</td>
212 * <td>total number of stops that GTUs made in the given time and space, dimensionless</td>
213 * </tr>
214 * </tbody>
215 * </table>
216 * </p>
217 * <p>
218 * <h2>DELETE</h2>
219 * <table style="width:800px;">
220 * <caption> </caption> <thead>
221 * <tr>
222 * <th style="width:25%;">Variable</th>
223 * <th style="width:15%;">Type</th>
224 * <th style="width:60%;">Comments</th>
225 * </tr>
226 * </thead><tbody>
227 * <tr>
228 * <td>timestamp</td>
229 * <td>double</td>
230 * <td>time of the event, in simulation time seconds</td>
231 * </tr>
232 * <tr>
233 * <td>statisticId</td>
234 * <td>String</td>
235 * <td>the unique id for the statistic that is removed</td>
236 * </tr>
237 * </tbody>
238 * </table>
239 * </p>
240 * <p>
241 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
242 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
243 * </p>
244 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
245 * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
246 */
247 public class Sim0mqKpiTransceiver
248 {
249
250 /** IMB connector. */
251 // private final Connector connector;
252
253 /** The query for the statistic. */
254 // private final Query query;
255
256 /** The Network id for which the graph is made. */
257 // private final String networkId;
258
259 /** The interval between generation of graphs. */
260 // private final Duration transmissionInterval;
261
262 private TotalTravelDistance totalTravelDistance = new TotalTravelDistance();
263
264 private TotalTravelTime totalTravelTime = new TotalTravelTime();
265
266 private MeanSpeed meanSpeed = new MeanSpeed(this.totalTravelDistance, this.totalTravelTime);
267
268 private MeanTravelTimePerDistance meanTravelTimePerKm = new MeanTravelTimePerDistance(this.meanSpeed);
269
270 private MeanTripLength meanTripLength = new MeanTripLength();
271
272 private TotalDelay totalDelay = new TotalDelay(new Speed(130.0, SpeedUnit.KM_PER_HOUR));
273
274 private TotalNumberOfStops totalNumberOfStops = new TotalNumberOfStops();
275
276 private Time updateTime = Time.ZERO;
277
278 // TODO implement DELETE message
279
280 // /**
281 // * Construct a new ImbKpiTransceiver.
282 // * @param connector the IMB connector
283 // * @param time time of creation
284 // * @param networkId the network id
285 // * @param query the statistics query
286 // * @param transmissionInterval the interval between generation of graphs
287 // * @throws IMBException when the post of the IMB message fails
288 // */
289 // public Sim0MQKpiTransceiver(final Connector connector, Time time, String networkId, final Query query,
290 // final Duration transmissionInterval) throws IMBException
291 // {
292 // this.connector = connector;
293 // this.query = query;
294 // this.networkId = networkId;
295 // this.transmissionInterval = transmissionInterval;
296 //
297 // List<Object> newMessage = new ArrayList<>();
298 // newMessage.add(time.si);
299 // newMessage.add(query.getId());
300 // newMessage.add(query.toString());
301 // newMessage.add(this.networkId);
302 // newMessage.add((int) 0); // TODO numberMetadataEntries
303 // newMessage.add((int) 0); // TODO numberSpaceTimeRegions
304 // newMessage.add(false); // TODO "connected" not part of query anymore
305 // newMessage.add(true); // TODO totalTrajectory
306 // newMessage.add(transmissionInterval.si);
307 //
308 // this.connector.postIMBMessage("StatisticsGTULane", IMBEventType.NEW, newMessage.toArray());
309 // sendStatisticsUpdate();
310 // }
311 //
312 // /**
313 // * Notifies about time, such that statistics over some period (very recently ended) can be gathered and published.
314 // * @param time the time to be used in the notification (usually the current time)
315 // */
316 // public void notifyTime(Time time)
317 // {
318 // if (time.gt(this.updateTime))
319 // {
320 // try
321 // {
322 // sendStatisticsUpdate();
323 // }
324 // catch (IMBException exception)
325 // {
326 // throw new OtsRuntimeException("Cannot send statistics update.", exception);
327 // }
328 // }
329 // }
330 //
331 // /**
332 // * @throws IMBException when the transmission of the IMB message fails
333 // */
334 // public void sendStatisticsUpdate() throws IMBException
335 // {
336 // Length tdist = this.totalTravelDistance.getValue(this.query, this.updateTime);
337 // Duration ttt = this.totalTravelTime.getValue(this.query, this.updateTime);
338 // Speed ms = this.meanSpeed.getValue(this.query, this.updateTime);
339 // Duration mttpkm = this.meanTravelTimePerKm.getValue(this.query, this.updateTime);
340 // Length mtl = this.meanTripLength.getValue(this.query, this.updateTime);
341 // Duration tdel = this.totalDelay.getValue(this.query, this.updateTime);
342 // Dimensionless nos = this.totalNumberOfStops.getValue(this.query, this.updateTime);
343 // double time = this.updateTime.si;
344 // System.out.println("===== @time " + time + " s =====");
345 // System.out.println("Total distance " + tdist);
346 // System.out.println("Total travel time " + ttt);
347 // System.out.println("Mean speed " + ms);
348 // System.out.println("Mean travel time " + mttpkm + " (per km)");
349 // System.out.println("Mean trip length " + mtl);
350 // System.out.println("Total delay " + tdel);
351 // System.out.println("Number of stops " + nos);
352 // this.connector.postIMBMessage("StatisticsGTULane", IMBEventType.CHANGE,
353 // new Object[] { time, this.query.getId(), tdist.si, ttt.si, ms.si, mttpkm.si, tdel.si, mtl.si, nos.si });
354 // this.updateTime = this.updateTime.plus(this.transmissionInterval);
355 // }
356
357 }