Challenge

Week 4 - Genetic Algorithm

GENETIC ALGORITHM IN MATLAB

AIM:

To write MATLAB code to find Global maxima for Stalagmite.

INTRODUCTION:

Genetic algorithm (GA) is one of an optimization technic which developed based on “Natural Evaluation” written by Charles Darwin. In MATLAB, GA is used to find the minimum value for a function. Using this function global maxima will be found for stalagmite function.

GENETIC ALGORITHM:

GA is used for creating the fittest population development. GA has 5 phases to evaluate the system,

  1. Creation of Population.
  2. Find Fitness value.
  3. Selection
  4. Cross-over.
  5. Mutation

Creation of Population: In this process sequence, values have been found by a random selection process is known as population.

Fitness Value: For the found population Fitness value found by defined parameters. It basically creates a probability of fitness for each value. Using this fitness value GA decides the method of operation to be done so that a new generation child will be created.

Selection: In this selection process GA select values has a high probability of fitness which can sustain in the next generation.

Cross-Over: Some values in a population selected as parents that cross over one to another to produce a new generation child.

Mutation: In this process, selected values will be mutated in order to produce new generation values.

This process produces a new generation for each iteration which produces optimum values.

GENETIC ALGORITHM IN MatLab:

Matlab used to finds minimum value for both constrained and unconstrained optimization functions using genetic algorithm like discussed above. Here minimum value found by creating a new population for each iteration to find the optimum value. At each iteration, GA selects values randomly from the current population and uses them, parents, to produce children for next generation. These children then selected as a parent and then process repeated till find the solution of the function

STALAGMITE:

Stalagmite is a deposit of minerals by water droplets which forms a mount shaped cones inside caves. Basically this stalagmite contains lava, minerals, mud, peat, pitch, sand, sinter, etc. These stalagmites have many peaks. Using MatLab (GA), maximum values for a peak point is found. But GA will calculate only minimum points so that function of a stalagmite is inversed. So that GA calculates minimum point for the function. After calculation, the function then inversed to plot the actual result.

Stalagmite function formulas
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

PROGRAM

clear all
close all
clc
%Input values are created for x and y
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
Min=0;                    %Initial value are determined
n=20;                     %No of itreation for Global algorithm
[X Y]=meshgrid(x,y);      %Formation 2D array from 1D array

for i=1:length(X)         %Incrementel loop for lenth of X(2D) array
    for j=1:length(Y)     %Incrementel loop for lenth of Y(2D) array
        Input(1)=X(i,j);  %Takes correspondent value of X from grid
        Input(2)=Y(i,j);  %Takes correspondent value of Y from grid 
        Z(i,j)=stlagfunction(Input); %Funtion stalagmite
    end
end

%STUDY-1 Ga function (Simple Command)
tic                       %Start stop watc timer       
for j=1:n
[X_Y Minimum_value(j)]=ga(@stlagfunction,2)   %genetic algorithm
X1(j)=X_Y(1);                                 %Minimum values X position
Y1(j)=X_Y(2);                                 %Minimum values Y position
Maximum(j)=-Minimum_value(j);
if Min>=Minimum_value(j)                      %If Min_value is less than or equal to current minimum value found by ga then is allow in to loop
    Min=Minimum_value(j);                     %Store that particular value as Minimum value
    Maximum_value(1)=-Min;                    %Find maximum value
    k=j;                                      %store that position
end
end
Time_study_1=toc;                                    %Stop stopwatch
figure(1)                                     %Figure 1
subplot(2,1,1)                                %Sub plot to represent both surface and Itration plot
surf(X,Y,-Z)                                  %3D graph plotted.'-' used to compensate '-' in function
title('Stalagmite - Study 1')                           %Titel stalagmite
xlabel('X Axis')                              %Label x axis
ylabel('Y Axis')                              %Label y axis
shading interp                                % switch off grid line in surface
hold on                             
Maximum_point_Coordinates(1,:)=[X1(k) Y1(k)]                %X,Y values of maximum point
plot3(X1,Y1,Maximum,'linewidth',1,'marker','*') %Plot maximum value in 3D space
subplot(2,1,2)                                %Sub plot for Itration Vs Maximum point plot
plot(Maximum)
xlabel('No of Itreation')                              %Label x axis
ylabel('Maximum values')                               %Label y axis
title('Maximum values using simple Genetic Algorithm')
%STUDY-2 GA found between boundaries
tic                       %Start stop watc timer       
for j=1:n
[X_Y Minimum_value(j)]=ga(@stlagfunction,2,[],[],[],[],[0:0],[0.1:0.1])   %genetic algorithm with boundaries 
X1(j)=X_Y(1);                                 %Minimum values X position
Y1(j)=X_Y(2);                                 %Minimum values Y position
Maximum(j)=-Minimum_value(j);
if Min>=Minimum_value(j)                      %If Min_value is less than or equal to current minimum value found by ga then is allow in to loop
    Min=Minimum_value(j);                     %Store that particular value as Minimum value
    Maximum_value(2)=-Min;                    %Find maximum value
    k=j;                                      %store that position
