Challenge

Week 4 - Genetic Algorithm

Aim: Optimization using genetic algorithm and to determine the global maximum function using MATLAB.

Theory: A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.

Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning.

Optimization is the process of making something better. In any process, we have a set of inputs and a set of outputs as shown in the following figure.

Optimization refers to finding the values of inputs in such a way that we get the “best” output values. The definition of “best” varies from problem to problem, but in mathematical terms, it refers to maximizing or minimizing one or more objective functions, by varying the input parameters.

Uses of Genetic Algorithm:

Genetic algorithm solves smooth or non-smooth optimization problems with any types of constraints, including integer constraints. It is a stochastic, population-based algorithm that searches randomly by mutation and crossover among population members.

Stalagmite Algorithm:

The algorithm randomly selects members from the current population which become the parents for the next iteration. The parent members of the function undergo mutations and crossovers to generate the child members of the functions.

Equation:

Function Equation used in the problem is

`f(x,y)=f1*f2*f3*f4`

`f1=(sin⁡(5.1*π*x+0.5) )^2`

`f2=(sin⁡(5.1*π*y+0.5) )^2`

`f3=exp⁡((-4 log⁡(2)*((x-0.0667)^2/0.64))`

`f4=exp⁡((-4 log⁡(2)*((y-0.0667)^2/0.64))`

We will use ‘linspace’ command to vary range of X and Y from 0 to 0.6 using 150 divisions.

Next, We will include the function in our Stalagmite Function and we will solve it. Then we will plot the graph which will include of three types of graph.

  1. Statistical Behaviour
  2. Plotting the Bounded graph
  3. Plotting the Population Bounded graph

In Statistical Behaviour we will use the for loop to run the respective number of points using a loop.

In the Bounded graph we will bound them in the for loop using the upper and lower bounded function as [coor,fmax(i)] = ga(@func1,2,[],[],[],[],[0;0],[0.6;0.6]);

In the Population Bounded graph we will use some specific population and will again bound them in for loop using the upper and lower bounded function as [coor,fmax(i)] = ga(@func1,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);

Using this we would be able to find the global maxima.

MATLAB Code:

%Stalagmite Function.
function [f]= func1(input)
x=input(1);
y=input(2);
f1= (sin(5.1*pi*x+0.5))^6;
f2=(sin(5.1*pi*y+0.5))^6;
f3=exp((-4*log(2))*((x-0.0667)^2/0.64));
f4=exp((-4*log(2))*((y-0.0667)^2/0.64));
f =-(f1.*f2.*f3.*f4);
end
clear all
close all
clc

%Defining the workspace
x=linspace(0,0.6,150); %Range of X
y=linspace(0,0.6,150); %Range of Y

%Evaluating the Stalagmite Function
%Iterating 50 times
n=50
for i=1:length(x)
for j=1:length(y)
coor(1)=x(i);
coor(2)=y(j);
f(i,j)=func1(coor);
end
end



tic
for i=1:n
[coor,fmax(i)] = ga(@func1,2);
xopt(i)=coor(1);
yopt(i)=coor(2);
end
time1=toc

%Plotting

%Plotting the Unbounded graph
figure(1)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fmax,'marker','o','markerfacecolor','r','markersize',2)
title('Unbounded')
subplot(2,1,2)
plot(-fmax)
xlabel('Iteration')
ylabel('fmax')
colorbar()



tic
for i=1:n
[coor,fmax(i)] = ga(@func1,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i)=coor(1);
yopt(i)=coor(2);
end
time2=toc

%Plotting the Bounded graph
figure(2)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fmax,'marker','o','markerfacecolor','r','markersize',2)
title('Bounded')
subplot(2,1,2)
plot(-fmax)
xlabel('Iteration')
ylabel('fmax')
colorbar()


%Options
options = optimoptions('ga');
options = optimoptions('ga','Populationsize',250)


tic
for i=1:n
[coor,fmax(i)] = ga(@func1,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i)=coor(1);
yopt(i)=coor(2);
end
time3=toc

%Plotting the Population bound graph
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fmax,'marker','o','markerfacecolor','r','markersize',2)
title('Population Bound')
subplot(2,1,2)
plot(-fmax)
xlabel('Iteration')
ylabel('fmax')
colorbar()

Results:

The programme ran for fifty iterations and after getting the plot, the global maxima point are

x = 0.0668

y = 0.0668

1.Unbounded Graph

2.Bounded Graph

3.Population Bound Graph

Errors or Warnings:

Online references:

https://towardsdatascience.com/introduction-to-genetic-algorithms-including-example-code-e396e98d8bf3

https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_introduction.htm

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.