-
Notifications
You must be signed in to change notification settings - Fork 8
/
example_script.m
64 lines (51 loc) · 1.26 KB
/
example_script.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
% psotv1
% script to test and run the PSO for swarm trajectories
clear
clf
addpath('PSO')
addpath('UAV')
addpath('SWARM')
% Save a gif with the results?
savegif = true;
% Particles count
N_Particles = 25;
% Iterations
N_Iterations = 45;
% Agent count (Physical count of robots, each particle consists of
% N_Agent agents)
N_Agents = 5;
% Starting point and Goal points moved to individual functions for clarity
Path.Start = createPathStartingPoints(N_Agents,'calesita', 0.05);
Path.Goal = createPathGoals(N_Agents, 'calesita', 0.3, 0);
axis([0 1 0 1]);
axis square
h_goals = PlotStartingPointandGoals(N_Agents, Path);
%% Initialize the Swarm and PSO
s = SWARM();
for k = 1:N_Agents
s.AddAgent(UAV(Path.Start.state(:, k)));
end
% Initialize PSO
p = PSO(s, N_Particles, Path.Goal, @CostFcn);
%% Iterate PSO
p.Iterate();
drawnow update
%%
if savegif
filename = 'sample-PSO-run2.gif';
end
for k = 1:N_Iterations
p.Iterate();
drawnow update
axis([0 1 0 1])
if savegif
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if k == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
end