Appendix B MATLAB® PROGRAMS PDF
Appendix B MATLAB® PROGRAMS PDF
MATLAB® PROGRAMS
The following codes intend to demonstrate how each algorithm works, so they
are relatively simple and we do not intend to optimize them. In addition,
most demonstrative cases are for 2D only, though they can be extended to any
higher dimensions in principle. They are not for general-purpose optimization,
because there are much better programs out there, both free and commercial.
These codes should work using Matlab® 1 . For Octave, 2 slight modifications
may be needed.
The following simple demo program of genetic algorithms tries to find the
maximum of
f(x) = -cos(x)e-(>x-^2.
1
Matlab, www.mathworks.com
2
J . W. Eaton, GNU Octave Manual, Network Theory Ltd, www.gnu.org/software/octave,
2002.
% Mutation at n sites
if pm>rand,
kk=floor(popsize*rand)+l; count=count+l;
popnew(kk,:)=mutate(pop(kk,:),nsite);
evolve(kk);
end
end V, end for j
% Record the current best
bestfun(i)=max(fitness);
bestsol(i)=mean(sol(bestfun(i)==fitness));
end
% Display results
subplot (2,1,1); plot(bestsol); titleCBest estimates');
subplot(2,l,2); plot(bestfun); title('Fitness');
7. All the sub functions
'/, generation of the initial population
function pop=init_gen(np,nsbit)
% String length=nsbit+l with pop(:,l) for the Sign
pop=rand(np,nsbit+l)>0.5;
% Evolving the new generation
function evolve(j)
global solnew popnew fitness fitold pop sol f;
solnew(j)=bintodec(popnew(j,:));
fitness(j)=f(solnew(j));
if fitness(j)>fitold(j),
pop(j,:)=popnew(j,:);
sol(j)=solnew(j);
end
% Convert a binary string into a decimal number
function [dec]=bintodec(bin)
global range;
% Length of the string without sign
nn=length(bin)-l;
num=bin(2:end); % get the binary
'/. Sign=+1 if bin(l)=0; Sign=-l if bin(l)=l.
Sign=l-2*bin(l);
dec=0;
% floating point/decimal place in a binary string
dp=floor(log2(max(abs(range))));
for i=l:nn,
dec=dec+num(i)*2~(dp-i);
end
dec=dec*Sign;
% Crossover operator
function [c,d]=crossover(a,b)
270 MATLAB® PROGRAMS
nn=length(a)-l;
'/, generating a random crossover point
cpoint=floor(nn*rand)+l;
c=[a(l:cpoint) b(cpoint+l:end)];
d=[b(l:cpoint) a(cpoint+l:end)];
% Mutatation operator
function anew=mutate(a,nsite)
nn=length(a); anew=a;
for i=l:nsite,
j=floor(rand*nn)+l;
anew(j)=mod(a(j)+l,2);
end
end
% Update the estimated optimal solution
f_opt=E_old;
end
'/, Display the final results
disp(strcat('Obj function :',fstr));
disp(strcat('Evaluations :', num2str(totaleval)));
disp(strcat('Best solution:', num2str(best)));
disp(strcat('Best objective:', num2str(f_opt)));
272 MATLAB® PROGRAMS
The following PSO program tries to find the global minimum of Michaelwicz's
2D function
function [best]=pso_demo(n,Num_iterations)
% n=number of particles; Num_iterations=number of iterations
if nargin<2, Num_iterations=15; end
if nargin<l, n=20; end
'/. Michaelwicz Function f*=-1.801 at [2.20319,1.57049]
fstr='-sin(x)*(sin(x~2/pi))~20-sin(y)*(sin(2*y~2/pi))~20';
% Converting to an inline function and vectorization
f=vectorize(inline(fstr));
'/, range= [xmin xmax ymin ymax] ;
range=[0404];
%
°/o Setting the parameters: alpha, beta
alpha=0.2; beta=0.5;
I
'/, Grid values of the objective for visualization only
Ndiv=100;
dx=(range(2)-range(1))/Ndiv;dy=(range(4)-range(3))/Ndiv;
xgrid=range(l):dx:range(2); ygrid=range(3):dy:range(4);
[x,y]=meshgrid(xgrid,ygrid);
z=f(x,y);
'/, Display the shape of the function to be optimized
figure(l); surfc(x,y,z);
χ
best=zeros(Num_iterations,3); °/0 initialize history
'/, Start Particle Swarm Optimization
'/, generating the initial locations of n particles
[xn,yn]=init_pso(n,range);
% Display the particle paths and contour of the function
figure(2);
% Start iterations
for i=l:Num_iterations,
% Show the contour of the function
contour(x,y,z,15); hold on;
B.4 HARMONY SEARCH 273
This simple program for Harmony Search intends to find the global minimum
of Rosenbrock's function f(x,y) = (1 - x)2 + 100(y — x2)2, which can easily
be extended to any dimension.
274 MATLAB® PROGRAMS
which has four equal peaks. In order to obtain the analytical solutions, we
have to differentiate it, but this function does not have derivative at (0,0).
However, the function is symmetric in x and y. That is, the function remains
the same when x and y are interchanged. So we only have to find the solution
in the first quadrant x > 0 and y > 0. In the first quadrant, we have
and
^- = [\- 2ay{x + y)}e-a{x2+y2) = 0. (B.4)
If you run the Matlab program with 25 fireflies, all four peaks can be found
simultaneously.
To solve a large-scale sparse linear system, Krylov subspace methods are most
efficient in most cases. For example, the conjugate gradient method is an
efficient solver for
Au = b,
where A is a normal matrix. In our simple implementation, it requires that
A is real and symmetric.
while max(abs(r))>delta
alpha=r'*r/(d'*A*d); '/„ alpha_n
K=r'*r; °/0 temporary value
u=u+alpha*d; % u_{n+l}
r=r-alpha*A*d; '/, r_{n+l}
beta=r'*r/K; '/, beta_n
B.7 NONLINEAR OPTIMIZATION 279
d=r+beta*d; 7, d_{n+l}
u' °/0 d i s p u on t h e screen
end
Initially, you can run the program using a small n = 50, which will show
almost exact answers. Then, you can try n = 500 and 5000 as further numeric
experiments.
0.05 < w < 2.0, 0.25 < d < 1.3, 2.0 < L < 15.0. (B.10)
92\X) - 12566ϊ2χ?-χί +
5108x^ _ L
- U
(B.12)
g3(x) = l - i f g ^ < o
g4(x) = ^ i - 1< 0
with simple bounds
0.05 < xi < 2.0, 0.25 < x2 < 1.3, 2.0 < x3 < 15.0. (B.13)
280 MATLAB® PROGRAMS
If we run this simple Matlab program, we can obtain the optimal solution
x* « (0.0500,0.2500,9.9877), (B.18)
which gives / m i n ~ 0.0075. This solution is better than the best solution
obtained by Cagnina et al.
/* « 0.012665, (B.19)
at
x* w (0.051690, 0.356750, 11.287126). (B.20)
B.7 NONLINEAR OPTIMIZATION 281
Ax < b, (B.23)
where
Ί 0 0.0193 0^
0 - 1 0.00954 0 | , 6 = ( 0 |. (B.24)
^0 0 0 1,
The simple bounds can be rewritten as
b=[0; 0; 240];
% Simple bounds
Lb=[d; d; 10; 10]; Ub=[99*d; 99*d; 200; 200];
options=optimset('Display','iter','TolFun',le-08);
[x,fval]=fmincon(@objfun,xO,A,b, [] , [] ,Lb,Ub,<2nonfun,options)
% The objective function
function f=objfun(x)
f=0.6224*x(l)*x(3)*x(4)+1.7781*x(2)*x(3)~2 . ..
+3.1661*x(l)~2*x(4)+19.84*x(l)~2*x(3)
'/, Nonlinear constraints
function [g,geq]=nonfun(x)
% Nonlinear inequality
g=-pi*x(3)"2*x(4)-4*pi/3*x(3)"3+1296000;
7. Equality constraint [none]
geq= [];
In the above code, '...' means the line continues. That is, the current line and
the next line should form a single line in Matlab. This '...' is only added for
typesetting the code to avoid an ugly long line.
If we run the above program, we have
x* « (0.7782,0.3846,40.3196,200.0000)τ, (Β.26)
with / m i n fa 5885.33, which is lower that the best solution /* « 6059.714
at so* « (0.8125, 0.4375, 42.0984, 176.6366), obtained by Cagnina et al.
It is worth pointing out that we have relaxed the constraints of d\ and efe
in the above implementation, as d\ and d? can only take discrete values of
integer multiples of 0.0625 in the original design problem. If we impose these
constraints, our program will produce exactly the same results as the best
solution /* m 6059.714 obtained by Cagnina et al. Here it is left as an exercise
for the readers to modify the program to achieve this. You can either add
extra constraints or change the formulation slightly.
From the above two examples (spring and pressure vessel design), we can
see that the optimizer f mincon in Matlab is a very good optimizer and suitable
for many applications.
REFERENCES