Function problem in loop

2 ビュー (過去 30 日間)
Amin Waqas
Amin Waqas 2021 年 1 月 15 日
コメント済み: Amin Waqas 2021 年 1 月 15 日
Dear researchers i am facing a problem in using function while running in a loop please guide me to solve the problem. My objective is to get the Gbest values over every iteration. Here is the code which i am using and intnded to run in a loop for i=1:10
First function
function out=fns1(X)
x1=X(:,1);
x2=X(:,2);
x3=X(:,3);
x4=X(:,4);
x5=X(:,5);
x6=X(:,6);
x7=X(:,7);
x8=X(:,8);
x9=X(:,9);
x10=X(:,10);
p1=P(:,1);
p2=P(:,2);
p3=P(:,3);
p4=P(:,4);
p5=P(:,5);
p6=P(:,6);
p7=P(:,7);
p8=P(:,8);
p9=P(:,9);
p10=P(:,10);
out = p1.*x1+p2.*x2+p3.*x3+p4.*x4+p5.*x5+p6.*x6+p7.*x7+p8.*x8+p9.*x9+p10.*x10+3;
end
Second Function
function out= fns(X)
x1=X(:,1);
x2=X(:,2);
x3=X(:,3);
x4=X(:,4);
x5=X(:,5);
x6=X(:,6);
x7=X(:,7);
x8=X(:,8);
x9=X(:,9);
x10=X(:,10);
p1=P(:,1);
p2=P(:,2);
p3=P(:,3);
p4=P(:,4);
p5=P(:,5);
p6=P(:,6);
p7=P(:,7);
p8=P(:,8);
p9=P(:,9);
p10=P(:,10);
fx =p.*x1+p.*x2+p.*x3+p.*x4+p.*x5+p.*x6+p.*x7+p.*x8+p.*x9+p.*x10+3;
for i=1:size(fx,1)
if fx(i,:)>=0
fit(i,:)=1./(1+fx(i,:));
elseif fx(i,:)<0
fit(i,:)=1+abs(fx(i,:));
end
end
out=fit;
Here is the main file which call these function
%%%%%
load s1.txt
load s2.txt
load s3.txt
load s4.txt
load s5.txt
load s6.txt
load s7.txt
load s8.txt
load s9.txt
load s10.txt
load p1.txt
load p2.txt
load p3.txt
load p4.txt
load p5.txt
load p6.txt
load p7.txt
load p8.txt
load p9.txt
load p10.txt
s1g=s1(:,1);
s2g=s2(:,1);
s3g=s3(:,1);
s4g=s4(:,1);
s5g=s5(:,1);
s6g=s6(:,1);
s7g=s7(:,1);
s8g=s8(:,1);
s9g=s9(:,1);
s10g=s10(:,1);
p1=p1(:,1);
p2=p2(:,1);
p3=p3(:,1);
p4=p4(:,1);
p5=p5(:,1);
p6=p6(:,1);
p7=p7(:,1);
p8=p8(:,1);
p9=p9(:,1);
p10=p10(:,1);
foodsource=10;
D=10;%%% dimension of the problem
lb=[];
ub=[];
XYZ=[];
fxval=[];
fxind=[];
for i=1:2
lb=[0 0 0 0 0 0 0 0 0 0];%% lower bound
ub=[s1g(i) s2g(i) s3g(i) s4g(i) s5g(i) s6g(i) s7g(i) s8g(i) s9g(i) s10g(i)];%%% upper bound
P=[p1(i) p2(i) p3(i) p4(i) p5(i) p6(i) p7(i) p8(i) p9(i) p10(i)];
N=foodsource;%%%population
limit= (N.*D);%%%used for scout phase
trial=zeros(N,1)%%%init tozero
max_iter=150;
for i=1:N
for j=1:D
%A(i,j)=ub(:,j)-lb(:,j)
A(i,j)= ub(:,j);
end
end
for i=1:N
for j=1:D
pos(i,j)= ub(:,j);
end
end
fx(i,:,:) = fns1(pos(i,:,:)); %%% Miniiation objective function
fx1(i,:,:) = fns(pos(i,:,:)); %%% Max fitness function
for iter=1:max_iter
for i=1:N
Xnew(i,:,:)=pos(i,:);
Xnew(i,:,:)=pos(i,:,:);
B=max(A(:,1));
[M,I] = max(A(:,1));
p2c= (I*D);
partner= ceil (rand*N);
while (partner==i)
partner=ceil(rand*N);
end
X=pos(i,p2c);
Xp=pos(partner,p2c);
Xnew(p2c)= X +0.2.*(X-Xp);
%%%check the bounds%%%
for j=1:D
if Xnew(j)>ub(j)
Xnew(j)=ub(j);
elseif Xnew(j)<lb(j)
Xnew(j)=lb(j);
end
end
%%%perform greedy selection
fnew(i,:,:)=fns1(Xnew(i,:,:));
if fnew(i,:,:) <fx(i,:,:)
pos(i,:,:)=Xnew(i,:,:);
fx(i,:,:)=fnew(i,:,:);
trial(i)=0;
else
trial(i)= trial(i)+1;
end
end%%% End for loop
prob=fx./sum(fx);
for i=1:N
if(rand<prob(i))
Xnew(i,:,:)=pos(i,:,:);
p2c= (I*D);
partner=ceil(rand*N);
while (partner==1)
partner=ceil(rand*N);
end
X=pos(i,p2c);
Xp=pos(partner,p2c);
Xnew(p2c)= X + 0.2.*(X-Xp);
%%%%Check the bounds
for j=1:D
if Xnew(j)>ub(j)
Xnew(j)=ub(j);
elseif Xnew(j)<lb(j)
Xnew(j)=lb(j);
end
end
%%%perform the greedy selection
fnew(i,:,:)=fns1(Xnew(i,:,:));
if fnew(i,:,:)<fx(i,:,:)
pos(i,:,:)=Xnew(i,:,:);
fx(i,:,:)=fnew(i,:,:);
trial(i)=0;
else
trial(i)=trial(i)+1;
end
end
end
%%%memorize the best soln so far
[ fxval, fxind]=min(fx);
Gbest(i,:,:)=pos(fxind,:);
XYZ(i,:,:)=Gbest(i,:,:);
Xbest(iter,:)= Gbest(i,:,:);
Fbest(iter)=fxval;
Fbest1(iter)= fns1(Gbest(i,:,:));
xyz=sum(Gbest(i,:,:));
%%% Show iter information
disp(['iteration' num2str(iter) ' Best Cost : =' num2str(Fbest(iter))]);
H=find(trial>limit);
if length (H)>0
%%% generate population randomly for such H
for j=1:D
pos(H,j)=lb(:,j) +rand.*(ub(:,j)-lb (:,j));
%pos(H,j)=ub(:,j)
end
fx(H,:)=fns1(pos(H,:));
end
%%%plot the result
plot(Fbest1, 'r', 'Linewidth',2);
xlabel=('Iteration');
ylabel=('Fitness value');
grid on
end
end
%%%%
The values for p1-p10 and values of x1-x10 are changing with respect to each time slot. I need to get the Gbest=pos(fxind,:) over every iteration for for the loop i intended to run is for i=1:100.
Thanks in advance
  4 件のコメント
