genetic algorithm
Genetic Algorithm:
genetic algorithm is a method in MATLAB that is used to carry out optimisation for problems which are highly non-linear and non-differentiable. it is based on natural selection process, similar to biological evolution.
the algorithm begins by selecting random initial population and determines its fitness value. using this information, it produces the next generation by converting the initial dataset as parents. this process continues in an iteration fter which an optimum solution is attained. it stops when the given conditions are satisfied.
Program to optimise stalagmite function:
%optimising stalagmite function
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 100
f = stalgmite(x,y);
option = optimoptions(\'ga\');
options = optimoptions(@ga,\'populationsize\',350);
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stag,2,[],[],[],[],[0;0],[1;1],[],[],options);
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(\'bounded inputs wth modified population size\')
subplot(2,1,2)
plot(fopt)
xlabel(\'iterations\')
ylabel(\'function maximum\')
-------------------------------------------------------------------------------------
%creating stalagmite
function[f] = stalgmite(x,y)
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating a 2Dimensional 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) = stag(input_vector);
end
end
surfc(xx,yy,f)
shading interp
[x,fval] = ga(@stag,2)
globalmaxima=max(max(f));
for i=1:length(xx)
for j=1:length(yy)
if globalmaxima==f(i,j)
xval=xx(i,j);
yval=yy(i,j);
end
end
end
end
---------------------------------------------------------------------------------------
%stalagmite function
function[f] = stag(input_vector)
x = input_vector(1)
y = input_vector(2)
input_vectorx1 = (sin(5.1*pi*x+0.5)).^6;
input_vectory1 = (sin(5.1*pi*y+0.5)).^6;
input_vectorx2 = exp(-4*log(2)*((x-0.0667).^2/0.64));
input_vectory2 = exp(-4*log(2)*((y-0.0667).^2)/0.64);
% function to evaluate maximum value:
f = -(input_vectorx1*input_vectory1*input_vectorx2*input_vectory2);
end
Maximum value of stalagmite function for 100 iterations: