-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathevaluate_policy.m
75 lines (57 loc) · 2.38 KB
/
evaluate_policy.m
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
% NYX 12/07/17
function [Colli_rate, Performance, Lane_change_times] = ...
evaluate_policy(Policy_1, plot_flag, if_saving_gif, ...
simu_times, simulate_horizon, min_car_num, max_car_num)
setGlobal;
Colli_rate = zeros(max_car_num - min_car_num + 1, 1);
Performance = zeros(max_car_num - min_car_num + 1, 1);
Lane_change_times = zeros(max_car_num - min_car_num + 1, 1);
for num_cars = min_car_num: max_car_num
fprintf('\nCar Number: %d\n', num_cars);
performance = 0;
tot_colli = 0;
bad_init = 0;
lane_change_times = 0;
for episode = 1 : simu_times
if mod(episode,100) == 0
fprintf('\nSimulating episode: %d\n', episode);
end
[output, ~, if_bad_init] = simulate(num_cars, simulate_horizon, Policy_1, plot_flag, Params, if_saving_gif);
if ~if_bad_init
tot_colli = tot_colli + output(2);
end
if ~if_bad_init && ~output(2)
performance = performance + output(1) / simulate_horizon;
lane_change_times = lane_change_times + output(3);
end
% save gif
% if output(3) >= 1 && plot_flag && ~output(2) && if_saving_gif
% filename = ['simulation_animation_' num2str(tic) '_num_cars_' num2str(num_cars)...
% '_ln_change_' num2str(output(3)) '.gif'];
% for i = 1:length(frames)
% im = frame2im(frames(i));
% [imind,cm] = rgb2ind(im,256);
% % Write to the GIF File
% cd Animation
% if i == 1
% imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
% else
% imwrite(imind,cm,filename,'gif','WriteMode','append');
% end
% cd ..
% end
% end
% count bad initialization
if if_bad_init
bad_init = bad_init + 1;
end
if plot_flag == true
close all;
end
end %simulation for simu_times
Colli_rate(num_cars - min_car_num + 1) = tot_colli / (simu_times - bad_init);
Performance(num_cars - min_car_num + 1) = performance/...
(simu_times - bad_init - tot_colli);
Lane_change_times(num_cars - min_car_num + 1) = lane_change_times/...
(simu_times - bad_init - tot_colli);
end