-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimulation.py
152 lines (108 loc) · 4.28 KB
/
simulation.py
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
import sys
import requests
import json
from config.config import Variables
import numpy as np
import time as t
#TODO: normalize with resepct to prefix sums
admin_id = sys.argv[1]
print(admin_id)
drivers_url = f"http://localhost:5050/get/admin/drivers?admin_id={admin_id}"
paths_url = "http://localhost:5050/get/driver/path?"
r = requests.request("GET", drivers_url)
drivers = r.json()
print(drivers)
print (r)
osrm = "http://localhost:5000"
coords=""
time_frame=100.0
driver_prefix=[0] #prefix sum of driver path lengths to locate where a driver's points start on duration matrix
times_to_reach=[] #time at which the driver reaches point i
driver_paths=[] # coordinates of driver's path
driver_locs = [0]*len(drivers) # index of last location visited
driver_pos= [[0,0]]*len(drivers) # current position of the driver at time t
driver_completed = [False]*len(drivers)
for driver in drivers:
driver_path=[]
path = requests.get(paths_url + f"driver_id={driver['driver_id']}")
# print("PATH: ",path)
path=path.json()
# print(path)
if len(driver_prefix) == 0:
driver_prefix.append(len(path))
else:
driver_prefix.append(driver_prefix[-1] + len(path))
for point in path:
driver_path.append([point["latitude"], point["longitude"]])
coords += str(point["longitude"]) + "," + str(point["latitude"]) + ";"
driver_paths.append(driver_path)
coords = coords[:-1]
distance_duration_url = f"{osrm}/table/v1/driving/" + coords
r = requests.get(distance_duration_url, params={"annotations": "distance,duration"})
r = r.json()
distance_matrix = r["distances"]
duration_matrix = r["durations"]
# print(driver_prefix)
max_duration=0
for i in range(len(driver_prefix[:-1])):
driver_path_durations=[0]
for j in range(driver_prefix[i], driver_prefix[i+1]-1):
driver_path_durations.append(duration_matrix[j][j+1])
times_to_reach.append(driver_path_durations)
for d in times_to_reach:
for i in range(1,len(d)):
d[i]+=d[i-1]
for i in times_to_reach:
for j in i:
max_duration=max(max_duration,j)
# print(max_duration)
for i in range(len(times_to_reach)):
for j in range(len(times_to_reach[i])):
times_to_reach[i][j] = times_to_reach[i][j]*time_frame/max_duration
# print(duration_matrix)
# print(r)
# print(driver_locs)
# print(driver_paths)
# print(times_to_reach)
# print(times_to_reach)
# print("LEN D ", len(times_to_reach[0]))
# print("LEN P ", len(driver_paths[0]))
# print("LEN L ", len(driver_locs[0]))
test = [0]*len(driver_paths[0])
# print(driver_paths[0][-1])
# print(driver_paths[0])
# print(times_to_reach[0])
for i in range(len(times_to_reach[0])):
print(times_to_reach[0][i], " - ", driver_paths[0][i])
for time in range(0,int(time_frame)):
# t.sleep(0.55)
print("TIME: ", time)
for i in range(0,1):
if driver_completed[i]:
driver_pos[i]=driver_paths[i][-1]
print("DELIVERED")
print("CURR POINT ", driver_pos[i])
else:
if time>=times_to_reach[i][-1]:
driver_completed[i]=True
driver_pos[i]=driver_paths[i][-1]
continue
while driver_locs[i]-1< len(times_to_reach) and time>=times_to_reach[i][driver_locs[i]]:
driver_locs[i]+=1
# driver_locs[i]+=1
# if driver_locs[i]>=len(driver_paths[i])-1:
# driver_completed[i]=True
# break
driver_locs[i]-=1 # gives last point visited
print("DRIVER LOC ", driver_locs[i])
if driver_completed[i]:
driver_pos[i]=driver_paths[i][-1]
else:
curr_point = driver_paths[i][driver_locs[i]]
next_point = driver_paths[i][driver_locs[i]+1]
duration_between_points = times_to_reach[i][driver_locs[i]+1]-times_to_reach[i][driver_locs[i]]
time_elapsed = time - times_to_reach[i][driver_locs[i]]
x_curr = curr_point[0]+ (next_point[0]-curr_point[0])*time_elapsed*1.0/duration_between_points
y_curr = curr_point[1]+ (next_point[1]-curr_point[1])*time_elapsed*1.0/duration_between_points
driver_pos[i]=[x_curr,y_curr]
print("CURR POINT ", driver_pos[i])