forked from anandarangan249/swarm-foraging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foraging_brownian_motion.m
86 lines (81 loc) · 2.05 KB
/
foraging_brownian_motion.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
% Create an environment of size 100x100
size = 500;
data = 100.*ones(size,size);
%Create a 50x50 food source at x_food,y_food
x_food = 220; y_food = 220;
data(x_food-20:x_food+20,y_food-20:y_food+20) = 200;
% Initializing
% Max Iterations
max_iter = 1000;
% Robot Spawn position;
%x = randi(size);
%y = randi(size);
start = round(size/2);
x = start; y = start;
data(x,y) = 255; % Marking the places robot has visited with red
% Food found flag
found = 0;
for i = 1:max_iter
if found == 0
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)
x = max(min(x+dx,size),1);
y = max(min(y+dy,size),1);
if data(x,y) == 200
found = 1;
break
end
data(x,y) = 255;
end
end
if found == 1
while x ~= start || y ~= start
if x<start
x = x+1;
end
if x>start
x = x-1;
end
if y<start
y = y+1;
end
if y>start
y = y-1;
end
data(x,y) = 255;
% Display it.
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);
end
break
end
% Display it.
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
% Display it.
image(data);
% Initialize a color map array of 256 colors.
colorMap = turbo(256);
% Apply the colormap and show the colorbar
colormap(colorMap);
colorbar;