-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBG_PSO.m
More file actions
148 lines (108 loc) · 6.18 KB
/
BG_PSO.m
File metadata and controls
148 lines (108 loc) · 6.18 KB
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
147
148
function [watermrkd_img,recmessage,PSNR,IF,NCC,pbest]=BG_PSO(cover_object,message)
h=msgbox('Wait while PBFO is Computing');
clc
p=4; % dimension of search space
s=6; % The number of bacteria
% s=5000;
Nc=2; % Number of chemotactic steps
Ns=2; % Limits the length of a swim
Nre=1; % The number of reproduction steps
Ned=1; % The number of elimination-dispersal events
Sr=s/2; % The number of bacteria reproductions (splits) per generation
Ped=0.25; % The probabilty that each bacteria will be eliminated/dispersed
c(:,1)=0.05*ones(s,1); % the run length
for m=1:s % the initital posistions
P(1,:,1,1,1)= 10*rand(s,1)';
P(2,:,1,1,1)= 10*rand(s,1)';
P(3,:,1,1,1)= 10*rand(s,1)';
P(4,:,1,1,1)= 10*rand(s,1)';
end
for i = 1:s
Delta(:,i)=(2*round(randn(p,1))-1).*randn(p,1);
end
c1 =5; % PSO parameter C1 .5
c2 = 5; % PSO parameter C2 .5
R1 = randn(p,s); % PSO parameter
R2 = randn(p,s); % PSO parameter
Plocal_best_position=randn(p,s,Nc); % PSO
Pglobal_best_position=randn(p,s,Nc); % PSO
velocity = randn(p,s); % PSO
current_position=randn(p,s,Nc); % PSO
%Main loop
%Elimination and dispersal loop
for ell=1:Ned
%Reprodution loop
for K=1:Nre
% swim/tumble(chemotaxis)loop
for j=1:Nc
for i=1:s
[J(i,j,K,ell),watermrkd_img,recmessage,PSNR,IF,NCC]=Live_fn(P(:,i,j,K,ell),cover_object,message);
% Tumble
Jlast=J(i,j,K,ell);
Jlocal(i,j)=Jlast;
P(:,i,j+1,K,ell)=P(:,i,j,K,ell)+c(i,K)*Delta(:,i)/sqrt(Delta(:,i)'*Delta(:,i)); % This adds a unit vector in the random direction
% Swim (for bacteria that seem to be headed in the right direction)
[J(i,j+1,K,ell),watermrkd_img,recmessage,PSNR,IF,NCC]=Live_fn(P(:,i,j+1,K,ell),cover_object,message);
m=0; % Initialize counter for swim length
while m<Ns
m=m+1;
if J(i,j+1,K,ell)>Jlast
Jlast=J(i,j+1,K,ell);
P(:,i,j+1,K,ell)=P(:,i,j+1,K,ell)+c(i,K)*Delta(:,i)/sqrt(Delta(:,i)'*Delta(:,i));
[J(i,j+1,K,ell),watermrkd_img,recmessage,PSNR,IF,NCC]=Live_fn(P(:,i,j+1,K,ell),cover_object,message);
current_position(:,i,j+1)= P(:,i,j+1,K,ell) ; % PSO
Jlocal(i,j+1) = J(i,j+1,K,ell) ; % PSO
else
Jlocal(i,j+1) = J(i,j+1,K,ell) ;
current_position(:,i,j+1)= P(:,i,j+1,K,ell);
m=Ns ;
end
end
sprintf('The value of interation i %3.0f ,j = %3.0f , K= %3.0f, ell= %3.0f' , i, j, K ,ell );
end % Go to next bacterium
%For each chemotactic evaluate the local and global best position
%Local best position over all chemotactic that each bacteria move through it
[Jmin_for_each_chemotactic,index]=min(Jlocal,[],2);
for m=1:s
Plocal_best_position(:,m,j) = current_position(:,m,index(m,:));
end
%Global best position over all chemotactic and for each bacteria
[Y,I]=min(Jmin_for_each_chemotactic);
global_best_position =current_position(:,I,index(I,:));
for m =1:s
Pglobal_best_position(:,m,j)=global_best_position;
end
%Caluculate the new direction for each bacteria
velocity =5* velocity + c1*(R1.*(Plocal_best_position(:,:,j)-current_position(:,:,j+1))) + c2*(R2.*(Pglobal_best_position(:,:,j)-current_position(:,:,j+1)));
Delta = velocity ;
end % Go to the next chemotactic
%Reprodution
Jhealth=sum(J(:,:,K,ell),2); % Set the health of each of the S bacteria
[Jhealth,sortind]=sort(Jhealth); % Sorts the nutrient concentration in order of ascending
P(:,:,1,K+1,ell)=P(:,sortind,Nc+1,K,ell);
c(:,K+1)=c(sortind,K); % And keeps the chemotaxis parameters with each bacterium at the next generation
%Split the bacteria (reproduction)
for i=1:Sr
P(:,i+Sr,1,K+1,ell)=P(:,i,1,K+1,ell); % The least fit do not reproduce, the most fit ones split into two identical copies
c(i+Sr,K+1)=c(i,K+1);
end
end % Go to next reproduction
%Eliminatoin and dispersal
for m=1:s
if Ped>rand % % Generate random number
P(1,:,1,1,1)= 10*rand(s,1)';
P(2,:,1,1,1)= 10*rand(s,1)';
P(3,:,1,1,1)= 10*rand(s,1)';
P(4,:,1,1,1)= 10*rand(s,1)';
else
P(:,m,1,1,ell+1)=P(:,m,1,Nre+1,ell); % Bacteria that are not dispersed
end
end
end % Go to next elimination and disperstal
%Report
reproduction = J(:,1:Nc,Nre,Ned);
[jlastreproduction,O] = min(reproduction,[],2); % min cost function for each bacterial
[Y,I] = min(jlastreproduction);
pbest=P(:,I,O(I,:),K,ell);
close(h)
end