How to apply bat algorithm to spring design problem?
Show older comments
This is what I did:
%[5 20 1 0]
% Main programs starts here
function [best,fmin,N_iter]=bat_algorithm_spring1(para)
format long;
% Lb=[0.05 0.25 2.0]; Ub=[1.0 1.3 15.0];
% Default parameters
n=para(1) ; % Population size, typically 10 to 40
N_gen=para(2); % Number of generations
A=para(3) ; % Loudness (constant or decreasing)
r=para(4) ; % Pulse rate (constant or decreasing)
% This frequency range determines the scalings
% You should change these values if necessary
Qmin=0.0025; % Frequency minimum
Qmax=88.4 ; % Frequency maximum
% Iteration parameters
N_iter=0 ; % Total number of function evaluations
% Dimension of the search variables
d=3 ; % Number of dimensions
% Lower limit/bounds/ a vector
Lb=0.0025*ones(1,d);
% Upper limit/bounds/ a vector
Ub=88.4*ones(1,d);
% Initializing arrays
Q=zeros(n,1) ; % Frequency
v=zeros(n,d) ; % Velocities
% Initialize the population/solutions
for i=1:n,
Sol(i,:)=Lb+(Ub-Lb).*rand(1,d);
Fitness(i)=objfun(Sol(i,:));
end
% Find the initial best solution
[fmin,I]=min(Fitness);
best=[0.09 0.35 11];
% Start the iterations -- Bat Algorithm (essential part) %
for t=1:N_gen,
% Loop over all bats/solutions
for i=1:n,
Q(i)=Qmin+(Qmax-Qmin)*rand;
v(i,:)=v(i,:)+(Sol(i,:)-best)*Q(i);
S(i,:)=Sol(i,:)+v(i,:);
% Apply simple bounds/limits
%Sol(i,:)=simplebounds(Sol(i,:),Lb,Ub);
% Pulse rate
if rand>r
% The factor 0.001 limits the step sizes of random walks
S(i,:)=best+0.001*randn(1,d);
end
% Evaluate new solutions
Fnew=objfun(S(i,:));
% Update if the solution improves, or not too loud
if (Fnew<=Fitness(i)) & (rand<A) ,
Sol(i,:)=S(i,:);
Fitness(i)=Fnew;
% A=0.025*A;
%r=r*(1-exp(-1*t));
end
% Update the current best solution
if Fnew<=fmin,
best=S(i,:);
fmin=Fnew;
end
end
N_iter=N_iter+n;
end
% Output/display
disp(['Number of evaluations: ',num2str(N_iter)]);
disp(['Best =',num2str(best),' fmin=',num2str(fmin)]);
% Application of simple limits/bounds
function s=simplebounds(s,Lb,Ub)
% Apply the lower bound vector
ns_tmp=s;
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
% Apply the upper bound vector
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
%Update this new move
s=ns_tmp;
% Objective function
function f=objfun(x)
f=(2+x(3))*(x(1)^2)*x(2);
% Nonlinear constraints
function [g,geq]=nonfun(x)
% Inequality constraints
g(1)=1-(((x(2)^3)*(x(3)))/(71785*(x(1)^4)));
% Notice the typo 7178 (instead of 71785)
gtmp=((4*(x(2))^2)-(x(1)*x(2)))/(12566*(x(2)*(x(1)^3)-(x(1)^4)));
g(2)=gtmp+(1/(5108*(x(1)^2)))-1;
g(3)=1-((140.45*x(1))/((x(2)^2)*x(3)));
g(4)=((x(1)+x(2))/1.5)-1;
% Equality constraints [none]
geq=[]
This converges lower than the real value. What could be wrong?
1 Comment
Star Strider
on 12 Oct 2014
Answers (1)
Joe Ajay
on 8 Feb 2015
0 votes
Hi, am too working on that; got the same problem here. Got any answers?
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!