366 Appendix B
[E_new,constr] = func1(x);
if E_new < E_old
E_old = E_new;
else
if exp(-(E_new-E_old)/E_old)> rand
E_old = E_new;
end
x = x - perturb_x;
end
px(j) = j;
py(j) = E_new;
if E_new < best_obj
best_obj = E_new;
bestx = x;
flag = 0;
end
[j bestx best_obj]
if flag > 1000
break;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File name func1.m
% Enter the function to be optimized
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function [y,constr] = func1(x)
y = 20 + x(1)*x(1)-10*cos(2*pi*x(1)) + x(2)*x(2)-
10*cos(2*pi*x(2));
constr(1) = 10;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File name pso.m
% Particle Swarm Optimization algorithm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% lb -> lower bound of variables
% ub -> upper bound of variables
% x -> position of individual
% v -> velocity of individual
% rand -> random number from 0 to 1
% fitness -> fitness of individual
% pbest -> best fitness achieved by individual
% gbest -> best fitness of group
367Appendix B
% pop -> population size
% phi_1, phi_2 -> tuning parameters
% nmax -> maximum number of iterations
%
clear all
clc
format long
pop = 20;
phi_1 = 1.05;
phi_2 = 1.1;
nmax = 100;
weight = linspace(1,0.3,nmax);
lb = [-500 -500];
ub = [500 500];
for i = 1:length(lb)
for j = 1:pop
x(i,j) = lb(i) + (ub(i)-lb(i))*rand;
v(i,j) = 0;
end
end
for i = 1:pop
fitness(i) = func1(x(:,i));
pbest(i) = fitness(i);
px(i,:) = x(:,i);
end
[gbest, location] = min(fitness);
gx = x(:,location);
plot3(px(:,1),px(:,2),pbest,'r*')
grid on
xlabel('x1')
ylabel('x2')
zlabel('f(x)')
for i = 1:nmax
for j = 1:pop
v(:,j) = weight(i)*v(:,j) + phi_1*rand*(px(j,:)'-x(:,j)) +
phi_2*rand*(gx-x(:,j));
x(:,j) = x(:,j) + v(:,j);
for k = 1:length(x(:,j))
if x(k,j) < lb(k) || x(k,j) > ub(k)
x(k,j) = lb(k) + (ub(k)-lb(k))*rand;
end
end
fitness(j) = func1(x(:,j));
if fitness(j) < pbest(j)
pbest(j) = fitness(j);
px(j,:) = x(:,j);
end
end
[gbest, location] = min(pbest);
gx = x(:,location);
368 Appendix B
[gx' gbest]
plot3(px(:,1),px(:,2),pbest,'r*')
grid on
xlabel('x1')
ylabel('x2')
zlabel('f(x)')
axis([-500 500 -500 500 -1000 0])
pause(0.2)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File name func1.m
% Enter the function to be optimized
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function y = func1(x)
y = -x(1)*sin(sqrt(abs(x(1)))) -x(2)*sin(sqrt(abs(x(2))));
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Chapter 6
Code Name Details
DFP.m (main program) Davidon–Fletcher–Powell (DFP) method (see Chapter 3)
grad_vec.m (function) Gradient vector computation (see Chapter 3)
golden_funct1.m (function) Golden section method (see Chapter 3)
func1.m (function) Computes value of objective function
constr.m (function) Computes value of constraint function
pso.m (main program) Particle swarm optimization (PSO) method to solve welded
beam problem
func1.m (function) Computes value of objective function
constr.m (function) Computes value of constraint function
ALM.m (main program) Augmented Lagrangian method
func1.m (function) Computes value of augmented objective function
sqp.m (main program) Sequential quadratic programming method
func_val.m (function) Computes augmented Lagrangian function
func_val1.m (function) Computes function value
eqconstr_val.m (function) Computes equality constraints value
ineqconstr_val.m (function) Computes inequality constraints value
grad_vec_f.m Computes gradient vector of the objective function
grad_vec_eqcon.m (function) Computes gradient vector for equality constraints
grad_vec_ineqcon.m (function) Computes gradient vector for inequality constraints
hessian.m (function) Computes Hessian matrix (see Chapter 3)
369Appendix B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code func1.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% y -> objective function
% penalty -> penalty term
%
function y = func1(x,scale_factor)
y = (x(1)-1)^2 + (x(2)-5)^2;
penalty = 0.0;
[h,g] = constr(x);
for i = 1:length(h)
if h(i)~=0
penalty = penalty + h(i)^2;
end
end
for i = 1:length(g)
if g(i)>0
penalty = penalty + g(i)^2;
end
end
y = y+penalty*scale_factor;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code constr.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% define your constraints here
% g(1), g(2) -> inequality constraints
% h(1), h(2), -> equality constraints
%
function [h,g] = constr(x)
h(1) = 0;
g(1) = -x(1)^2 + x(2) -4;
g(2) = -(x(1)-2)^2 + x(2) -3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File name pso.m
% Particle Swarm Optimization algorithm
% Welded beam problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% lb -> lower bound of variables
% ub -> upper bound of variables
% x -> position of individual
% v -> velocity of individual
% rand -> random number from 0 to 1
370 Appendix B
% fitness -> fitness of individual
% pbest -> best fitness achieved by individual
% gbest -> best fitness of group
% nmax -> maximum number of iterations
%
clear all
clc
format long
pop = 200;
phi_1 = 1.05;
phi_2 = 1.1;
nmax = 3000;
scale_factor = 10000000;
weight = linspace(1,0.3,nmax);
fprintf('_________________________________________________\n')
lb = [0.1 0.1 0.1 0.1];
ub = [2 10 10 2];
for i = 1:length(lb)
for j = 1:pop
x(i,j) = lb(i) + (ub(i)-lb(i))*rand;
v(i,j) = 0;
end
end
for i = 1:pop
fitness(i) = func1(x(:,i),scale_factor);
pbest(i) = fitness(i);
px(i,:) = x(:,i);
end
[gbest, location] = min(fitness);
gx = x(:,location);
for i = 1:nmax
for j = 1:pop
v(:,j) = weight(i)*v(:,j) + phi_1*rand*(px(j,:)'-x(:,j))
+ phi_2*rand*(gx-x(:,j));
x(:,j) = x(:,j) + v(:,j);
for k = 1:length(x(:,j))
if x(k,j) < lb(k) || x(k,j) > ub(k)
x(k,j) = lb(k) + (ub(k)-lb(k))*rand;
end
end
fitness(j) = func1(x(:,j),scale_factor);
if fitness(j) < pbest(j)
pbest(j) = fitness(j);
px(j,:) = x(:,j);
end
end
[gbest, location] = min(pbest);
gx = x(:,location);
[gx' gbest];

Get Optimization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.