forked from anandarangan249/swarm-foraging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brownian_motion_multiple_agents.m
53 lines (50 loc) · 1.56 KB
/
brownian_motion_multiple_agents.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
%% Initialization
% Create an environment of size 100x100
size = 500;
data = 150.*ones(size,size);
% Initializing
% Max Iterations
max_iter = 1000;
% Number of agents
N = 10;
% Initializing N agents at the centre of the map
x = round(size/2).*ones(N,1);
y = round(size/2).*ones(N,1);
%% Brownian Motion Algorithm
for i = 1:max_iter
for k = 1:N
% Random Robot Spawn position;
%x(k) = randi(size);
%y(k) = randi(size);
data(x(k),y(k)) = 255; % Marking the places robot has visited with red
s = 0;
while s==0
s = round(normrnd(0,1));
end
dx = 0; dy = 0;
while dx == 0 && dy == 0
dx = randi(3)-2;
dy = randi(3)-2;
end
for j = 1:abs(s) % moving s steps in the direction determined by dx and dy
x(k) = max(min(x(k)+dx,size),1);
y(k) = max(min(y(k)+dy,size),1);
data(x(k),y(k)) = 255; % Marking the places robot has visited with red
end
end
%% Display
image(data);
% Initialize a color map array of 256 colors.
colorMap = turbo(256);
% Apply the colormap and show the colorbar
colormap(colorMap);
colorbar;
pause(0.001);
display(i);
end
image(data);
% Initialize a color map array of 256 colors.
colorMap = turbo(256);
% Apply the colormap and show the colorbar
colormap(colorMap);
colorbar;