forked from choonkiatlee/hack_cambridge_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
108 lines (87 loc) · 3.03 KB
/
run.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
from flask import Flask, render_template, request, url_for, jsonify
from flask_cors import CORS
import random, json
import pandas as pd
import numpy as np
import glob
import routes
M=4000
A=181.4
B=2.42
C=0.62
v = []
a = []
app = Flask(__name__)
CORS(app)
@app.route('/get_route', methods=['GET'])
def get_route():
if 'query' not in request.args:
return '400'
res = routes.get_route_from_api(request.args)
return res
@app.route('/', methods=['GET'])
def form():
json_helper = {}
json_helper['aggression'] = get_aggression(vel_arr[0:4])
order = np.argsort(json_helper['aggression'])
json_helper["aggression"] = list(np.array(json_helper["aggression"])[order])
json_helper['trace'] = get_trace(fuel_cons[0:4], order)
json_helper['max_fuel'] = max([max(i) for i in fuel_cons[0:4]])
print(json_helper['max_fuel'])
print(json_helper['trace'][0])
print(json_helper['aggression'])
json_object = json.dumps(json_helper)
return render_template('index.html' , s_data=json_object)
@app.route('/acceleration', methods=['GET'])
def acceleration():
return render_template('index2.html')
@app.route('/felix', methods=['GET'])
def felix():
return render_template('index2_felix.html')
@app.route("/get_aggressive_update")
def get_aggressive_update():
current_v = float(request.args["current_v"])
current_a = float(request.args["current_a"])
v.append(current_v)
a.append(current_a)
return jsonify( {"aggressive":aggressive_w_accel(v,a)} )
def get_aggression(velocities):
res = [aggressive(i) for i in velocities]
return res
def aggressive_w_accel(v, a):
a,v = np.array(a), np.array(v)
av=a*v
agg=abs((1/M)*((A*v.sum()+B*(v**2).sum()+C*(v**3).sum()+M*av.sum())/v.sum())*(8.9/np.mean(v)))
return np.round(agg * 20, decimals=2)
def aggressive(v):
v = np.array(v)
a=np.gradient(v)
av=a*v
agg=abs((1/M)*((A*v.sum()+B*(v**2).sum()+C*(v**3).sum()+M*av.sum())/v.sum())*(8.9/np.mean(v)))
return np.round(agg * 20, decimals=2)
def get_trace(array, order):
res = []
print([len(a) for a in array])
for idx in range(100):
part = {"x": idx}
for j in order:
part["trace_{}".format(j)] = array[j][idx]
res.append(part)
return res
if __name__ == '__main__':
filenames = glob.glob("VED_DynamicData_Part1/*")
vel_arr = []
fuel_cons = []
for filename in filenames:
df = pd.read_csv(filename)
df["Fuel Rate[L/hr]"] = pd.to_numeric(df["Fuel Rate[L/hr]"])
df = df[pd.notna(df["Fuel Rate[L/hr]"])]
df = df[df["Fuel Rate[L/hr]"] > 0]
df["Vehicle Speed[km/h]"] = pd.to_numeric(df["Vehicle Speed[km/h]"])
for vehId in df["VehId"].unique():
veh1 = df[df["VehId"] == vehId].reset_index()
vel = list(np.array(veh1["Vehicle Speed[km/h]"])*1000/3600)
fuel = list(np.array(veh1["Vehicle Speed[km/h]"]))
vel_arr.append(list(vel))
fuel_cons.append(list(np.array(fuel)/np.mean(vel)))
app.run('0.0.0.0',debug=True)