Challenge

Week 4.1 - Genetic Algorithm

AIM: To optimize the stalagmite function and find the global maxima of the function by using MATLAB.


THEORY :
Genetic Algorithm :
Genetic Algorithm (GA) is a branch of the field of study called evolstraintconcontraint tolerance by increasing the population size
Some advantages of genetic algorithms in this method are the following:
1) They usually perform better than traditional feature selection techniques.
2) Genetic algorithms can manage data sets with many features.
3) They don't need specific knowledge about the problem under study.
4) These algorithms can be easily parallelized in computer clusters.
Working of Genetic Algorithm:
The following outline summarizes how the genetic algorithm works:
1) The algorithm begins by creating a random initial population. The algorithm then creates a sequence of new populations.
2) At each step, the algorithm uses the individuals in the current generation to create the next population.
To create the new population, the algorithm performs the following steps:
a) Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.
b) Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation
values.
c) Selects members, called parents, based on their expectations.
d) Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed on to
the next population.
e) Produces children from the parents. Children are produced either by making random changes to a single parent—mutation or by
combining the vector entries of a pair of parents—crossover.
e) Replaces the current population with the children to form the next generation. The algorithm stops when one of the stopping criteria
is met.

CODE :


Function code :
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(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);
[f] = -(f1_x.*f1_y.*f2_x.*f2_y);
end


Main code :

clear all
close all
clc
%Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx,yy] = meshgrid(x,y);
num_cases = 80 ;
%calling 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) = stalagmite(input_vector);
end
end
%Study 1 - Statistical behavior
tic
for i=1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
title('Unbound inputs')
xlabel('X values')
ylabel('Y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')
%Study 2 - Statistical behavior with upper and lower bounds
tic
for i=1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
title('Bounded inputs')
xlabel('X values')
ylabel('Y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')
%Study 3 - Increasing GA iterations
options = optimoptions('ga')
options = optimoptions(options,'populationSize',200)
tic
for i=1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('X values')
ylabel('Y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')


STEPS :


  1. The code starts with defining x and y as inputs using linspace

  2. A function known as Stalagmite_func is defined as how in the 2nd stage of codes.

  3. 2 for loops are incorporated in the main code so that all the combinations of x and y values are taken in the stalagmite_func
    function

  4. In the 1st study, the GA function runs for 80 iterations. Plots of the stalagmites and maximum values generated in each iteration is
    shown.

  5. In the 2nd study, the GA function is incorporated with lower and upper bounds and runs for 80 iterations. Plots of the stalagmites and
    maximum values generated in each iteration are shown.

  6. In the 3rd study, the GA function is incorporated with lower and upper bounds along with the population size of 200. Plots of the
    stalagmites and maximum values generated in each iteration are shown.

OUTPUT :


STUDY 1

In the first study, there are no lower and upper limits hence the GA does not have the search space between 0 and 1. Hence each time the
program is executed different optimum values are obtained as shown in plot 2.


STUDY 2

In the 2nd study, lower and upper limits are included in the GA. Hence most of the maximum values obtained in each iteration vary
between (-1) and (-0.7). This method narrows down the maximum values to 4 points as shown in plot 1.


STUDY 3

In the 3rd study, the global maximum obtained is accurate and is narrowed down to a value of -0.999999 as shown in the graphs.


CONCLUSION :


The optimized stalagmite function and global maxima of function are found successfully using MATLAB.

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.