Challenge

Week 4 - Genetic Algorithm

Aim :

To use a genetic algorithm to find the maximum of a function.

Description :

A genetic algorithm is an inbuilt Matlab function that mimics natural selection to find the results.

Process :

1. The input space is defined.

2. The input space is fed into the defined stalagmite function.

3. Then the genetic algorithm is run multiple times to get the correct output.

4. Further the results are refined by carefully defining lower and upper bounds of search space.

5. Normally the genetic algorithm is used to find the minimum value in order to find the maximum the function is inverted.

Code :

Main code :

%inputs
clear all;
close all;
clc;

%Defining Search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

%2d workspace
[k , l]=meshgrid(x,y);

%caluclating stalagmite function
for i= 1:length(k)
for j= 1:length(l)
in_put(1)=k(i,j);
in_put(2)=l(i,j);
f(i,j)=sta_lagmite(in_put);%calling the function
end
end
surfc(k,l,f);%plotting the function


shading interp;
%caluclating the result
for i=1:100
[a(:,i),b(i)]=ga(@sta_lagmite,2,[],[],[],[],[0 0],[0.1 0.1]);
hold on;
plot3(a(1,i),a(2,i),b(i),'marker','o','markerfacecolor','r');
end

Function :

function res=sta_lagmite(xx)
p=(sin(5.1*pi*xx(1)+0.5)).^6;
q=exp((-4*log(2)*(-0.0667+xx(1)).^2/0.64));
r=(sin(5.1*pi*xx(2)+0.5)).^6;
s=exp((-4*log(2)*(-0.0667+xx(2)).^2/0.64));
res=-p*q*r*s;

end

Plots:



Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.