-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencircle_test.m
146 lines (125 loc) · 4.94 KB
/
encircle_test.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
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
%encircle_test -
% ------------------------------------------------------------------------
% AUTHOR:
% Lijun SUN
% ------------------------------------------------------------------------
%% running time calculation: start
starttime = tic;
%% calculate the straight line escape direction for the target prey robot
switch prey
case {"linear","linear\_smart"}
escape_dirs = escape_line_choose(p_robots(:,:,1),p_target,...
map_w,map_h);
otherwise
% do nothing
end
%% loop
% % uncomment and only used for the occupying based capture definition.
% p_target_last = p_target;
% pr_robots_last = p_robots(:, :, 1);
for iter_cc = 1:MAX_ITERS_CC
%% dynamic
% jump over the first iteration for the dynamic prey
if mod(iter_cc, ITERS_TARGET_NOT_UPDATE) && iter_cc > 1
switch prey
case "random"
p_target = p_target_random(p_target,...
p_robots(:,:,1),map_w,map_h);
case "linear"
[p_target,escape_dirs] = p_target_linear(p_target,...
p_robots(:,:,1),map_w,map_h,escape_dirs);
case "linear\_smart"
[p_target,escape_dirs] = p_target_linear_smart(...
p_target,p_robots(:,:,1),map_w,map_h,escape_dirs);
otherwise
% do nothing
end
% END switch prey
% %% capture status examination -- hunter occupies the cell of the prey
% % uncomment and only used for the occupying based capture definition.
% captured = ...
% capture_occupy(p_target_last, pr_robots_last, p_target);
end
% END if ~mod(iter_cc, ITERS_UPDATE)
%% preserve variable
% comment this when using the occupying based capture defintion.
pr_robots_last = p_robots(:,:,1);
%% loop all the subpopulations, including the (real & virtual) robots.
[p_robots,fitness_p,pi_robots,fitness_pi,pg_robots,fitness_pg,...
v_robots,iters_still]...
= optimizer_cc_pso_encircle(p_robots,fitness_p,pi_robots,...
fitness_pi,pg_robots,fitness_pg,v_robots,w,c1,c2,map_w,map_h,...
radius,p_target,iters_still,captured);
% %% presrve variables.
% % uncomment and only used for the occupying based capture definition.
% p_target_last = p_target;
% pr_robots_last = p_robots(:, :, 1);
%% animation
if animation_on
encircle_animation;
fitness_curves_plot;
end
% END if animation_on
%% capture status examination
captured = capture_status(p_target,p_robots(:,:,1),map_w,map_h);
%% convergence status examination
[converged, iters_cc_converging] = converge_status(pr_robots_last,...
p_robots(:,:,1),iters_cc_converging);
%% experiment status examination and actions
if captured && ~captured_recorded
iters_cc_captured = iter_cc;
captured_recorded = true;
% running time calculation: end
runtime = toc(starttime);
% save the captured frame if the animation is turned off
if ~animation_on
frame_save(map_w,map_h,p_target,p_robots,frame_name_captured);
end
% END if ~animation_on
end
% END if captured
%
if converged
if captured
% if the target has been captured, while the predator swarm
% robots has also converged, then the game is over.
% preserve the data
iters_cc_converged = iter_cc - iters_cc_converging;
% save the converged frame if the animation is turned off
if ~animation_on
frame_save(map_w,map_h,p_target,p_robots,...
frame_name_converged);
end
% END if ~animation_on
% the game is over, exit
break;
else
% if the target has NOT been captured, but the predator swarm
% has converged which is an exception, then add a random noise
% to the whole predator swarm robots
p_robots(:,:,1) = swarm_random_move(p_robots(:,:,1),...
p_target,map_w,map_h);
%re-evaluate the fitness of all individuals
[pi_robots,pg_robots,fitness_p,fitness_pi,fitness_pg] = ...
fitness_update(p_target,p_robots,pi_robots,pg_robots,...
fitness_p,fitness_pi,fitness_pg,map_w,map_h);
% the converged status is fake, so update the memory
converged = false;
iters_cc_converging = -1;
end
% END if captured
end
% END if converged
end
% END for iter_cc = 1:MAX_ITERS_CC
%% save the data
% save the movie data
if animation_on
movie_save(frames,movie_name);
end
% END if animation_on
%% close
% if animation_on
% close('all');
% end
% END if animation_on