Thanks Mr Jan, your counsels help me to reduce some errors. But my real problem is increlentation of this command line which no mismatch of three columns of 'agent' with 'best_agent'
buffalo(i,:,:)=Lb+(Ub-Lb).*rand(size(Lb));
buffalo(:,:,:)=Lb+(Ub-Lb).*rand(size(Lb));
You can try this code and have a good simulation but with only one iteration
function [best_agent,fmin]=Ago_algorithmv6(n)
clear
clc
if nargin<1,
n=10;
end
m_k=0;
lp1=0.7;
lp2=0.5;
bg=0;
w_k=0;
N_IterTotal=2000;
nd=59;
Lb = repmat([36.5, 50, 0.73], nd, 1);
Ub = repmat([41.5, 396, 0.94], nd, 1);
sum =0;
for t=1:3;
disp('agent(i,:) bp_k(i)');
for i=1:n,
for j=1:n
agent(:,:,:)=Lb(:,:)+(Ub(:,:)-Lb(:,:)).*rand(size(Lb));
w_array(:,:,:)= agent(:,:,:);
bp_k(:,:,:)= agent(:,:,:);
disp(strcat(num2str(agent(:,:,:))));
end
end
fitness=10^10*ones(n,1);
[fmin,best_agent,agent,fitness]=get_best_agent(agent,agent,fitness);
N_iter=0;
for iter=1:N_IterTotal,
for jj=1:n
s=agent(jj,:,:);
w=w_array(jj,:,:);
s=s + lp1*(best_agent-w)*rand + lp2*(bp_k(jj,:,:)*rand - w);
disp(strcat(num2str(jj),' - agent after :[',num2str(s),']=',num2str(fobj(s))));
w =((w+ s ))/rand;
s=simplebounds(s,Lb,Ub);
w_array(jj,:,:)=w;
if fobj(s)< fobj(agent(jj,:,:))
agent(jj,:,:)=s;
end
if fobj(s)<fobj(bp_k(jj,:,:)),
bp_k(jj,:,:)=s;
end
if fobj(s)<fobj(best_agent),
best_agent=s;
end
end
best_agent
fmin= fobj(best_agent)
end
end
function [fmin,best,agent,fitness]=get_best_agent(agent,newagent,fitness)
for j=1:size(agent,3),
fnew=fobj(newagent(j,:));
if fnew<=fitness(j),
fitness(j)=fnew;
agent(j,:)=newagent(j,:);
end
end
[fmin,K]=min(fitness) ;
best=agent(K,:);
function s=simplebounds(s,Lb,Ub)
ns_tmp=s;
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
s=ns_tmp;
function f=fobj(x)
filename = 'D_test2.xlsx';
sheet = 1;
xlRange = 'A2:E60';
Mm = xlsread(filename, sheet, xlRange);
HB=(Mm(:,2));
P=(Mm(:,3));
QTU=(Mm(:,1));
Rg=(Mm(:,4));
k=0.00981;
m=(Mm(:,5));
x1=QTU(:);
f =sum((x1 - x(2)./(k.*x(1).*x(3))).^ 2);
dbstop if error
i expect to have the same dimensions with this code below
function [best_agent,fmin]=Ago_algorithmv6(n)
clear
clc
if nargin<1,
n=10;
end
m_k=0;
lp1=0.7;
lp2=0.5;
bg=0;
w_k=0;
N_IterTotal=2000;
nd=3;
Lb = repmat([36.5, 50, 0.73], nd, 1);
Ub = repmat([41.5, 396, 0.94], nd, 1);
sum =0;
for t=1:1;
disp('agent(i,:) bp_k(i)');
for i=1:n,
for j=1:n
agent(i,:,:)=Lb(:,:)+(Ub(:,:)-Lb(:,:)).*rand(size(Lb));
w_array(i,:,:)= agent(i,:,:);
bp_k(i,:,:)= agent(i,:,:);
end
end
fitness=10^10*ones(n,1);
[fmin,best_agent,agent,fitness]=get_best_agent(agent,agent,fitness);
N_iter=0;
for iter=1:N_IterTotal,
for jj=1:n
s=agent(jj,:,:);
w=w_array(jj,:,:);
s=s + lp1*(best_agent-w)*rand + lp2*(bp_k(jj,:,:)*rand - w);
w =((w+ s ))/rand;
s=simplebounds(s,Lb,Ub);
w_array(jj,:,:)=w;
if fobj(s)< fobj(agent(jj,:,:))
agent(jj,:,:)=s;
end
if fobj(s)<fobj(bp_k(jj,:,:)),
bp_k(jj,:,:)=s;
end
if fobj(s)<fobj(best_agent),
best_agent=s;
end
end
best_agent
fmin= fobj(best_agent)
end
end
function [fmin,best,agent,fitness]=get_best_agent(agent,newagent,fitness)
for j=1:size(agent,3),
fnew=fobj(newagent(j,:));
if fnew<=fitness(j),
fitness(j)=fnew;
agent(j,:)=newagent(j,:);
end
end
[fmin,K]=min(fitness) ;
best=agent(K,:);
function s=simplebounds(s,Lb,Ub)
ns_tmp=s;
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
s=ns_tmp;
function f=fobj(x)
filename = 'D_test2.xlsx';
sheet = 1;
xlRange = 'A2:E60';
Mm = xlsread(filename, sheet, xlRange);
HB=(Mm(:,2));
P=(Mm(:,3));
QTU=(Mm(:,1));
Rg=(Mm(:,4));
k=0.00981;
m=(Mm(:,5));
x1=QTU(:);
f =sum((x1 - x(2)./(k.*x(1).*x(3))).^ 2);
Please run the two code to understand my requests. You will see below the attach document for running this code.
Best Answer