I don't understand why does it give error?
2 次查看(过去 30 天)
显示 更早的评论
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')
0 个评论
回答(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 个评论
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!