-
Notifications
You must be signed in to change notification settings - Fork 2
/
tpa_demo.py
269 lines (200 loc) · 10.2 KB
/
tpa_demo.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
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
import streamlit as st
import pandas as pd
import folium
from streamlit_folium import folium_static
import json
import requests
import time
import urllib.parse
from base64 import b64encode
import hmac
import hashlib
import binascii
import datetime
from dotenv import load_dotenv
import os
load_dotenv()
st.header("HERE Tour Planning Demo")
grant_type = 'client_credentials'
oauth_consumer_key = os.getenv('HERE_ACCESS_KEY_ID')
access_key_secret = os.getenv('HERE_ACCESS_KEY_SECRET')
oauth_nonce = str(int(time.time()*1000))
oauth_timestamp = str(int(time.time()))
oauth_signature_method = 'HMAC-SHA256'
oauth_version = '1.0'
url = 'https://account.api.here.com/oauth2/token'
st.sidebar.header("Setup")
# HMAC-SHA256 hashing algorithm to generate the OAuth signature
def create_signature(secret_key, signature_base_string):
encoded_string = signature_base_string.encode()
encoded_key = secret_key.encode()
temp = hmac.new(encoded_key, encoded_string, hashlib.sha256).hexdigest()
byte_array = b64encode(binascii.unhexlify(temp))
return byte_array.decode()
# concatenate the six oauth parameters, plus the request parameters from above, sorted alphabetically by the key and separated by "&"
def create_parameter_string(grant_type, oauth_consumer_key,oauth_nonce,oauth_signature_method,oauth_timestamp,oauth_version):
parameter_string = ''
parameter_string = parameter_string + 'grant_type=' + grant_type
parameter_string = parameter_string + '&oauth_consumer_key=' + oauth_consumer_key
parameter_string = parameter_string + '&oauth_nonce=' + oauth_nonce
parameter_string = parameter_string + '&oauth_signature_method=' + oauth_signature_method
parameter_string = parameter_string + '&oauth_timestamp=' + oauth_timestamp
parameter_string = parameter_string + '&oauth_version=' + oauth_version
return parameter_string
parameter_string = create_parameter_string(grant_type, oauth_consumer_key,oauth_nonce,oauth_signature_method,oauth_timestamp,oauth_version)
encoded_parameter_string = urllib.parse.quote(parameter_string, safe='')
encoded_base_string = 'POST' + '&' + urllib.parse.quote(url, safe='')
encoded_base_string = encoded_base_string + '&' + encoded_parameter_string
# create the signing key
signing_key = access_key_secret + '&'
oauth_signature = create_signature(signing_key, encoded_base_string)
encoded_oauth_signature = urllib.parse.quote(oauth_signature, safe='')
#---------------------Requesting Token---------------------
body = {'grant_type' : '{}'.format(grant_type)}
headers = {
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : 'OAuth oauth_consumer_key="{0}",oauth_nonce="{1}",oauth_signature="{2}",oauth_signature_method="HMAC-SHA256",oauth_timestamp="{3}",oauth_version="1.0"'.format(oauth_consumer_key,oauth_nonce,encoded_oauth_signature,oauth_timestamp)
}
response = requests.post(url, data=body, headers=headers)
bearer_token = json.loads(response.text)['access_token']
#---------------------Fleet config ---------------------#
d = st.sidebar.date_input("Plan", datetime.date.today())
st.write('Tour planning for:', str(d)+"T"+"07:00"+":00.000Z")
form = st.sidebar.form("tpa_form")
fixed_cost = form.slider('Fixed cost', 0, 10, 5)
distance_cost = form.slider('Distance cost (per km)', 0, 10, 5)
time_cost = form.slider('Time cost (per Hour)', 0, 20, 10)
vehicle_capacity = form.text_input('Vehicle capacity (Boxes)', 20)
vehicle_amount = form.text_input('Nr. of Vehicles', 10)
vehicle_profile = form.selectbox('Vehicle Profile',('car', 'truck'))
shift = form.slider("Vehicle shift:", 0, 23, (6, 23))
show_json = form.checkbox('Show Request/Response')
form.form_submit_button("Submit")
# Tour Planning Request
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
df_plan = pd.read_excel(uploaded_file, sheet_name='plan')
df_fleet = pd.read_excel(uploaded_file, sheet_name='fleet')
valid_file = True
else:
st.write("Please upload valid excel file.")
valid_file = False
if valid_file:
st.subheader("Plan")
st.dataframe(df_plan)
st.subheader("Fleet")
st.dataframe(df_fleet)
trp_fleet = {'types': [], 'profiles': []}
trp_plan = {'jobs': [], 'relations': []}
#job start and end
shift_start_timestamp = str(d)+"T"+str(shift[0]).zfill(2)+":00:00Z"
shifts_end_timestamp = str(d)+"T"+str(shift[1]).zfill(2)+":00:00Z"
car_shift_start = {'time': shift_start_timestamp,
'location': {'lat': float(df_fleet['shift_start_lat'][0]),
'lng': float(df_fleet['shift_start_lng'][0])
}}
car_shift_end = {'time': shifts_end_timestamp,
'location': {'lat': float(df_fleet['shift_end_lat'][0]),
'lng': float(df_fleet['shift_end_lng'][0])
}}
if vehicle_profile == 'car':
vehicle_description = {'id': 'vehicle_1',
'profile': 'sprinter',
'costs': {'fixed': (float(fixed_cost)),
'distance': distance_cost/float(1000),
'time': time_cost/float(3600)
},
'shifts': [{'start': car_shift_start,
'end': car_shift_end}],
'capacity': [int(vehicle_capacity)],
'amount': int(vehicle_amount)}
vehicle_profile_description = {'type': 'car', 'name': 'sprinter'}
elif vehicle_profile == 'truck':
vehicle_description = {'id': 'truck1',
'profile': 'scania',
'costs': {'fixed': (float(fixed_cost)),
'distance': (float(distance_cost)),
'time': (float(time_cost))
},
'shifts': [{'start': car_shift_start,
'end': car_shift_end}],
'capacity': [vehicle_capacity],
'amount': vehicle_amount}
vehicle_profile_description = {'type': 'truck', 'name': 'scania'}
trp_fleet['types'] = [vehicle_description]
trp_fleet['profiles'] = [vehicle_profile_description]
# Plan
my_jobs = []
i = 0
for _, row in df_plan.iterrows():
place = {}
#job start and end
job_start_timestamp = str(d)+"T"+str(row['job_start'])+".000Z"
job_end_timestamp = str(d)+"T"+str(row['job_end'])+".000Z"
if row['job_type'] == 'delivery':
place = {'deliveries' : []}
place['deliveries'].append({'times':[[job_start_timestamp, job_end_timestamp]],
'location': {'lat': row['job_lat'], 'lng': row['job_lng']},
'duration': row['job_duration'],
'demand': [row['job_demand']]})
if row['job_type'] == 'pickup':
place = {'pickups' : []}
place['pickups'].append({'times':[[job_start_timestamp, job_end_timestamp]],
'location': {'lat': row['job_lat'], 'lng': row['job_lng']},
'duration': row['job_duration'],
'demand': [row['job_demand']]})
my_jobs.append({'id': row['job_id'], 'places': place})
trp_plans = {'jobs': my_jobs}
trp_request = {'id': 'request1',
'fleet': trp_fleet,
'plan': trp_plans}
trp_request_json = json.dumps(trp_request)
if show_json:
st.subheader("Request")
st.json(trp_request_json)
# TRP Requesting
authorization_token = "Bearer " + bearer_token
headers = {
'Content-Type' : 'application/json',
"Authorization": authorization_token
}
# visualization
centroid_lat = df_plan['job_lat'].mean()
centroid_lng = df_plan['job_lng'].mean()
import openrouteservice as ors
client = ors.Client(key=os.getenv('ORS_CLIENT_KEY'))
tpa_response = requests.post("https://tourplanning.hereapi.com/v2/problems", data=trp_request_json, headers=headers)
if show_json:
st.subheader("Response")
st.json(tpa_response.text)
st.subheader("Solution")
res = json.loads(tpa_response.text)
tour_stats = {'Total Cost': [res['statistic']['cost']],
'Total Distance': [res['statistic']['distance']/float(1000)],
'Total Driving Time': [res['statistic']['times']['driving']/float(3600)],
'Total Serving Time': [res['statistic']['times']['serving']/float(3600)],
'Total Waiting Time': [res['statistic']['times']['waiting']/float(3600)],
'Total Break Time': [res['statistic']['times']['break']/float(3600)],
}
df_stats = pd.DataFrame(tour_stats)
st.dataframe(df_stats)
tiles_url = "https://2.base.maps.ls.hereapi.com/maptile/2.1/maptile/newest/normal.day/{z}/{x}/{y}/512/png8?apiKey=" + os.getenv('HERE_MAP_API_KEY')
m = folium.Map([centroid_lat, centroid_lng], zoom_start=11, tiles=tiles_url, attr="<a href=here.com/>With Love From HERE Technologies</a>")
for i in range(len(res['tours'])):
coordinates = []
for stop in res['tours'][i]['stops']:
coordinates.append([stop['location']['lng'], stop['location']['lat']])
folium.Marker([stop['location']['lat'], stop['location']['lng']],
radius=5,
fill_color="#00008b",
fill=True
).add_to(m)
route = client.directions(
coordinates=coordinates,
format='geojson',
validate=False,
)
folium.PolyLine(color="#00008b", locations=[list(reversed(coord))
for coord in
route['features'][0]['geometry']['coordinates']]).add_to(m)
folium_static(m)