end
end
Time_Study_2=toc;                                    %Stop stopwatch
figure(2)                                     %Figure 1
subplot(2,1,1)                                %Sub plot to represent both surface and Itration plot
surf(X,Y,-Z)                                  %3D graph plotted.'-' used to compensate '-' in function
title('Stalagmite - Study 2')                           %Titel stalagmite
xlabel('X Axis')                              %Label x axis
ylabel('Y Axis')                              %Label y axis
shading interp                                % switch off grid line in surface
hold on                             
Maximum_point_Coordinates(2,:)=[X1(k) Y1(k)]                %X,Y values of maximum point
plot3(X1,Y1,Maximum,'linewidth',1,'marker','*') %Plot maximum value in 3D space
subplot(2,1,2)                                %Sub plot for Itration Vs Maximum point plot
plot(Maximum)
xlabel('No of Itreation')                              %Label x axis
ylabel('Maximum values')                               %Label y axis
title('Genetic Algorithm with Boundaries')
%STUDY-3 Change properties in GA
options=optimoptions('ga','PopulationSize',200)       %Increase property 'Population size' to 100
tic                       %Start stop watc timer       
for j=1:n
[X_Y,Minimum_value(j)]=ga(@stlagfunction,2,[],[],[],[],[0:0],[0.1:0.1],[],options); %Incresed property detonted by option
X1(j)=X_Y(1);                                 %Minimum values X position
Y1(j)=X_Y(2);                                 %Minimum values Y position
Maximum(j)=-Minimum_value(j);
if Min>=Minimum_value(j)                      %If Min_value is less than or equal to current minimum value found by ga then is allow in to loop
    Min=Minimum_value(j);                     %Store that particular value as Minimum value
    Maximum_value(3)=-Min;                    %Find maximum value
    k=j;                                      %store that position
end
end
Time_Study_3=toc;                                    %Stop stopwatch
figure(3)                                     %Figure 1
subplot(2,1,1)                                %Sub plot to represent both surface and Itration plot
surf(X,Y,-Z)                                  %3D graph plotted.'-' used to compensate '-' in function
title('Stalagmite - Study 3')                           %Titel stalagmite
xlabel('X Axis')                              %Label x axis
ylabel('Y Axis')                              %Label y axis
zlabel('Z Axis')
shading interp                                % switch off grid line in surface
hold on                             
Maximum_point_Coordinates(3,:)=[X1(k) Y1(k)]                %X,Y values of maximum point
plot3(X1,Y1,Maximum,'linewidth',1,'marker','*') %Plot maximum value in 3D space
subplot(2,1,2)                                %Sub plot for Itration Vs Maximum point plot
plot(Maximum)
xlabel('No of Itreation')                              %Label x axis
ylabel('Maximum values')                               %Label y axis
title('Genetic Algorithm with Boundaries and increased property')
%results
Maximum_value                         %Maximum value calculated by 3 studies
Maximum_point_Coordinates             %Maximum values co-ordinates calculated by 3 studies
%Time taken for each study
Time_study_1
Time_Study_2
Time_Study_3

FUNCTION:

Stalagmite formula created as a function.

function [f]=stlagfunction(Input)    %output f taken from function
x=Input(1);                          %Input of X stored as x
y=Input(2);                          %Input of Y stored as y

%Stalagmite function
%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];

%Function splits into small parts for easy calculation
X1=(5.1*pi*x)+0.5;
F1_x=(sin(X1))^6;

Y1=(5.1*pi*y)+0.5;
F1_y=(sin(Y1))^6;

X2=((x-0.0667)^2)/0.64;
X21=[-4*log(2)*X2];
F2_x=exp(X21);

Y2=((y-0.0667)^2)/0.64;
Y21=[-4*log(2)*Y2];
F2_y=exp(Y21);
%Final result stored as 'f'
f=-(F1_x*F2_x*F1_y*F2_y)       
end

SYNTAX (STUDIES):

For GA, Matlab has various syntax in order to find minimum values for different functions. On that 3 of them listed below,

STUDY 1:

  • [X_Y Minimum_value]=ga(function, variable);

In this function and no variables are given as inputs. This syntax will output X and Y coordinates(X_Y) of maximum value occurred and Minimum values. For various iteration maximum value shown in figure 1. The values vary from 0.001 to 0.8361 which is not an exact value and not precise.

STUDY 2:

  • [X_Y Minimum_value]=ga(function, variable,[],[],[],[],[lower bound x: ; Lower bound y],[Upper bound x:Upper bound y]);

In regular ga(), values found randomly. Sometimes it will be found local minima instead of global minima. So that boundary values are inputted in which global minima will occur. This makes ga() to find an approximate value of Global maxima.

No. of iteration Vs Maximum values shown in the figure. The variation is between 0.5153 to 1.This values are quite near to global maxima and some of them show global maxima.

STUDY 3:

  • [X_Y Minimum_value]=ga(function, variable,[],[],[],[],[Lower bound],[]Upper bound],options);

In this syntax, properties of regular GA has been changed using optimoptions(). Using this syntax population size value changed from 50 to 200.

The Population size defines no. of the population at each generation. By increasing population size more no of the values selected as a population which increases the probability of finding fitness value.

No. of iteration Vs Maximum values shown in the figure. The variation is 0.85 and 1.This values are quite near to global maxima and some of them show global maxima. The values of local maxima which lie near to global maxima.

RESULTS:

The program executed and result listed below,

Maximum value found by each method,

Maximum_value =

0.8361 1.0000 1.0000

Co-ordinates(X and Y) of each maximum value found by 3 methods:

Maximum_point_Coordinates =

0.2659 0.0664

0.0668 0.0668

0.0668 0.0668

Time is taken for each study:

Time_study_1 =

11.1894

Time_Study_2 =

10.1519

Time_Study_3 =

34.3818

CONCLUSION:

Therefore Global maxima of stalagmite are "1" it occurs at X=0.0668 and Y=0.0668.

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.