Week 4 - Genetic Algorithm
AIM:
To optimise the stalagmite function and find the global maxima of the function.
ABOUT GENETIC ALGORITHM:
- In compter science and operations research, a genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithm (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover and selection. John holland introduced genetic algorithms in 1960 based on the concept of Darwin's theory of evolution his student David E.Goldberg further extended GA in 1989.
-
In a genetic algorithm, a population of candidate solutions (called individuals, creatures, or phenotypes) to an optimization problem is evolved toward better solutions. Each candidate solution has a set of properties (its chromosomes or genotype) which can be mutated and altered traditionally, solutions are represented in binary as strings of 0s and 1s, but other encodings are also possible.
- A typical genetic algorithm requires
1. A genetic representation of the solution domain.
2. A fitness function to evaluate the solution domain
CODE EXPLANATION:
- In this program we are using linspace command to get a 150 numbers between 0 to 0.6.
- Then mesh grid command is used to create a 2D plot.
- we are attempting 100 iterations to get a robust output usinig GA.
- The for loop is used to solve every element in the mesh grid.
- The process goes with the 3 study's.
- The command optimoptions is introduced to increase the population size.
- we'll get bounded and unbounded outputs.
MAIN PROGRAM:
clear all
close all
clc
%Inputs
x= linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_case= 100;
% creating two dimensional 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)= get_stalagmite(input_vector);
end
end
%study 1
tic
% Taking inputs from stalagmite function
for i=1:num_case
[inputs,fopt(i)]=ga(@get_stalagmite,2);
xopt(i)= inputs(1);
ypot(i)= inputs(2);
end
study1_time=toc
figure(1)
subplot(2,1,1)
hold on
% obtaining the surface plot
surfc(xx,yy,f)
shading interp
% plot
title('unbouned inputs')
xlabel('x values')
ylabel('y values')
% For marker points
plot3(xopt,yopt,fopt,'o','markersize','6','markerfacecolor','b')
subplot(2,1,2)
% Planar plots for input
plot(fopt)
xlabel('iterations')
ylabel('Function maximum')
% Study2
tic
for i=1:num_cases
[inputs,fopt(i)]= ga(@get_stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)= inputs(1);
ypot(i)= inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
shading interp
title('Bouned inputs')
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,fopt,'o','markersize','6','markerfacecolor','g')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('Function maximum')
% study3 ( population limit)
options=optimoptions('ga')
options=optimoptions(options,'populationsize',170)
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@get_stalagmite,2,[],[],[],[],[0;0],[0.6,0.6],[],[],options);
xopt(i)= inputs(1);
ypot(i)= inputs(2);
end
fopt=-fopt;
f=-f;
study2time= toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('xvalues')
ylabel('yvalues')
plot3(xopt,yopt,fopt,'marker','o','markersize','5','markerfacecolor','c')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('Functionmaximum')
max_value=fopt
SUB PROGRAM:
function[v]= get_stalagmite(a)
x=a(1,1);
y=a(1,2);
f1_x =(sin(((5.1*pi*x)+(0.5)))).^6;
f1_y =(sin(((5.1*pi*y)+(0.5)))).^6;
f2_x = exp(-4*log(2)*((x-0.0667)^2/(0.64)));
f2_y = exp(-4*log(2)*((y-0.0667)^2/(0.64)));
v = -(f1_x*f1_y*f2_x*f2_y);
end