-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest_pg.py
129 lines (111 loc) · 5.26 KB
/
test_pg.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
from environment import *
from pg import *
import pickle as pl
np.seterr(all='raise')
params = {'lane_width': 4,
'num_scenarios': 1000,
'pos_var': 0.3,
'num_episodes': 6,
'num_trainings_after_simulation': 10,
'num_iterations': 1000,
'num_nds': 10,
'num_lanes': 2,
'actions': [0, 1],
'radar.fov': 2 * np.pi,
'radar.r_max': 7.5,
'sig_gps': 3.4,
'noise_l_max': 0.2,
'noise_alpha_max': 0.02,
'sigma_l': 0.1,
'sigma_alpha': 0.1 * np.pi / 180,
'fim_gps': None,
'fim_gps_master': None,
'objective_peb': 0.12,
'cost_mea': 0.1,
'terminal_reward': 1,
'discounting': 0.7,
'state_def': ['delta_x', 'delta_y', 'var1x', 'var1y', 'var2x', 'var2y', 'varxx', 'varyy', 'n_ngbrs'],
'state_p_def': ['delta_x_p', 'delta_y_p', 'var1x_p', 'var1y_p', 'var2x_p', 'var2y_p', 'varxx_p', 'varyy_p',
'n_ngbrs_p'],
'saving_path': 'tf_models/current',
'xlim': 10,
'round_robin': False,
'sparse_reward': True,
'm': np.array([1.5, 2.7, 0.3, 0.3, 1.7, 1.7, 0, 0, 3]),
's': np.array([40, 40, 1.1, 1.1, 2.6, 2.6, 0.1, 0.1, 2.5])}
params['xlim'] = (params['num_nds'] / params['num_lanes'] - 1) * 5
if params['num_lanes'] == 3:
params['noise_l_max'] = 0.25
params['noise_alpha_max'] = 0.025
elif params['num_lanes'] == 1:
params['noise_l_max'] = 0.2
params['noise_alpha_max'] = 0.02
# attributes that can be determined instantly
headers1 = ['epsd', 'iter', 'scnr', 'nd_idx1', 'nd_idx2', 'exe_crt_agt', 'delta_x', 'delta_y', 'var1x', 'var1y',
'var2x', 'var2y', 'varxx', 'varyy', 'n_ngbrs', 'action']
# attributes that must be determined after the simulation
headers2 = ['reward', 'reward_p', 'delta_x_p', 'delta_y_p', 'var1x_p', 'var1y_p', 'var2x_p', 'var2y_p', 'varxx_p',
'varyy_p', 'n_ngbrs_p', 'q']
# All attributes
headers = headers1 + headers2
sig_gps = params['sig_gps']
gps_fim = np.diag([1 / sig_gps ** 2, 1 / sig_gps ** 2]) * 2
gps_fim_master = np.diag([1 / sig_gps ** 2, 1 / sig_gps ** 2]) * 1e9
params['fim_gps'] = gps_fim
params['fim_gps_master'] = gps_fim_master
data = pd.DataFrame(columns=headers)
n_objective_reached = np.ones(params['num_iterations'])
scenarios = list()
for scenario_idx in range(params['num_scenarios']):
scenarios.append(Scenario(params['num_nds'], params['num_lanes'], params))
scenarios[scenario_idx].pass_msg_ngbrs(params)
epsd_idx = 0
with tf.Session() as sess:
tf.saved_model.loader.load(sess, ['serve'], params['saving_path'])
graph = tf.get_default_graph()
print(graph.get_operations())
data_this_epsd = pd.DataFrame(columns=headers1)
all_var = list()
exe_agts = np.zeros((params['num_scenarios'], 200), dtype=int)
for scenario in scenarios:
scenario.reset()
for itr_idx in range(params['num_iterations']):
for scenario_index, scenario in enumerate(scenarios):
scenario.archive_actions()
raw_data = list()
for scnr_idx, scenario in enumerate(scenarios):
if params['round_robin']:
agt_idx = itr_idx % len(scenario.links)
else:
agt_idx = np.random.randint(0, len(scenario.links))
agt = scenario.links[agt_idx]
exe_agts[scnr_idx, agt_idx] += 1
delta_x, delta_y, var1x, var1y, var2x, var2y, varxx, varyy, n_nbgrs =\
scenario.gen_state(agt[0], agt[1], params)
action = 0 # action is set to 0 here because we need the state description to predict.
entry = [epsd_idx, itr_idx, scnr_idx, agt[0], agt[1], exe_agts[scnr_idx, agt_idx],
delta_x, delta_y, var1x, var1y, var2x, var2y, varxx, varyy, n_nbgrs, action]
raw_data.append(entry)
data_this_epsd_iter = pd.DataFrame(raw_data, columns=headers1)
input_state = (np.array(data_this_epsd_iter[params['state_def']]) - params['m']) / params['s']
prob = sess.run('p:0', feed_dict={'state:0': input_state})
# actions = np.random.choice(range(prob.shape[1]), p=prob[0, :])
actions = np.argmax(prob, axis=1)
for idx, scenario in enumerate(scenarios):
if scenario.objective_achieved(params):
actions[idx] = 0
data_this_epsd_iter['action'] = actions
for row_idx in range(data_this_epsd_iter.shape[0]):
scnr_idx = data_this_epsd_iter.loc[row_idx, 'scnr']
nd_idx1 = data_this_epsd_iter.loc[row_idx, 'nd_idx1']
nd_idx2 = data_this_epsd_iter.loc[row_idx, 'nd_idx2']
scenarios[scnr_idx].update_var(actions[row_idx], nd_idx1, nd_idx2, params)
all_var.append(np.diag(scenarios[scnr_idx].var))
data_this_epsd = pd.concat([data_this_epsd, data_this_epsd_iter], axis=0, ignore_index=True)
if itr_idx % 100 == 0:
print(itr_idx)
if all([scenario.objective_achieved(params) for scenario in scenarios]):
break
n_reached = np.mean(list(map(lambda s: sum((s.pebs < params['objective_peb'] * 1)), scenarios)))
n_objective_reached[itr_idx] = n_reached
pl.dump(n_objective_reached, open('results/performance_pg.p', 'wb'))