Amin Waqas
Amin Waqas 2021 年 1 月 15 日
Thanks Daniel Pollard i will take care your suggestions in future
Amin Waqas
Amin Waqas 2021 年 1 月 15 日
KSSV what you suggest to change plz Highlight so that i can get my answer

サインインしてコメントする。

回答 (1 件)

KSSV
KSSV 2021 年 1 月 15 日
function out=fns1(X)
x1=X(:,1);
x2=X(:,2);
x3=X(:,3);
x4=X(:,4);
x5=X(:,5);
x6=X(:,6);
x7=X(:,7);
x8=X(:,8);
x9=X(:,9);
x10=X(:,10);
p1=P(:,1);
p2=P(:,2);
p3=P(:,3);
p4=P(:,4);
p5=P(:,5);
p6=P(:,6);
p7=P(:,7);
p8=P(:,8);
p9=P(:,9);
p10=P(:,10);
out = p1.*x1+p2.*x2+p3.*x3+p4.*x4+p5.*x5+p6.*x6+p7.*x7+p8.*x8+p9.*x9+p10.*x10+3;
end
The above function can be simply replaced with the line:
out = sum(X.*P,2)+3 ;
  5 件のコメント
KSSV
KSSV 2021 年 1 月 15 日
You have to show the whole code which you have tried and lead to error.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by