-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic_manager.py
More file actions
1696 lines (1537 loc) · 94.2 KB
/
traffic_manager.py
File metadata and controls
1696 lines (1537 loc) · 94.2 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
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
#import native python libraries
import sqlite3
from random import randint
from datetime import datetime
import threading
import os
import sys
import shutil
from multiprocessing import Process
import time
import datetime
import json
#import external libraries
from asq.initiators import query
import xml.etree.ElementTree as ET
import utm
#import internal classes
import utility
from person import Person
import config
import traci_helper
from logging_detail import get_logger
tools = os.path.join(os.getcwd(), 'sumo_tools')
sys.path.append(tools)
import traci as traci # @UnresolvedImport
#logging info
logger = get_logger('virtual_oulu', 'logs/virtual_oulu.log')
logger.info('start app')
#log people action
people_logger = get_logger('people_logger', 'logs/people_virtual_oulu.log')
class TrafficManager(threading.Thread):
'''
TODO:
1. Skip to the soonest action
2. Create a class to control people (should run by different thread)
'''
#old data version
OFFSET_X = -414982.40
OFFSET_Y = -7194168.66
def __init__(self, database_file=None, network_file=None, network_node_file=None, sumo_network_file=None, simulation_config_file=None):
super(TrafficManager, self).__init__()
# private variables definition
min_lat = 64.864240
max_lat = 65.118303
min_lon = 25.196334
max_lon = 25.844493
#public variables
self.sumo_port = config.SUMO_PORT
if (database_file is None):
self.database_file = config.DATABASE_FILE
else:
self.database_file = database_file
if (network_file is None):
self.network_file = config.NETWORK_FILE
else:
self.network_file = network_file
if (network_node_file is None):
self.network_node_file = config.NETWORK_NODE_FILE
else:
self.network_node_file = network_node_file
if (sumo_network_file is None):
self.sumo_network_file = config.SUMO_NETWORK_FILE
else:
self.sumo_network_file = sumo_network_file
if (simulation_config_file is None):
self.simulation_config_file = config.SIMULATION_CONFIG_FILE
else:
self.simulation_config_file = simulation_config_file
# public variables definition
self.workplaces = []
self.leisure_places = []
self.restaurant_places = []
self.schools = []
self.stores = []
self.female_first_names = []
self.male_first_names = []
self.last_names = []
self.residentials = []
self.residential_count = 0
self.car_lanes = []
self.car_lanes_count = 0
self.people = []
self.people_controllers = []
self.traci_action_queue = []
self.congested_places = []
self.current_step = 0
self.simulating_time = 0
self.vehicles = []
self.current_day = 0
self.subscribed_cars = {}
self.vehicles_positions = {}
# database connection
conn = sqlite3.connect(self.database_file)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
# network edges connection
tree = ET.parse(self.network_file)
root = tree.getroot()
# get places
fetch_places = "select * from places where is_coord_get=1 and edge_id is not null"
cur.execute(fetch_places)
rows = cur.fetchall()
for row in rows:
place = {}
place['name'] = row['name']
place['address'] = row['address']
place['lat'] = float(row['lat'])
place['lon'] = float(row['lon'])
place['id'] = row['id']
place['type'] = row['type']
place['edge_id'] = row['edge_id']
# if the place's coordinate inside Oulu => added to its type list
if (place['lat'] > min_lat
and place['lat'] < max_lat
and place['lon'] > min_lon
and place['lon'] < max_lon):
if row['main_type'] == 'leisure':
self.leisure_places.append(place)
elif row['main_type'] == 'meal':
self.restaurant_places.append(place)
elif row['type'] == 'university':
self.schools.append(place)
elif row['main_type'] == 'store':
self.stores.append(place)
elif row['main_type'] == 'workplace':
self.workplaces.append(place)
# get first names
fetch_first_names = "select * from first_names"
cur.execute(fetch_first_names)
rows = cur.fetchall()
for row in rows:
if row['gender'] == 'M':
self.male_first_names.append(row['name'])
elif row['gender'] == 'F':
self.female_first_names.append(row['name'])
# get last names
fetch_last_names = "select * from last_names"
cur.execute(fetch_last_names)
rows = cur.fetchall()
for row in rows:
self.last_names.append(row['name'])
# get vehicles
fetch_last_names = "select * from car_types"
cur.execute(fetch_last_names)
rows = cur.fetchall()
for row in rows:
self.vehicles.append({"id": row['id'],
"name": row['name'],
"accel": row['accel'],
"decel": row['decel'],
"sigma": row['sigma'],
"length": row['length'],
"min_gap": row['minGap'],
"max_speed": row['maxSpeed'],
"model_file": row['model_file'],
"description": row['description']})
# get list of residential edges
for edge in root.iter('edge'):
if (edge.get('type') == 'highway.residential' and self.non_car_lanes_count(edge)<int(edge.get('numLanes'))):
self.residential_count += 1
self.residentials.append(edge.get('id'))
# get list of edges that normal can access
''' condition (or):
1. Have no <lane> child
2. Number of lanes higher than non-car lanes
- None car lanes: exists in <lane> child tag with allow='delivery', 'pedestrian', vip, bicycle, rail
'''
for edge in root.iter('edge'):
if self.non_car_lanes_count(edge) < int(edge.attrib['numLanes']):
self.car_lanes.append({'id': edge.attrib['id'], 'from': edge.attrib['from'], 'to': edge.attrib['to']})
self.car_lanes_count += 1
# close database connection
conn.close()
# all preparing finished, start loading people
self.load_people(config.TOTAL_PEOPLE)
def non_car_lanes_count(self, edge):
lanes = edge.findall("./lane")
if len(lanes) == 0:
return 0
non_car_lanes = 0
for lane in lanes:
if (lane.attrib['allow'] in ['delivery', 'pedestrian', 'vip', 'bicycle', 'rail']):
non_car_lanes += 1
return non_car_lanes
def to_latlon(self, x, y):
lat, lon = utm.to_latlon(x - TrafficManager.OFFSET_X, y - TrafficManager.OFFSET_Y, 35, zone_letter='W')
return (lat, lon)
def from_latlon(self, lat, lon):
latlon = utm.from_latlon(lat, lon)
return (latlon[0] + TrafficManager.OFFSET_X, latlon[1] + TrafficManager.OFFSET_Y)
def find_nearest_edge(self, x, y):
'''
Distance from a node to an edge: the smallest between those 3 distances
- Distance from the node to the "from" node of the edge
- Distance from the node to the "to" node of the edge
- Distance from the node to the line
'''
conn = sqlite3.connect(self.database_file)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute('select * from oulu_edges')
edges = cur.fetchall()
nearest_distance = 9999999999999999
nearest_edge = None
for edge in edges:
if edge['numLanes'] > 0:
distance = 0
cur.execute('select * from oulu_nodes where id=?', (edge['from'],))
node_from = cur.fetchone()
cur.execute('select * from oulu_nodes where id=?', (edge['to'],))
node_to = cur.fetchone()
x1 = float(node_from['x'])
y1 = float(node_from['y'])
x2 = float(node_to['x'])
y2 = float(node_to['y'])
distance = utility.calculate_distance(x, y, x1, y1, x2, y2)
# choose the nearest distance
if (distance < nearest_distance):
nearest_distance = distance
nearest_edge = edge
return nearest_edge
def create_random_person(self, number_of_day=1):
''' THE NUMBERS ARE ALL MADE UP AND NEED TO BE CHANGED AFTER PROPER SURVEY
- Age from 18 to 55 (either going to school or working)
+ 18-22: 35% going to school, 55% working, 10% stay at home
+ 23-28: 65% going to school, 25% working, 10% stay at home
+ 29-55: 80% working, 20% stay at home
- Gender random between M and F (50-50)
- Name random
- Workplace is random
- School is a random university
- home is a random edge in network file
'''
new_person = Person()
# the person's id
new_person.id = utility.generate_random_string(20)
# the person's age
new_person.age = randint(18,55)
#the person's gender
chance_gender = randint(1,100)
if chance_gender<=50:
new_person.gender = Person.GENDER_FEMALE
else:
new_person.gender = Person.GENDER_MALE
#the person's name
chance_first_name = randint(1,100)
chance_last_name = randint(0, len(self.last_names)-1)
if (new_person.gender == Person.GENDER_FEMALE):
new_person.name = self.female_first_names[chance_first_name-1]
else:
new_person.name = self.male_first_names[chance_first_name-1]
new_person.name = new_person.name + ' ' + self.last_names[chance_last_name]
#the person's working status
chance_working_status = randint(1,100)
if (new_person.age>=18 and new_person.age<=22):
if chance_working_status <= 70:
new_person.working_status = Person.STATUS_STUDYING
elif chance_working_status <= 90:
new_person.working_status = Person.STATUS_WORKING
else:
new_person.working_status = Person.STATUS_STAY_AT_HOME
elif (new_person.age>=23 and new_person.age<=28):
if chance_working_status <= 55:
new_person.working_status = Person.STATUS_STUDYING
elif chance_working_status <= 90:
new_person.working_status = Person.STATUS_WORKING
else:
new_person.working_status = Person.STATUS_STAY_AT_HOME
else:
if chance_working_status <= 80:
new_person.working_status = Person.STATUS_WORKING
else:
new_person.working_status = Person.STATUS_STAY_AT_HOME
# the person's workplace
if (new_person.working_status == Person.STATUS_WORKING):
chance_workplace = randint(0, len(self.workplaces)-1)
new_person.work_address = { 'id': self.workplaces[chance_workplace]['id'],
'name': self.workplaces[chance_workplace]['name'],
'address': self.workplaces[chance_workplace]['address'],
'lat': self.workplaces[chance_workplace]['lat'],
'lon': self.workplaces[chance_workplace]['lon'],
'edge_id': self.workplaces[chance_workplace]['edge_id']}
# the person's university
if (new_person.working_status == Person.STATUS_STUDYING):
chance_university = randint(0, len(self.schools)-1)
new_person.school_address = { 'id': self.schools[chance_university]['id'],
'name': self.schools[chance_university]['name'],
'address': self.schools[chance_university]['address'],
'lat': self.schools[chance_university]['lat'],
'lon': self.schools[chance_university]['lon'],
'edge_id': self.schools[chance_university]['edge_id']}
# the person's house
chance_house = randint(0, self.residential_count-1)
new_person.home_address['edge_id'] = self.residentials[chance_house]
# the person's car
chance_car = randint(0, len(self.vehicles)-1)
new_person.vehicle = self.vehicles[chance_car]['id']
# the person's day plan
self.generate_day_plan(new_person, number_of_day)
# return the person
return new_person
def generate_day_plan(self,person, number_of_day=1):
'''
- If working:
+ Going to work from 7AM to 9AM, stay there for 8 hours
+ After work, 20% go to restaurant for dinner (if go to restaurant, stay there for 1-2 hours), 70% go home, 10% go to shopping places (stay there for 0.5-1 hour then go home)
+ At night, 30% go to some leisure place and stay there for 2-3 hours (from 7AM to 9AM), the person go straight from restaurant to the leisure place. 70% just stay at home, the person also go home if had dinner at restaurant
- If going to school:
+ 70% go to school at random time from 7:00-14:00, stay there for a random duration between 2 hours to 6 hours
+ If stay at home or have more than 4 empty hours during morning then: 40% go to shopping places (stay there for 0.5-1 hour), 40% go to leisure places (stay there for 1-2 hours, if time less than 1 hour then straight to the next destination, else go home), 20% stay at home
+ At night 30% go to leisure place, 70% stay at home
- If staying at home:
+ 40% go to a shopping place from 9-11 or 15-17, stay there for 0.5-1 hour then go home, 60% just stay at home
+ If stay at home during afternoon then 30% go to a leisure place (stay there for 1-2 hours) from 2pm to 5pm
+ 20% go to restaurant from 6pm to 7pm, stay there for 1 hour. If end time of leisure place is 1 hour apart from restaurant time then go straight to restaurant from the leisure place.
+ At night 30% go to a leisure place and stay there for 2-3 hour from 8pm-10pm. If the person have dinner out then go straight to the leisure place from the restaurant.
'''
chance_event = 0
chance_place = 0
chance_time = 0
chance_duration = 0
sequence = 1
total_second_in_one_day = 24*3600
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'DUMMY',
'from_place_id': -1,
'from_edge_id' : -1,
'to_place_id': -1,
'to_edge_id' : -1,
'duration': 5,
'time': 20})
sequence += 1
for current_day in range(number_of_day):
if person.working_status == Person.STATUS_WORKING:
# workplace
workplace = query(self.workplaces).single(lambda workplace: workplace['id'] == person.work_address['id'])
# some variables
go_to_restaurant = False
chance_restaurant_place = 0
#time go to work from 7AM to 9AM => 120 minutes
chance_time = randint(current_day*total_second_in_one_day+7*3600, current_day*total_second_in_one_day+9*3600)
#go to work
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id' : person.home_address['edge_id'],
'to_place_id': workplace['id'],
'to_edge_id' : workplace['edge_id'],
'duration': -1,
'time': chance_time})
sequence += 1
# working
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'WORKING',
'from_place_id': workplace['id'],
'from_edge_id' : workplace['edge_id'],
'to_place_id': workplace['id'],
'to_edge_id' : workplace['edge_id'],
'duration': randint(8*3600, 9*3600),
'time': -1})
sequence+=1
# go for dinner
chance_event = randint(0, 100)
if chance_event <= 20: #go to restaurant
# go to restaurant
go_to_restaurant = True
chance_restaurant_place = randint(0, len(self.restaurant_places)-1)
chance_time = randint(60*60, 120*60)
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': workplace['id'],
'from_edge_id' : workplace['edge_id'],
'to_place_id': self.restaurant_places[chance_restaurant_place]['id'],
'to_edge_id': self.restaurant_places[chance_restaurant_place]['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
#time at restaurant
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'EATING',
'from_place_id': self.restaurant_places[chance_restaurant_place]['id'],
'from_edge_id': self.restaurant_places[chance_restaurant_place]['edge_id'],
'to_place_id': self.restaurant_places[chance_restaurant_place]['id'],
'to_edge_id': self.restaurant_places[chance_restaurant_place]['edge_id'],
'duration': chance_time,
'time': -1})
sequence+=1
elif chance_event <= 30: # go shopping
# go to store
chance_place = randint(0, len(self.stores)-1)
chance_time = randint(30*60, 60*60)
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': workplace['id'],
'from_edge_id' : workplace['edge_id'],
'to_place_id': self.stores[chance_place]['id'],
'to_edge_id': self.stores[chance_place]['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
#time at store
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SHOPPING',
'from_place_id': self.stores[chance_place]['id'],
'from_edge_id': self.stores[chance_place]['edge_id'],
'to_place_id': self.stores[chance_place]['id'],
'to_edge_id': self.stores[chance_place]['edge_id'],
'duration': chance_time,
'time': -1})
sequence+=1
#go home
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.stores[chance_place]['id'],
'from_edge_id': self.stores[chance_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
else: #go home from work
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': workplace['id'],
'from_edge_id': workplace['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
# go to leisure place at night
chance_event = randint(0, 100)
if chance_event <= 30: # go to leisure place
chance_place = randint(0, len(self.leisure_places)-1)
chance_duration = randint(1.5*3600, 3*3600)
# go to leisure place
if go_to_restaurant: # go straight from restaurant
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.restaurant_places[chance_restaurant_place]['id'],
'from_edge_id': self.restaurant_places[chance_restaurant_place]['edge_id'],
'to_place_id': self.leisure_places[chance_place]['id'],
'to_edge_id': self.leisure_places[chance_place]['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
else: # go from home at random time 7PM to 9PM
chance_time = randint(current_day*total_second_in_one_day+19*3600, current_day*total_second_in_one_day+21*3600)
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': self.leisure_places[chance_place]['id'],
'to_edge_id': self.leisure_places[chance_place]['edge_id'],
'duration': -1,
'time': chance_time})
sequence+=1
#stay at leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'LEISURE',
'from_place_id': self.leisure_places[chance_place]['id'],
'from_edge_id': self.leisure_places[chance_place]['edge_id'],
'to_place_id': self.leisure_places[chance_place]['id'],
'to_edge_id': self.leisure_places[chance_place]['edge_id'],
'duration': chance_duration,
'time': -1})
sequence+=1
#go home from leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.leisure_places[chance_place]['id'],
'from_edge_id': self.leisure_places[chance_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': chance_duration,
'time': -1})
sequence+=1
else:
if go_to_restaurant: #go home if have dinner at restaurant
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.restaurant_places[chance_restaurant_place]['id'],
'from_edge_id': self.restaurant_places[chance_restaurant_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
elif person.working_status == Person.STATUS_STUDYING: # go to school
university = query(self.schools).single(lambda school: school['id'] == person.school_address['id'])
go_to_shopping = False
chance_go_to_school = randint(0, 100)
if chance_go_to_school <= 70: # school day
time_go_to_school = randint(current_day*total_second_in_one_day+7*3600, current_day*total_second_in_one_day+14*3600)
duration_at_school = randint(2*3600, 6*3600)
if time_go_to_school >= current_day*total_second_in_one_day+12*3600: #go to school at evening
chance_free_time_morning = randint(0, 100)
if chance_free_time_morning <= 40: #go shopping from 8am to 10am, stay 0.5 to 1 hour
go_to_shopping = True
time_go_shopping = randint(current_day*total_second_in_one_day+8*3600, current_day*total_second_in_one_day+10*3600)
duration_shopping = randint(0.5*3600, 3600)
chance_shopping_place = randint(0, len(self.stores)-1)
#move to shopping place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': self.stores[chance_shopping_place]['id'],
'to_edge_id': self.stores[chance_shopping_place]['edge_id'],
'duration': -1,
'time': time_go_shopping})
sequence+=1
#actually shopping
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SHOPPING',
'from_place_id': self.stores[chance_shopping_place]['id'],
'from_edge_id': self.stores[chance_shopping_place]['edge_id'],
'to_place_id': self.stores[chance_shopping_place]['id'],
'to_edge_id': self.stores[chance_shopping_place]['edge_id'],
'duration': duration_shopping,
'time': -1})
sequence+=1
#to home
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.stores[chance_shopping_place]['id'],
'from_edge_id': self.stores[chance_shopping_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
elif chance_free_time_morning <= 80: # go to leisure place
time_leisure_place = randint(current_day*total_second_in_one_day+8*3600, current_day*total_second_in_one_day+9*3600)
duration_leisure_place = randint(3600, 2*3600)
chance_leisure_place = randint(0, len(self.leisure_places)-1)
#move to leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': self.leisure_places[chance_leisure_place]['id'],
'to_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'duration': -1,
'time': time_leisure_place})
sequence+=1
#actually time at leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SHOPPING',
'from_place_id': self.leisure_places[chance_leisure_place]['id'],
'from_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'to_place_id': self.leisure_places[chance_leisure_place]['id'],
'to_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'duration': duration_leisure_place,
'time': -1})
sequence+=1
#to home
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.leisure_places[chance_leisure_place]['id'],
'from_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
#go to school
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': person.school_address['id'],
'to_edge_id': person.school_address['edge_id'],
'duration': -1,
'time': time_go_to_school})
sequence+=1
#stay at school
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SCHOOLING',
'from_place_id': person.school_address['id'],
'from_edge_id': person.school_address['edge_id'],
'to_place_id': person.school_address['id'],
'to_edge_id': person.school_address['edge_id'],
'duration': duration_at_school,
'time': -1})
sequence+=1
#go home from school
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': person.school_address['id'],
'from_edge_id': person.school_address['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
elif time_go_to_school + duration_at_school <= current_day*total_second_in_one_day+13*3600: #free time during afternoon
#go to school
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': person.school_address['id'],
'to_edge_id': person.school_address['edge_id'],
'duration': -1,
'time': time_go_to_school})
sequence+=1
#stay at school
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SCHOOLING',
'from_place_id': person.school_address['id'],
'from_edge_id': person.school_address['edge_id'],
'to_place_id': person.school_address['id'],
'to_edge_id': person.school_address['edge_id'],
'duration': duration_at_school,
'time': -1})
sequence+=1
#go home from school
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': person.school_address['id'],
'from_edge_id': person.school_address['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
chance_free_time_afternoon = randint(0, 100)
if chance_free_time_afternoon <= 40: #go shopping from 3pm to 5am, stay 0.5 to 1 hour
go_to_shopping = True
time_go_shopping = randint(current_day*total_second_in_one_day+15*3600, current_day*total_second_in_one_day+17*3600)
duration_shopping = randint(0.5*3600, 3600)
chance_shopping_place = randint(0, len(self.stores)-1)
#move to shopping place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': self.stores[chance_shopping_place]['id'],
'to_edge_id': self.stores[chance_shopping_place]['edge_id'],
'duration': -1,
'time': time_go_shopping})
sequence+=1
#actually shopping
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SHOPPING',
'from_place_id': self.stores[chance_shopping_place]['id'],
'from_edge_id': self.stores[chance_shopping_place]['edge_id'],
'to_place_id': self.stores[chance_shopping_place]['id'],
'to_edge_id': self.stores[chance_shopping_place]['edge_id'],
'duration': duration_shopping,
'time': -1})
sequence+=1
#to home
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.stores[chance_shopping_place]['id'],
'from_edge_id': self.stores[chance_shopping_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
elif chance_free_time_afternoon <= 80: # go to leisure place
time_leisure_place = randint(current_day*total_second_in_one_day+14*3600, current_day*total_second_in_one_day+16*3600)
duration_leisure_place = randint(3600, 2*3600)
chance_leisure_place = randint(0, len(self.leisure_places)-1)
#move to leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': self.leisure_places[chance_leisure_place]['id'],
'to_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'duration': -1,
'time': time_leisure_place})
sequence+=1
#actually time at leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SHOPPING',
'from_place_id': self.leisure_places[chance_leisure_place]['id'],
'from_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'to_place_id': self.leisure_places[chance_leisure_place]['id'],
'to_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'duration': duration_leisure_place,
'time': -1})
sequence+=1
#to home
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.leisure_places[chance_leisure_place]['id'],
'from_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
else: #no school during the day
chance_free_time = randint(0, 100)
if chance_free_time <= 40: #go shopping from 9am to 5am, stay 0.5 to 1 hour
go_to_shopping = True
time_go_shopping = randint(current_day*total_second_in_one_day+9*3600, current_day*total_second_in_one_day+17*3600)
duration_shopping = randint(0.5*3600, 3600)
chance_shopping_place = randint(0, len(self.stores)-1)
#move to shopping place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': self.stores[chance_shopping_place]['id'],
'to_edge_id': self.stores[chance_shopping_place]['edge_id'],
'duration': -1,
'time': time_go_shopping})
sequence+=1
#actually shopping
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SHOPPING',
'from_place_id': self.stores[chance_shopping_place]['id'],
'from_edge_id': self.stores[chance_shopping_place]['edge_id'],
'to_place_id': self.stores[chance_shopping_place]['id'],
'to_edge_id': self.stores[chance_shopping_place]['edge_id'],
'duration': duration_shopping,
'time': -1})
sequence+=1
#to home
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.stores[chance_shopping_place]['id'],
'from_edge_id': self.stores[chance_shopping_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
elif chance_free_time <= 80: # go to leisure place
time_leisure_place = randint(current_day*total_second_in_one_day+9*3600, current_day*total_second_in_one_day+17*3600)
duration_leisure_place = randint(3600, 2*3600)
chance_leisure_place = randint(0, len(self.leisure_places)-1)
#move to leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': self.leisure_places[chance_leisure_place]['id'],
'to_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'duration': -1,
'time': time_leisure_place})
sequence+=1
#actually time at leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SHOPPING',
'from_place_id': self.leisure_places[chance_leisure_place]['id'],
'from_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'to_place_id': self.leisure_places[chance_leisure_place]['id'],
'to_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'duration': duration_leisure_place,
'time': -1})
sequence+=1
#go home
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.leisure_places[chance_leisure_place]['id'],
'from_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
# at night
chance_leisure_evening = randint(0, 100)
if chance_leisure_evening<=30:
time_leisure_place = randint(current_day*total_second_in_one_day+19*3600, current_day*total_second_in_one_day+21*3600)
duration_leisure_place = randint(0.5*3600, 3*3600)
chance_leisure_place = randint(0, len(self.leisure_places)-1)
#move to leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': self.leisure_places[chance_leisure_place]['id'],
'to_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'duration': -1,
'time': time_leisure_place})
sequence+=1
#actually time at leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SHOPPING',
'from_place_id': self.leisure_places[chance_leisure_place]['id'],
'from_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'to_place_id': self.leisure_places[chance_leisure_place]['id'],
'to_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'duration': duration_leisure_place,
'time': -1})
sequence+=1
#go home
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.leisure_places[chance_leisure_place]['id'],
'from_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
elif person.working_status == Person.STATUS_STAY_AT_HOME:
chance_go_shopping = randint(0, 100)
chance_go_leisure = randint(0, 100)
time_go_shopping = 0
duration_shopping = 0
if chance_go_shopping >= 50:
time_go_shopping = randint(current_day*total_second_in_one_day+9*3600, current_day*total_second_in_one_day+12*3600)
duration_shopping = randint(0.5*3600, 3600)
chance_shopping_place = randint(0, len(self.stores)-1)
#move to shopping place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': self.stores[chance_shopping_place]['id'],
'to_edge_id': self.stores[chance_shopping_place]['edge_id'],
'duration': -1,
'time': time_go_shopping})
sequence+=1
#actually shopping
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'SHOPPING',
'from_place_id': self.stores[chance_shopping_place]['id'],
'from_edge_id': self.stores[chance_shopping_place]['edge_id'],
'to_place_id': self.stores[chance_shopping_place]['id'],
'to_edge_id': self.stores[chance_shopping_place]['edge_id'],
'duration': duration_shopping,
'time': -1})
sequence+=1
#to home
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.stores[chance_shopping_place]['id'],
'from_edge_id': self.stores[chance_shopping_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})
sequence+=1
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'STAY_AT_HOME',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': randint(0.5*3600, 1.5*3600),
'time': -1})
sequence+=1
if chance_go_leisure >= 50:
if chance_go_shopping<50:
time_leisure_place = randint(current_day*total_second_in_one_day+9*3600, current_day*total_second_in_one_day+12*3600)
else:
time_leisure_place = -1
duration_leisure_place = randint(3600, 1.5*3600)
chance_leisure_place = randint(0, len(self.leisure_places)-1)
if chance_go_shopping >= 50:
time_leisure_place = randint(time_go_shopping+duration_shopping+1800+3600, current_day*total_second_in_one_day+16*3600)
#move to leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': -1,
'from_edge_id': person.home_address['edge_id'],
'to_place_id': self.leisure_places[chance_leisure_place]['id'],
'to_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'duration': -1,
'time': time_leisure_place})
sequence+=1
#actually time at leisure place
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'LEISURE',
'from_place_id': self.leisure_places[chance_leisure_place]['id'],
'from_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'to_place_id': self.leisure_places[chance_leisure_place]['id'],
'to_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'duration': duration_leisure_place,
'time': -1})
sequence+=1
#go home
person.day_plan.append({'action_id': utility.generate_random_string(20),
'sequence' : sequence,
'type': 'MOVING',
'from_place_id': self.leisure_places[chance_leisure_place]['id'],
'from_edge_id': self.leisure_places[chance_leisure_place]['edge_id'],
'to_place_id': -1,
'to_edge_id': person.home_address['edge_id'],
'duration': -1,
'time': -1})