Week 4 - Genetic Algorithm
In computer 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 algorithms (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.
The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.
we can use ga to find the set of variables that would get us as close as possible to the expected results. Depending on runtime, problem, and domain, any terminators(boundary conditions)are acceptable. Using a GA helps get algorithms within tolerances fairly quickly.
basic syntax is
[inputs,fval]=ga(function,no of variable)
using this we will get values below and above the lower and upper limits to overcome this well be using bounds
while using bounds syntax is
[inputs,fval]=ga(function,no of variable,x,y,X,Y,LB,UB)
x=linear inequality constraints
y=linear inequality constraints
X=linear equality constraints
Y=linear equality constraints
LB=lower bound
UB=upper bound
all linear constraints are not present we can represent them by []
this still has local maxima
syntax to decrease local maxima and increase the population size
[inputs,fval]=ga(function,no of variable,x,y,X,Y,LB,UB,[],[],options)
in the function, we have put '-' to obtain values to suits our GA, i.e. to invert it for obtaining maxima rather than minima
clear all
close all
clc
%defining inputs
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);
%creating matrix of inputs
[xx,yy]= meshgrid(x,y);
%no of iterations
num_cases=50;
%loop for calculating z values
for i=1:length(xx)
for j=1:length(yy)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
s(i,j)= stalagmite(input_vector);
end
end
%initial case condition
%creating 3d surface
figure(1)
surfc(xx,yy,-s); %using '-s' as our function
shading interp
xlabel('x values')
ylabel('y values')
title('Stalagmite Function')
%study 1- with unbound values
%segregating data
tic
for i =1:num_cases
[inputs,fval(i)]=ga(@stalagmite,2);
xopv(i)=inputs(1);
yopv(i)=inputs(2);
end
study_1=toc
%plotting graphs
figure(2)
subplot(2,1,1)
surfc(x,y,-s)
shading interp
xlabel('x values')
ylabel('y values')
title('unbounded values')
hold on
plot3(xopv,yopv,-fval,'marker','*','markersize',4,'markerfacecolor','g')
subplot(2,1,2)
plot(-fval)
xlabel('no of iterations')
ylabel('maximum of function value')
hold on
%study 2- with bounded limits
%segregating data
tic
for i =1:num_cases
[inputs,fval(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopv(i)=inputs(1);
yopv(i)=inputs(2);
end
study_2=toc
%plotting graphs
figure(3)
subplot(2,1,1)
surfc(x,y,-s)
shading interp
xlabel('x values')
ylabel('y values')
title('bounded values')
hold on
plot3(xopv,yopv,-fval,'marker','*','markersize',4,'markerfacecolor','r')
subplot(2,1,2)
plot(-fval)
xlabel('no of iterations')
ylabel('maximum of function value')
hold on
%study 3- with bounded limits and increased population
options=optimoptions(@ga)
options=optimoptions(@ga,'Populationsize',500)
%segregating data
tic
for i =1:num_cases
[inputs,fval(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopv(i)=inputs(1);
yopv(i)=inputs(2);
end
study_3=toc
%plotting graphs
figure(4)
subplot(2,1,1)
surfc(x,y,-s)
shading interp
xlabel('x values')
ylabel('y values')
title('bounded values')
hold on
plot3(xopv,yopv,-fval,'marker','*','markersize',4,'markerfacecolor','r')
subplot(2,1,2)
plot(-fval)
xlabel('no of iterations')
ylabel('maximum of function value')
hold on
code for a function used
function [s]= stalagmite(input_vector)
G=input_vector(1);
H=input_vector(2);
f1x= (sin((5.1*pi*G)+0.5))^6;
f1y= (sin((5.1*pi*H)+0.5))^6;
f2x= exp((-4)*log(2)*((G-0.0667)^2)/0.64);
f2y= exp((-4)*log(2)*((H-0.0667)^2)/0.64);
[s]= -(f1x*f1y*f2x*f2y);
figure 1
figure 2
figure 3
figure 4