-
Notifications
You must be signed in to change notification settings - Fork 5
/
berlinmod_datagenerator.sql
1609 lines (1472 loc) · 56.6 KB
/
berlinmod_datagenerator.sql
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-----------------------------------------------------------------------------
-- BerlinMOD Data Generator
-------------------------------------------------------------------------------
This file is part of MobilityDB.
Copyright (C) 2020, Esteban Zimanyi, Mahmoud Sakr,
Universite Libre de Bruxelles.
The functions defined in this file use MobilityDB to generate data
similar to the data used in the BerlinMOD benchmark as defined in
http://dna.fernuni-hagen.de/secondo/BerlinMOD/BerlinMOD-FinalReview-2008-06-18.pdf
You can change parameters in the various functions of this file.
Usually, changing the master parameter 'P_SCALE_FACTOR' should do it.
But you also might be interested in changing parameters for the
random number generator, experiment with non-standard scaling
patterns or modify the sampling of positions.
The database must contain the following input relations.
* Nodes(id bigint primary key, geom geometry(Point))
* Edges(id bigint primary key, tag_id int, sourceNode bigint, targetNode bigint,
length_m float, cost_s float, reverse_cost_s float, maxspeed_forward float,
maxspeed_backward float, priority float, geom geometry(Linestring))
sourceNode and targetNode references Nodes(id)
The Nodes and Edges tables define the road network graph.
These tables are typically obtained by osm2pgrouting from OSM data.
The minimum number of attributes these tables should contain are
those defined above. The OSM tag 'highway' defines several of
the attributes and this is stated in the configuration file
for osm2pgrouting which looks as follows:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<tag_name name="highway" id="1">
<tag_value name="motorway" id="101" priority="1.0" maxspeed="120" />
[...]
<tag_value name="services" id="116" priority="4" maxspeed="20" />
</tag_name>
</configuration>
It is supposed that the Edges and the Nodes table define a connected
graph, that is, there is a path between every pair of nodes in the graph.
IF THIS CONDITION IS NOT SATISFIED THE GENERATION WILL FAIL.
Indeed, in that case pgRouting will return a NULL value when looking
for a path between two nodes.
* HomeRegions(id int primary key, priority int, weight int, prob float,
cumProb float, geom geometry)
* WorkRegions(id int primary key, priority int, weight int, prob float,
cumProb float, geom geometry)
priority indicates the region selection priority
weight is the relative weight to choose from the given region
geom is a (Multi)Polygon describing the region's area
* HomeNodes(id bigint primary key, osm_id bigint, geom geometry, region int)
* WorkNodes(id bigint primary key, osm_id bigint, geom geometry, region int)
The generated data is saved into the database in which the
functions are executed using the following tables
* Vehicles(vehId int primary key, licence text, type text, model text)
* VehicleNodes(vehId int primary key, home bigint, work bigint, noNeighbours int);
* Neighbourhood(vehId int, seqNo int, node bigint)
primary key (vehId, seqNo)
* Destinations(vehId int, sourceNode bigint, targetNode bigint)
primary key (vehId, sourceNode, targetNode)
* Paths(vehId int, start_vid bigint, end_vid bigint, seqNo int,
node bigint, edge bigint, geom geometry, speed float, category int);
* LeisureTrips(vehId int, day date, tripNo int, seqNo int, sourceNode bigint,
targetNode bigint)
primary key (vehId, day, tripNo, seqNo)
tripNo is 1 for morning/evening trip and is 2 for afternoon trip
seqNo is the sequence of trips composing a leisure trip
* Trips(tripId serial, vehId int, day date, seqNo int, sourceNode bigint,
targetNode bigint, trip tgeompoint, trajectory geometry);
* Licences(licenceId int primary key, licence text, vehId int)
* Points(pointId int, geom geometry)
* Regions(regionId int, geom geometry)
* Instants(instantId int, instant timestamptz)
* Periods(period int, period)
-----------------------------------------------------------------------------*/
-------------------------------------------------------------------------------
-- Functions generating random numbers according to various
-- probability distributions. Inspired from
-- https://stackoverflow.com/questions/9431914/gaussian-random-distribution-in-postgresql
-- https://bugfactory.io/blog/generating-random-numbers-according-to-a-continuous-probability-distribution-with-postgresql/
-------------------------------------------------------------------------------
-- Random integer in a range with uniform distribution
CREATE OR REPLACE FUNCTION random_int(low int, high int)
RETURNS int AS $$
BEGIN
RETURN floor(random() * (high - low + 1) + low);
END;
$$ LANGUAGE plpgsql STRICT;
/*
select random_int(1,7), count(*)
from generate_series(1, 1e3)
group by 1
order by 1
*/
-- Random integer with binomial distribution
CREATE OR REPLACE FUNCTION random_binomial(n int, p float)
RETURNS int AS $$
DECLARE
-- Loop variable
i int;
-- Result of the function
result float = 0;
BEGIN
IF n <= 0 OR p <= 0.0 OR p >= 1.0 THEN
RETURN NULL;
END IF;
FOR i IN 1..n LOOP
IF random() < p THEN
result = result + 1;
END IF;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql STRICT;
/*
with data as (
select random_binomial(100,0.5) AS r from generate_series(1,1e5) t
)
select min(r), max(r), avg(r)
from data;
-- Successfully run. Total query runtime: 40 secs 876 msec.
*/
-- Random float with exponential distribution
CREATE OR REPLACE FUNCTION random_exp(lambda float DEFAULT 1.0)
RETURNS float AS $$
DECLARE
-- Random value
r float;
BEGIN
IF lambda = 0.0 THEN
RETURN NULL;
END IF;
LOOP
r = random();
EXIT WHEN r <> 0.0;
END LOOP;
RETURN -1 * ln(r) * lambda;
END;
$$ LANGUAGE plpgsql STRICT;
/*
with data as (
select random_exp(1) AS r from generate_series(1,1e5) t
)
select min(r), max(r), avg(r)
from data;
-- Successfully run. Total query runtime: 6 min 18 secs.
*/
-- Random float with Gaussian distribution
CREATE OR REPLACE FUNCTION random_gauss(avg float = 0, stddev float = 1)
RETURNS float AS $$
DECLARE
x1 real; x2 real; w real;
BEGIN
LOOP
x1 = 2.0 * random() - 1.0;
x2 = 2.0 * random() - 1.0;
w = x1 * x1 + x2 * x2;
EXIT WHEN w < 1.0;
END LOOP;
RETURN avg + x1 * sqrt(-2.0*ln(w)/w) * stddev;
END;
$$ LANGUAGE plpgsql STRICT;
/*
with data as (
select t, random_gauss(100,15)::int score from generate_series(1,1000000) t
)
select score, sum(1), repeat('=',sum(1)::int/500) bar
from data
where score between 60 and 140
group by score
order by 1;
*/
-- Random float with a Gaussian distributed value within [Low, High]
CREATE OR REPLACE FUNCTION random_boundedgauss(low float, high float,
avg float = 0, stddev float = 1)
RETURNS float AS $$
DECLARE
-- Result of the function
result real;
BEGIN
result = random_gauss(avg, stddev);
IF result < low THEN
RETURN low;
ELSEIF result > high THEN
RETURN high;
ELSE
RETURN result;
END IF;
END;
$$ LANGUAGE plpgsql STRICT;
/*
select random_boundedgauss(-0.5, 0.5)
from generate_series(1, 1e2)
order by 1
*/
-------------------------------------------------------------------------------
-- Creates a random duration of length [0ms, 2h] using Gaussian
-- distribution
CREATE OR REPLACE FUNCTION createPause()
RETURNS interval AS $$
BEGIN
RETURN (((random_boundedgauss(-6.0, 6.0, 0.0, 1.4) * 100.0) + 600.0) * 6000.0)::int * interval '1 ms';
END;
$$ LANGUAGE plpgsql STRICT;
/*
with test(t) as (
select CreatePause()
from generate_series(1, 1e5)
order by 1
)
select min(t), max(t) from test
*/
-- Creates a random non-zero duration of length [2ms, N min - 4ms]
-- using a uniform distribution
CREATE OR REPLACE FUNCTION createPauseN(Minutes int)
RETURNS interval AS $$
BEGIN
RETURN ( 2 + random_int(1, Minutes * 60000 - 6) ) * interval '1 ms';
END;
$$ LANGUAGE plpgsql STRICT;
/*
with test(t) as (
select CreatePauseN(1)
from generate_series(1, 1e5)
order by 1
)
select min(t), max(t) from test
*/
-- Creates a normally distributed duration within [-Rhours h, +Rhours h]
CREATE OR REPLACE FUNCTION createDurationRhoursNormal(Rhours float)
RETURNS interval AS $$
DECLARE
-- Result of the function
result interval;
BEGIN
result = ((random_gauss() * Rhours * 1800000) / 86400000) * interval '1 d';
IF result > (Rhours / 24.0 ) * interval '1 d' THEN
result = (Rhours / 24.0) * interval '1 d';
ELSEIF result < (Rhours / -24.0 ) * interval '1 d' THEN
result = (Rhours / -24.0) * interval '1 d';
END IF;
RETURN result;
END
$$ LANGUAGE plpgsql STRICT;
/*
with test(t) as (
select CreateDurationRhoursNormal(12)
from generate_series(1, 1e5)
order by 1
)
select min(t), max(t) from test
*/
-------------------------------------------------------------------------------
-- Maps an OSM road type as defined in the tag 'highway' to one of
-- the three categories from BerlinMOD: freeway (1), main street (2),
-- side street (3)
/*
'motorway' id='101' priority='1.0' maxspeed='120' category='1'
'motorway_link' id='102' priority='1.0' maxspeed='120' category='1'
'motorway_junction' id='103' priority='1.0' maxspeed='120' category='1'
'trunk' id='104' priority='1.05' maxspeed='120' category='1'
'trunk_link' id='105' priority='1.05' maxspeed='120' category='1'
'primary' id='106' priority='1.15' maxspeed='90' category='2'
'primary_link' id='107' priority='1.15' maxspeed='90' category='1'
'secondary' id='108' priority='1.5' maxspeed='70' category='2'
'secondary_link' id='109' priority='1.5' maxspeed='70' category='2'
'tertiary' id='110' priority='1.75' maxspeed='50' category='2'
'tertiary_link' id='111' priority='1.75' maxspeed='50' category='2'
'residential' id='112' priority='2.5' maxspeed='30' category='3'
'living_street' id='113' priority='3' maxspeed='20' category='3'
'unclassified' id='114' priority='3' maxspeed='20' category='3'
'service' id='115' priority='4' maxspeed='20' category='3'
'services' id='116' priority='4' maxspeed='20' category='3'
*/
CREATE OR REPLACE FUNCTION berlinmod_roadCategory(tagId int)
RETURNS int AS $$
BEGIN
RETURN CASE
-- motorway, motorway_link, motorway_junction, trunk, trunk_link
WHEN tagId BETWEEN 101 AND 105 THEN 1 -- i.e., "freeway"
-- primary, primary_link, secondary, secondary_link, tertiary, tertiary_link
WHEN tagId BETWEEN 106 AND 111 THEN 2 -- i.e., "main street"
-- residential, living_street, unclassified, service, services
ELSE 3 -- i.e., "side street"
END;
END;
$$ LANGUAGE plpgsql STRICT;
-- Type combining the elements needed to define a path between source and
-- target nodes in the graph
DROP TYPE IF EXISTS step CASCADE;
CREATE TYPE step as (linestring geometry, maxspeed float, category int);
-- Call pgrouting to find a path between source and target nodes.
-- A path is composed of an array of steps (see the above type definition).
-- The last argument corresponds to the parameter P_PATH_MODE.
-- This function is currently not used in the generation but is useful
-- for debugging purposes.
CREATE OR REPLACE FUNCTION createPath(sourceN bigint, targetN bigint, pathMode text)
RETURNS step[] AS $$
DECLARE
-- Query sent to pgrouting depending on the argument pathMode
query_pgr text;
-- Result of the function
result step[];
BEGIN
IF pathMode = 'Fastest Path' THEN
query_pgr = 'SELECT id, sourceNode, targetNode, cost_s AS cost, reverse_cost_s as reverse_cost FROM Edges';
ELSE
query_pgr = 'SELECT id, sourceNode, targetNode, length_m AS cost, '
'length_m * sign(reverse_cost_s) as reverse_cost FROM Edges';
END IF;
WITH Temp1 AS (
SELECT P.seqNo, P.node, P.edge
FROM pgr_dijkstra(query_pgr, sourceN, targetN, true) P
),
Temp2 AS (
SELECT T.seqNo,
-- adjusting directionality
CASE
WHEN T.node = E.sourceNode THEN E.geom
ELSE ST_Reverse(geom)
END AS geom,
maxspeed_forward AS maxSpeed, berlinmod_roadCategory(tag_id) AS category
FROM Temp1 T, Edges E
WHERE edge IS NOT NULL AND E.id = T.edge
)
SELECT array_agg((geom, maxSpeed, category)::step ORDER BY seqNo) INTO result
FROM Temp2;
RETURN result;
END;
$$ LANGUAGE plpgsql STRICT;
/*
select createPath(9598, 4010, 'Fastest Path')
*/
-- Creates a trip following a path between a source and a target node starting
-- at a timestamp t. Implements Algorithm 1 in BerlinMOD Technical Report.
-- The last argument corresponds to the parameter P_DISTURB_DATA.
CREATE OR REPLACE FUNCTION createTrip(edges step[], startTime timestamptz,
disturbData boolean, messages text)
RETURNS tgeompoint AS $$
DECLARE
-------------------------
-- CONSTANT PARAMETERS --
-------------------------
-- Speed in km/h which is considered as a stop and thus only an
-- accelaration event can be applied
P_EPSILON_SPEED float = 1;
-- Used for determining whether the distance is almost equal to 0.0
P_EPSILON float = 0.0001;
-- The probability of an event is proportional to (P_EVENT_C)/Vmax.
-- The probability for an event being a forced stop is given by
-- 0.0 <= 'P_EVENT_P' <= 1.0 (the balance, 1-P, is meant to trigger
-- deceleration events).
P_EVENT_C float = 1.0;
P_EVENT_P float = 0.1;
-- Sampling distance in meters at which an acceleration/deceleration/stop
-- event may be generated.
P_EVENT_LENGTH float = 5.0;
-- Speed in Km/h that is added to the current speed in an acceleration event
P_EVENT_ACC float = 12.0;
-- Probabilities for forced stops at crossings by road type transition
-- defined by a matrix where lines and columns are ordered by
-- side road (S), main road (M), freeway (F). The OSM highway types must be
-- mapped to one of these categories using the function berlinmod_roadCategory
P_DEST_STOPPROB float[] =
'{{0.33, 0.66, 1.00}, {0.33, 0.50, 0.66}, {0.10, 0.33, 0.05}}';
-- Mean waiting time in seconds using an exponential distribution.
-- Increasing/decreasing this parameter allows us to slow down or speed up
-- the trips. Could be think of as a measure of network congestion.
-- Given a specific path, fine-tuning this parameter enable us to obtain
-- an average travel time for this path that is the same as the expected
-- travel time computed, e.g., by Google Maps.
P_DEST_EXPMU float = 1.0;
-- Parameters for measuring errors (only required for P_DISTURB_DATA = TRUE)
-- Maximum total deviation from the real position (default = 100.0)
-- and maximum deviation per step (default = 1.0) both in meters.
P_GPS_TOTALMAXERR float = 100.0;
P_GPS_STEPMAXERR float = 1.0;
---------------
-- Variables --
---------------
-- SRID of the geometries being manipulated
srid int;
-- Number of edges in a path, number of segments in an edge,
-- number of fractions of size P_EVENT_LENGTH in a segment
noEdges int; noSegs int; noFracs int;
-- Loop variables
edge int; seg int; frac int;
-- Number of instants generated so far
l int;
-- Categories of the current and next road
category int; nextCategory int;
-- Current speed and distance of the moving car
curSpeed float; curDist float;
-- Time to wait and total wait time
waitTime float; totalWaitTime float = 0.0;
-- Time to travel the fraction given the current speed and total travel time
travelTime float; totalTravelTime float = 0.0;
-- Angle between the current segment and the next one
alpha float;
-- Maximum speed of an edge
maxSpeedEdge float;
-- Maximum speed of a turn between two segments as determined
-- by their angle
maxSpeedTurn float;
-- Maximum speed and new speed of the car
maxSpeed float; newSpeed float;
-- Coordinates of the next point
x float; y float;
-- Coordinates of p1 and p2
x1 float; y1 float; x2 float; y2 float;
-- Number in [0,1] used for determining the next point
fraction float;
-- Disturbance of the coordinates of a point and total accumulated
-- error in the coordinates of an edge. Used when disturbing the position
-- of an object to simulate GPS errors
dx float; dy float;
errx float = 0.0; erry float = 0.0;
-- Length of a segment and maximum speed of an edge
segLength float;
-- Geometries of the current edge
linestring geometry;
-- Points of the current linestring
points geometry [];
-- Start and end points of segment of a linestring
p1 geometry; p2 geometry;
-- Next point (if any) after p2 in the same edge
p3 geometry;
-- Current position of the moving object
curPos geometry;
-- Current timestamp of the moving object
t timestamptz;
-- Instants of the result being constructed
instants tgeompoint[];
-- Statistics about the trip
noAccel int = 0;
noDecel int = 0;
noStop int = 0;
twSumSpeed float = 0.0;
BEGIN
srid = ST_SRID((edges[1]).linestring);
p1 = ST_PointN((edges[1]).linestring, 1);
x1 = ST_X(p1);
y1 = ST_Y(p1);
curPos = p1;
t = startTime;
curSpeed = 0;
instants[1] = tgeompoint_inst(p1, t);
l = 2;
noEdges = array_length(edges, 1);
-- Loop for every edge
FOR edge IN 1..noEdges LOOP
IF messages = 'debug' THEN
RAISE INFO ' Edge %', edge;
END IF;
-- Get the information about the current edge
linestring = (edges[edge]).linestring;
maxSpeedEdge = (edges[edge]).maxSpeed;
category = (edges[edge]).category;
SELECT array_agg(geom ORDER BY path) INTO points
FROM ST_DumpPoints(linestring);
noSegs = array_length(points, 1) - 1;
-- Loop for every segment of the current edge
FOR seg IN 1..noSegs LOOP
IF messages = 'debug' AND noSegs > 1 THEN
RAISE INFO ' Segment %', seg;
END IF;
p2 = points[seg + 1];
x2 = ST_X(p2);
y2 = ST_Y(p2);
-- If there is a segment ahead in the current edge
-- compute the angle of the turn
IF seg < noSegs THEN
p3 = points[seg + 2];
-- Compute the angle α between the current segment and the next one;
alpha = degrees(ST_Angle(p1, p2, p3));
-- Compute the maximum speed at the turn by multiplying the
-- maximum speed by a factor proportional to the angle so that
-- the factor is 1.00 at 0/360° and is 0.0 at 180°, e.g.
-- 0° -> 1.00, 5° 0.97, 45° 0.75, 90° 0.50, 135° 0.25, 175° 0.03
-- 180° 0.00, 185° 0.03, 225° 0.25, 270° 0.50, 315° 0.75, 355° 0.97, 360° 0.00
IF abs(mod(alpha::numeric, 360.0)) < P_EPSILON THEN
maxSpeedTurn = maxSpeedEdge;
ELSE
maxSpeedTurn = mod(abs(alpha - 180.0)::numeric, 180.0) / 180.0 * maxSpeedEdge;
END IF;
END IF;
segLength = ST_Distance(p1, p2);
IF segLength < P_EPSILON THEN
RAISE EXCEPTION 'Segment % of edge % has zero length', seg, edge;
END IF;
fraction = P_EVENT_LENGTH / segLength;
noFracs = ceiling(segLength / P_EVENT_LENGTH);
-- Loop for every fraction of the current segment
frac = 1;
WHILE frac < noFracs LOOP
-- If the current speed is considered as a stop, apply an
-- acceleration event where the new speed is bounded by the
-- maximum speed of either the segment or the turn
IF curSpeed <= P_EPSILON_SPEED THEN
noAccel = noAccel + 1;
-- If we are not approaching a turn
IF frac < noFracs THEN
curSpeed = least(P_EVENT_ACC, maxSpeedEdge);
ELSE
curSpeed = least(P_EVENT_ACC, maxSpeedTurn);
END IF;
IF messages = 'debug' THEN
RAISE INFO ' Acceleration after stop -> Speed = %', round(curSpeed::numeric, 3);
END IF;
ELSE
-- If the current speed is not considered as a stop,
-- with a probability proportional to P_EVENT_C/vmax apply
-- a deceleration event (p=90%) or a stop event (p=10%)
IF random() <= P_EVENT_C / maxSpeedEdge THEN
IF random() <= P_EVENT_P THEN
-- Apply stop event to the trip
curSpeed = 0.0;
noStop = noStop + 1;
IF messages = 'debug' THEN
RAISE INFO ' Stop -> Speed = %', round(curSpeed::numeric, 3);
END IF;
ELSE
-- Apply deceleration event to the trip
curSpeed = curSpeed * random_binomial(20, 0.5) / 20.0;
noDecel = noDecel + 1;
IF messages = 'debug' THEN
RAISE INFO ' Deceleration -> Speed = %', round(curSpeed::numeric, 3);
END IF;
END IF;
ELSE
-- Apply an acceleration event. The speed is bound by
-- (1) the maximum speed of the turn if we are within
-- an edge, or (2) the maximum speed of the edge
IF frac = noFracs AND seg < noSegs THEN
maxSpeed = maxSpeedTurn;
IF messages = 'debug' THEN
RAISE INFO ' Turn -> Angle = %, Maximum speed at turn = %', round(alpha::numeric, 3), round(maxSpeedTurn::numeric, 3);
END IF;
ELSE
maxSpeed = maxSpeedEdge;
END IF;
newSpeed = least(curSpeed + P_EVENT_ACC, maxSpeed);
IF curSpeed < newSpeed THEN
noAccel = noAccel + 1;
IF messages = 'debug' THEN
RAISE INFO ' Acceleration -> Speed = %', round(newSpeed::numeric, 3);
END IF;
ELSIF curSpeed > newSpeed THEN
noDecel = noDecel + 1;
IF messages = 'debug' THEN
RAISE INFO ' Deceleration -> Speed = %', round(newSpeed::numeric, 3);
END IF;
END IF;
curSpeed = newSpeed;
END IF;
END IF;
-- If speed is zero add a wait time
IF curSpeed < P_EPSILON_SPEED THEN
waitTime = random_exp(P_DEST_EXPMU);
IF waitTime < P_EPSILON THEN
waitTime = P_DEST_EXPMU;
END IF;
t = t + waitTime * interval '1 sec';
totalWaitTime = totalWaitTime + waitTime;
IF messages = 'debug' THEN
RAISE INFO ' Waiting for % seconds', round(waitTime::numeric, 3);
END IF;
ELSE
-- Otherwise, move current position P_EVENT_LENGTH meters towards p2
-- or to p2 if it is the last fraction
IF frac < noFracs THEN
x = x1 + ((x2 - x1) * fraction * frac);
y = y1 + ((y2 - y1) * fraction * frac);
IF disturbData THEN
dx = (2 * P_GPS_STEPMAXERR * rand()) - P_GPS_STEPMAXERR;
dy = (2 * P_GPS_STEPMAXERR * rand()) - P_GPS_STEPMAXERR;
errx = errx + dx;
erry = erry + dy;
IF errx > P_GPS_TOTALMAXERR THEN
errx = P_GPS_TOTALMAXERR;
END IF;
IF errx < - 1 * P_GPS_TOTALMAXERR THEN
errx = -1 * P_GPS_TOTALMAXERR;
END IF;
IF erry > P_GPS_TOTALMAXERR THEN
erry = P_GPS_TOTALMAXERR;
END IF;
IF erry < -1 * P_GPS_TOTALMAXERR THEN
erry = -1 * P_GPS_TOTALMAXERR;
END IF;
x = x + dx;
y = y + dy;
END IF;
curPos = ST_SetSRID(ST_Point(x, y), srid);
curDist = P_EVENT_LENGTH;
ELSE
curPos = p2;
curDist = segLength - (segLength * fraction * (frac - 1));
END IF;
travelTime = (curDist / (curSpeed / 3.6));
IF travelTime < P_EPSILON THEN
travelTime = P_DEST_EXPMU;
END IF;
t = t + travelTime * interval '1 sec';
totalTravelTime = totalTravelTime + travelTime;
twSumSpeed = twSumSpeed + (travelTime * curSpeed);
frac = frac + 1;
END IF;
instants[l] = tgeompoint_inst(curPos, t);
l = l + 1;
END LOOP;
p1 = p2;
x1 = x2;
y1 = y2;
END LOOP;
-- If we are not already in a stop, apply a stop event with a
-- probability depending on the category of the current edge
-- and the next one (if any)
IF curSpeed > P_EPSILON_SPEED AND edge < noEdges THEN
nextCategory = (edges[edge + 1]).category;
IF random() <= P_DEST_STOPPROB[category][nextCategory] THEN
curSpeed = 0;
waitTime = random_exp(P_DEST_EXPMU);
IF waitTime < P_EPSILON THEN
waitTime = P_DEST_EXPMU;
END IF;
t = t + waitTime * interval '1 sec';
totalWaitTime = totalWaitTime + waitTime;
IF messages = 'debug' THEN
RAISE INFO ' Stop at crossing -> Waiting for % seconds', round(waitTime::numeric, 3);
END IF;
instants[l] = tgeompoint_inst(curPos, t);
l = l + 1;
END IF;
END IF;
END LOOP;
IF messages = 'verbose' OR messages = 'debug' THEN
RAISE INFO ' Number of edges %', noEdges;
RAISE INFO ' Number of acceleration events: %', noAccel;
RAISE INFO ' Number of deceleration events: %', noDecel;
RAISE INFO ' Number of stop events: %', noStop;
RAISE INFO ' Total travel time: % secs.', round(totalTravelTime::numeric, 3);
RAISE INFO ' Total waiting time: % secs.', round(totalWaitTime::numeric, 3);
RAISE INFO ' Time-weighted average speed: % Km/h',
round((twSumSpeed / (totalTravelTime + totalWaitTime))::numeric, 3);
END IF;
RETURN tgeompoint_seq(instants, true, true, true);
-- RETURN instants;
END;
$$ LANGUAGE plpgsql STRICT;
/*
WITH Temp(trip) AS (
SELECT createTrip(createPath(34125, 44979, 'Fastest Path'), '2020-05-10 08:00:00', false, 'minimal')
)
SELECT startTimestamp(trip), endTimestamp(trip), timespan(trip)
FROM Temp;
*/
-------------------------------------------------------------------------------
-- Choose a random home, work, or destination node for the region-based
-- approach
CREATE OR REPLACE FUNCTION berlinmod_selectHomeNode()
RETURNS bigint AS $$
DECLARE
-- Result of the function
result bigint;
BEGIN
WITH RandomRegion AS (
SELECT id
FROM HomeRegions
WHERE random() <= cumProb
ORDER BY cumProb
LIMIT 1
)
SELECT N.id INTO result
FROM HomeNodes N, RandomRegion R
WHERE N.region = R.id
ORDER BY random()
LIMIT 1;
RETURN result;
END;
$$ LANGUAGE plpgsql STRICT;
/*
-- WE DON'T COVER ALL REGIONS EVEN AFTER 1e5 attempts
with temp(node) as (
select berlinmod_selectHomeNode()
from generate_series(1, 1e5)
)
select region, count(*)
from temp T, homenodes N
where t.node = id
group by region order by region;
-- Total query runtime: 3 min 6 secs.
*/
CREATE OR REPLACE FUNCTION berlinmod_selectWorkNode()
RETURNS int AS $$
DECLARE
-- Result of the function
result bigint;
BEGIN
WITH RandomRegion AS (
SELECT id
FROM WorkRegions
WHERE random() <= cumProb
ORDER BY cumProb
LIMIT 1
)
SELECT N.id INTO result
FROM WorkNodes N, RandomRegion R
WHERE N.region = R.id
ORDER BY random()
LIMIT 1;
RETURN result;
END;
$$ LANGUAGE plpgsql STRICT;
/*
-- WE DON'T COVER ALL REGIONS EVEN AFTER 1e5 attempts
with temp(node) as (
select berlinmod_selectWorkNode()
from generate_series(1, 1e5)
)
select region, count(*)
from temp T, homenodes N
where t.node = id
group by region order by region;
-- Total query runtime: 3 min.
*/
-- Selects a destination node for an additional trip. 80% of the
-- destinations are from the neighbourhood, 20% are from the complete graph
CREATE OR REPLACE FUNCTION berlinmod_selectDestNode(veh int, noNeigh int,
noNodes int)
RETURNS bigint AS $$
DECLARE
-- Random sequence number
seq int;
-- Result of the function
result bigint;
BEGIN
IF noNeigh > 0 AND random() < 0.8 THEN
seq = random_int(1, noNeigh);
SELECT node INTO result
FROM Neighbourhood
WHERE vehId = veh AND seqNo = seq;
ELSE
result = random_int(1, noNodes);
END IF;
RETURN result;
END;
$$ LANGUAGE plpgsql STRICT;
/*
SELECT berlinmod_selectDestNode(150)
FROM generate_series(1, 50)
ORDER BY 1;
*/
-- Return the unique licence string for a given vehicle identifier
-- where the identifier is in [0,26999]
CREATE OR REPLACE FUNCTION berlinmod_createLicence(vehicId int)
RETURNS text AS $$
BEGIN
IF vehicId > 0 and vehicId < 1000 THEN
RETURN text 'B-' || chr(random_int(1, 26) + 65) || chr(random_int(1, 25) + 65)
|| ' ' || vehicId::text;
ELSEIF vehicId % 1000 = 0 THEN
RETURN text 'B-' || chr((vehicId % 1000) + 65) || ' '
|| (random_int(1, 998) + 1)::text;
ELSE
RETURN text 'B-' || chr((vehicId % 1000) + 64) || 'Z '
|| (vehicId % 1000)::text;
END IF;
END;
$$ LANGUAGE plpgsql STRICT;
/*
SELECT berlinmod_createLicence(random_int(1,100))
FROM generate_series(1, 10);
*/
-- Return a random vehicle type with the following values
-- passenger (p=90%), bus (p=5%), truck (p=5%)
CREATE OR REPLACE FUNCTION berlinmod_vehicleType()
RETURNS text AS $$
DECLARE
-------------------------
-- CONSTANT PARAMETERS --
-------------------------
P_VEHICLE_TYPES text[] = '{"passenger", "bus", "truck"}';
BEGIN
IF random() < 0.9 THEN
RETURN P_VEHICLE_TYPES[1];
ELSEIF random() < 0.5 THEN
RETURN P_VEHICLE_TYPES[2];
ELSE
RETURN P_VEHICLE_TYPES[3];
END IF;
END;
$$ LANGUAGE plpgsql STRICT;
/*
SELECT berlinmod_vehicleType(), COUNT(*)
FROM generate_series(1, 1e5)
GROUP BY 1
ORDER BY 1;
*/
-- Return a random vehicle model with a uniform distribution
CREATE OR REPLACE FUNCTION berlinmod_vehicleModel()
RETURNS text AS $$
DECLARE
-------------------------
-- CONSTANT PARAMETERS --
-------------------------
P_VEHICLE_MODELS text[] = '{"Mercedes-Benz", "Volkswagen", "Maybach",
"Porsche", "Opel", "BMW", "Audi", "Acabion", "Borgward", "Wartburg",
"Sachsenring", "Multicar"}';
---------------
-- Variables --
---------------
index int;
BEGIN
index = random_int(1, array_length(P_VEHICLE_MODELS, 1));
RETURN P_VEHICLE_MODELS[index];
END;
$$ LANGUAGE plpgsql STRICT;
/*
SELECT berlinmod_vehicleModel(), COUNT(*)
FROM generate_series(1, 1e5)
GROUP BY 1
ORDER BY 1;
*/
-- Generate the trips for a given number vehicles and days starting at a day.
-- The argument disturbData correspond to the parameter P_DISTURB_DATA
CREATE OR REPLACE FUNCTION berlinmod_createTrips(noVehicles int, noDays int,
startDay date, disturbData boolean, messages text, tripGeneration text)
RETURNS void AS $$
DECLARE
-- Loops over the days for which we generate the data
d date;
-- 0 (Sunday) to 6 (Saturday)
weekday int;
-- Current timestamp
t timestamptz;
-- Temporal point obtained from a path
trip tgeompoint;
-- Home and work nodes
homeN bigint; workN bigint;
-- Source and target nodes of one subtrip of a leisure trip
sourceN bigint; targetN bigint;
-- Paths betwen source and target nodes
homework step[]; workhome step[]; path step[];
-- Number of leisure trips and number of subtrips of a leisure trip
noLeisTrip int; noSubtrips int;
-- Morning or afternoon (1 or 2) leisure trip
leisNo int;
-- Number of previous trips generated so far
tripSeq int = 0;
-- Loop variables
veh int; j int; leis int; dest int;
BEGIN
RAISE INFO 'Creation of the Trips table started at %', clock_timestamp();
DROP TABLE IF EXISTS Trips;
CREATE TABLE Trips(tripId SERIAL PRIMARY KEY, vehId int, day date, seqNo int,
sourceNode bigint, targetNode bigint, trip tgeompoint, trajectory geometry,
UNIQUE (vehId, day, seqNo));
-- Loop for each vehicle
FOR veh IN 1..noVehicles LOOP
IF messages = 'medium' OR messages = 'verbose' THEN
RAISE INFO '-- Vehicle %', veh;
ELSEIF veh % 100 = 1 THEN
RAISE INFO ' Vehicles % to %', veh, least(veh + 99, noVehicles);
END IF;
-- Get home -> work and work -> home paths
SELECT homeNode, workNode INTO homeN, workN
FROM VehicleNodes V WHERE V.vehId = veh;
SELECT array_agg((geom, speed, category)::step ORDER BY seqNo) INTO homework
FROM Paths
WHERE vehId = veh AND start_vid = homeN AND end_vid = workN;
SELECT array_agg((geom, speed, category)::step ORDER BY seqNo) INTO workhome
FROM Paths
WHERE vehId = veh AND start_vid = workN AND end_vid = homeN;
d = startDay;
-- Loop for each generation day
FOR j IN 1..noDays LOOP
IF messages = 'verbose' THEN
RAISE INFO ' -- Day %', d;
END IF;
weekday = date_part('dow', d);
-- 1: Monday, 5: Friday
IF weekday BETWEEN 1 AND 5 THEN
-- Home -> Work
t = d + time '08:00:00' + CreatePauseN(120);
IF messages = 'verbose' OR messages = 'debug' THEN
RAISE INFO ' Home to work trip started at %', t;
END IF;
IF tripGeneration = 'C' THEN
trip = create_trip(homework, t, disturbData, messages);
ELSE
trip = createTrip(homework, t, disturbData, messages);
END IF;
IF messages = 'medium' THEN
RAISE INFO ' Home to work trip started at % and lasted %',
t, endTimestamp(trip) - startTimestamp(trip);
END IF;
INSERT INTO Trips(vehId, day, seqNo, sourceNode, targetNode, trip, trajectory) VALUES
(veh, d, 1, homeN, workN, trip, trajectory(trip));
-- Work -> Home
t = d + time '16:00:00' + CreatePauseN(120);
IF messages = 'verbose' OR messages = 'debug' THEN
RAISE INFO ' Work to home trip started at %', t;
END IF;
IF tripGeneration = 'C' THEN
trip = create_trip(workhome, t, disturbData, messages);
ELSE
trip = createTrip(workhome, t, disturbData, messages);
END IF;
IF messages = 'medium' THEN
RAISE INFO ' Work to home trip started at % and lasted %',
t, endTimestamp(trip) - startTimestamp(trip);
END IF;
INSERT INTO Trips(vehId, day, seqNo, sourceNode, targetNode, trip, trajectory) VALUES
(veh, d, 2, workN, homeN, trip, trajectory(trip));
tripSeq = 2;
END IF;
-- Get the number of leisure trips
SELECT COUNT(DISTINCT tripNo) INTO noLeisTrip
FROM LeisureTrips L
WHERE L.vehId = veh AND L.day = d;
IF noLeisTrip = 0 AND messages = 'verbose' or messages = 'debug' THEN
RAISE INFO ' No leisure trip';
END IF;
-- Loop for each leisure trip (0, 1, or 2)
FOR leis IN 1..noLeisTrip LOOP
IF weekday BETWEEN 1 AND 5 THEN
t = d + time '20:00:00' + CreatePauseN(90);
IF messages = 'medium' THEN
RAISE INFO ' Weekday leisure trips started at %', t;
END IF;
leisNo = 1;