GLOBAL MAXIMA BY GENETIC ALGORITHM
AIM:To evaluate the global maxima by genetic algorithm using matlab.
THEORY:
Genetic algorithm:ga is the solver like ode 45 but ga optimizes the function where as ode 45 solve ode.In matlab,ga is built only to minimize value of the function.
Optimization:It is to maximize or minimize the function.
Crossover:It is a convergence operation which is intended to pull the population towards a local maximum/minimum.
Mutation:it is intended to break one or more members of population out of local maximum/minimum.
SYNTAX:
1.[x,fval]=ga(fun,nvars)
where,
x=inputs
fval=function value
ga=genetic
fun=function name
nvars=number of variables
2.[x,fval]=ga(fun,nvars,A,b,Aeq,beq,lb,ub)
where,
A,b,Aeq,beq=defines linear and non-linear constraints
lb=lower bounds
ub=upper bounds
3.[x,fval]=ga(fun,nvars,A,b,[],[],nonlcon,intcon,options)
where,
nonclon=non-linear constraints
intcon=integer constraints
options=optimisation option
4.options=optimoptions(solvername)
solver name=ga
5.options=optimoptions(solvername,name,value)
where,
name=population size
value=populationsize value
FUNCTION CODE:
function[inputs]=stalagmite(input_vector)
x1=input_vector(1);
y1=input_vector(2);
f1_x=(sin(5.1*pi*x1+0.5))^6;
f1_y=(sin(5.1*pi*y1+0.5))^6;
f2_x=exp(-4*log(2)*(x1-0.0667)^2/0.64);
f2_y=exp(-4*log(2)*(y1-0.0667)^2/0.64);
[inputs]=-(f1_x.*f1_y.*f2_x.*f2_y);
end
MAIN CODE:
clear all
close all
clc
% defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% creating a 2d mesh
[xx yy]=meshgrid(x,y);
%evaluating the stalagmite function
for i=1:length(xx)
for j=1:length(yy)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stalagmite(input_vector);
end
end
surfc(xx,yy,-f)
xlabel('x value')
ylabel('y value')
shading interp
[inputs ,fval]=ga(@stalagmite,2)
FIGURE(1):
CODING FOR ITERATIONS:
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50
% creating a 2d mesh
[xx yy]=meshgrid(x,y);
%evaluating the stalagmite function
for i=1:length(xx)
for j=1:length(yy)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stalagmite(input_vector);
end
end
tic
% study 1-satistical behaviour
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('function minimum')
tic;
% study 2 satistical behaviour
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.07;0.07]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('function maximum')
% increasing ga iterations
options=optimoptions(@ga)
options=optimoptions(@ga,'populationsize',50)
tic;
% study 3 satistical behaviour
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.07;0.07],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title('bounded inputs with increased population size')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('function maximum')
tic;
CONCLUSION:
surface plot generated by ga for unbounded inputs:
surface plot generated by ga for bounded inputs:
surface plot generated by ga with lower and upper bounds and increased population size: