# I don't understand why does it give error?

1 回表示 (過去 30 日間)
Sadiq Akbar 2023 年 1 月 5 日
コメント済み: Alan Stevens 2023 年 1 月 8 日
I downloaded the following code from Mathworks site whose URL is :
But when I run it, it gives an error. The code is:
%######### Example_Functions.m file ###########
%#### Write the several benchmark functions for BF-PSO here example of sphere is given ###
function fitns_fn=Fitness_fn(x) % Sphere function
fitns_fn=sum(x.^2);
end
%########### Function_limit.m file #######
%#### Give the limits of benchmark functions for BF-PSO ###
function[minm,maxm]=Function_limit(Example_Function)
minm=-100; % minimum value of dmension (or variables)
maxm=100; % maximum value of dmension (or variables)
end
%########### BF-PSO.m file #############
%####### Butterfly-PSO (or BF-PSO)#########
clear all
clc
format short
global bf
global dm
global Example_Function
[minm,maxm]=Function_limit(Example_Function);
ft_fn='Fitness_fn';
%############### Initialization ##############
flight_max = 1000; % Max number of flights by BF(or iterations)
itera_max=flight_max;
bf = 20; % Size of the butterfly swarm
dm = 30; % dimensions, 2, 10, 20, 30 ( or problem variables)
c1 =2; % Acceleration rate C1
c2 =2; % Acceleration rate C2
pg=1;
locations = rand(dm, bf).*(maxm-minm)+minm;
%%%% velocity limits as per variable problems;%%%%%%
vmax=((maxm-minm)/(rand*maxm));
vmin=-vmax;
velocity = rand(dm, bf).*(vmax-vmin)+vmin;
current_fitness = feval(ft_fn,locations);
local_best_locations = locations;
mean_best_locations=mean(local_best_locations,2);
local_best_fitness = current_fitness;
[global_best_fitness,Ind] = min(local_best_fitness);
globl_best_locations = local_best_locations(:,Ind);
%%%%%%%%%%% Main iterations counter Loop %%%%%%%%%
itera=0;
while (itera < itera_max) % start main iteration loop
itera = itera+1; p=ones(dm,bf)*(global_best_fitness./(sum(local_best_fitness))); %%% probability of nectar
ptt=(1-pt);
s=ones(dm, bf)*exp(-(itera_max-itera)/itera_max); % sensitivity of butterflies
w=(itera_max-itera)/itera_max;
velocity=w.*velocity+ptt.*s.*c1.*rand(dm,bf).*(local_best_locations-locations)+...
pg.*c2.*rand(dm,bf).*((globl_best_locations-locations)*ones(1,bf)); % velocity updation
velocity=min(vmax,max(vmin,velocity));% check velocity limits
locations=locations+rand.*pt.*velocity);
% location updation
locations=min(maxm, max(minm,locations)); % check location or variable limits
%%%%%%%%%%%%%%%%%% Updating local, mean and global best %%%%%%%
current_fitness=feval(ft_fn,locations);
Ifit=find(current_fitness<local_best_fitness);
local_best_fitness(:,Ifit)=current_fitness(:,Ifit);
local_best_locations(:,Ifit)=locations(:,Ifit);
[current_global_best_fitness,Ind]=min(local_best_fitness);
if current_global_best_fitness < global_best_fitness
global_best_fitness = current_global_best_fitness;
end
globl_best_locations=local_best_locations(:,Ind);
minfit2(itera+1)=global_best_fitness;
% record min fitness value for plot
end % end of main iteration loop
min_fit=global_best_fitness
globl_best_locati=globl_best_locations
bf_mean_fit=mean(minfit2)
bf_stdvn_fit=std(minfit2)
%%%%%% plots %%%%%%%
itera1=0:length(minfit2)-1;
plot(itera1, minfit2,'b')
xlabel('Iterations');
ylabel('Fitness Value');
legend('BF-PSO')

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

### 回答 (1 件)

Alan Stevens 2023 年 1 月 5 日
Put the functions at the end. Tidy up one or two minor errors to get:
%########### BF-PSO.m file #############
%####### Butterfly-PSO (or BF-PSO)#########
clear all
clc
format short
global bf
global dm
global Example_Function
[minm,maxm]=Function_limit(Example_Function);
ft_fn='Fitness_fn';
%############### Initialization ##############
flight_max = 1000; % Max number of flights by BF(or iterations)
itera_max=flight_max;
bf = 20; % Size of the butterfly swarm
dm = 30; % dimensions, 2, 10, 20, 30 ( or problem variables)
c1 =2; % Acceleration rate C1
c2 =2; % Acceleration rate C2
pg=1;
locations = rand(dm, bf).*(maxm-minm)+minm;
%%%% velocity limits as per variable problems;%%%%%%
vmax=((maxm-minm)/(rand*maxm));
vmin=-vmax;
velocity = rand(dm, bf).*(vmax-vmin)+vmin;
current_fitness = feval(ft_fn,locations);
local_best_locations = locations;
mean_best_locations=mean(local_best_locations,2);
local_best_fitness = current_fitness;
[global_best_fitness,Ind] = min(local_best_fitness);
globl_best_locations = local_best_locations(:,Ind);
%%%%%%%%%%% Main iterations counter Loop %%%%%%%%%
itera=0;
% You need to define pt (arbitrary value here)
pt = 0.1;
while (itera < itera_max) % start main iteration loop
itera = itera+1; p=ones(dm,bf)*(global_best_fitness./(sum(local_best_fitness))); %%% probability of nectar
ptt=(1-pt);
s=ones(dm, bf)*exp(-(itera_max-itera)/itera_max); % sensitivity of butterflies
w=(itera_max-itera)/itera_max;
velocity=w.*velocity+ptt.*s.*c1.*rand(dm,bf).*(local_best_locations-locations)+...
pg.*c2.*rand(dm,bf).*((globl_best_locations-locations).*ones(1,bf)); % velocity updation
velocity=min(vmax,max(vmin,velocity));% check velocity limits
locations=locations+rand.*pt.*velocity;
% location updation
locations=min(maxm, max(minm,locations)); % check location or variable limits
%%%%%%%%%%%%%%%%%% Updating local, mean and global best %%%%%%%
current_fitness=feval(ft_fn,locations);
Ifit=find(current_fitness<local_best_fitness);
local_best_fitness(:,Ifit)=current_fitness(:,Ifit);
local_best_locations(:,Ifit)=locations(:,Ifit);
[current_global_best_fitness,Ind]=min(local_best_fitness);
if current_global_best_fitness < global_best_fitness
global_best_fitness = current_global_best_fitness;
end
globl_best_locations=local_best_locations(:,Ind);
minfit2(itera+1)=global_best_fitness;
% record min fitness value for plot
end % end of main iteration loop
min_fit=global_best_fitness;
globl_best_locati=globl_best_locations;
bf_mean_fit=mean(minfit2);
bf_stdvn_fit=std(minfit2);
%%%%%% plots %%%%%%%
itera1=0:length(minfit2)-1;
plot(itera1, minfit2,'b')
xlabel('Iterations');
ylabel('Fitness Value');
legend('BF-PSO')
%######### Example_Functions.m file ###########
%#### Write the several benchmark functions for BF-PSO here example of sphere is given ###
function fitns_fn=Fitness_fn(x) % Sphere function
fitns_fn=sum(x.^2);
end
%########### Function_limit.m file #######
%#### Give the limits of benchmark functions for BF-PSO ###
function[minm,maxm]=Function_limit(Example_Function)
minm=-100; % minimum value of dmension (or variables)
maxm=100; % maximum value of dmension (or variables)
end
##### 5 件のコメント3 件の古いコメントを表示3 件の古いコメントを非表示
Sadiq Akbar 2023 年 1 月 8 日
Thank you very much dear Alan Stevens for your kind response. I am giving both the codes in the attachment. One is that corrected by you called "BFPSO_AlanStevens.m" and one is that changed by me called "BFPSO_mine.m". As you can see "BFPSO_mine" is also the same as is yours. But this shows the changes where I have made in yours. It works from the start lines but gives the error where it calls my function. And my function is given at the very bottom of the whole code. You can locate it very easily now. But why it is giving an error, that I don't undesratnd. Further, it also includes the value of Pt you defined. But the reason of error is not understood by me. The only difference in the two functions is that my function has 4 variables and has lower bound as [0 0 0 0] and upper bound as [10 10 90 90] but the original function has 30 variables and all have lower bound as -100 and upper bound as 100.
Regards,
Alan Stevens 2023 年 1 月 8 日
In your function b1 and b2 have different sizes from k, hence you can't do element by element multiplication as required in xe. I don't know enough about exactly what your program is modelling to help any further,
%%%%%%%%%%% My Function %%%%%%%%%%%%%%%
function e=karimVectorized1(b,u) %My function------------(8)
u=u';
b=b';
[R,~] = size(b);
P = R/2;
M = 2*R;
%%%%%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix,:)] = sort(b); %%%%%%%%%%%%%%%%%%%%%%%%%%%
b = b(ix1);
%%%%%%%%%%%%%%%
% calculate xo
%%%%%%%%%%%%%%%
u1 = u( 1:P );
u2 = u( P+(1:P));
k = 1:M;
xo = sum( u1.*exp( -1i.*pi*cosd(u2).*(k-1) ) , 1);
% xo = awgn(xo,Noise);% add Noise
%%%%%%%%%%%%%%%
% Calculate xe
%%%%%%%%%%%%%%%
b1 = b( 1:P );
b2 = b( P+(1:P));
% b1 and b2 have size 1x2, but k has size 1x8 hence the bracketed term
% in the following line cannot be executed.
xe = sum( b1.*exp( -1i.*pi*cosd(b2).*(k-1) ) , 1);
%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%
%e = mean( abs( xo(1,:) - xe(1,:) ) .^2 );% Both are correct
e = mean(abs(xo-xe).^2,2);
end

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

### カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by