Challenge

Week 4.1 - Genetic Algorithm

Aim :

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

Objective:

  • explain ga and ga syntax
  • plot graph for all studies and fmax Vs no.iteration.Titles and and axes labels are must.

Theory :

what is optimization

optimization is decision making process ,it helps to find the best results.under the prioritized or criteria and constraints.it is the procedure which is executed iteratively by comparing various solutions till an optimum or a satisfactory solution is found.

what is genetic alogorithm:

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.

we need to choose an optimization algorithm to perform the optimization process Genetic algorithm is the one of the optimization process.

how does GA works :

genetic algorithm optimization starts with population initialization,population is the number of the sample data points taken to perform the genetic algorithm operation.

these sample data points undergoes the following three steps:

selection :

in this steps the parents are slected from sample data points. main objective of the selection process is to select the highly fit individuals.

cross-over :

its is process of taking two parents solutions and producing the child from them.

mutation :mutation is carried to maintain the genetic diversity from one generation of a population of chromosomes to the next.if mutation is not performed,the chromosomes will become too similar to each other.

fitness function :

fitness function takes the solution as input and produces the suitability of the solution as the output.this evaluvate how close the solution is to the optimal solution of desired problem.

here, in we gona perform the genetic optization over or for stalagmite function,to find its minima

stalagmite is an upward growing mound of mineral deposits that have precipitated from water dripping onto the floor of cave.

A stalagmite function generally refers to the function with which we are generating values over a stalagmite kind of surface for finding out the local and global maxima and minima of that particular given function.

syntax for GA :

ga - ga function are used to find minimum of function.

x = ga(fun,nvars,A,b,Aeq,beq,lb,nonlcon,options)

fun - function to be optimized

nvars - the dimensions (number of design variable)of the functions

A,B - linear inequalities

Aeq,Beq - linear equalities

lb,ub - lower and upper bound on the design variables

nonlcon-constraints

options - options are used to to change the default parameters of ga(replaces default parameters values by specified in options)

Matlab Code :

stalagmite function

function so = stalagmite(input_vector)
x =input_vector(1);
y = input_vector(2);
g1x=(sin((5.1*pi*x) +0.5)).^6;
g2x=exp(-4*log(2)*((x-0.0667).^2/(0.64)));
g1y=(sin((5.1*pi*y) +0.5)).^6;
g2y=exp(-4*log(2)*((y-0.0667).^2/(0.64)));;
so =-(g1x*g2x*g1y*g2y);
end

here , in above code we define the stalagmite function which take two input as input vector(1) , input vector(2) store as x and y variable , give stalagmite kind surfaces to find local maxima and minima.

ga always find minimum of the function,to find the maxima introduced the -ve sign

optimization code : study 01 unbounded conditions

clear all
close all
clc

% defining search space 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces =50;
%creating 2d dimensional mesh
[xx yy]=meshgrid(x,y);
%evaluating the sta;agmite 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

%surf(xx,yy,f)

%shading interp
%xlabel('xaxis')
%ylabel('yaxis')
%[x,fval]= ga(@stalagmite,2);


tic 
for i = 1:num_spaces
    [results,fout(i)]=ga(@stalagmite,2);
    xout(i)=results(1);
    yout(i)=results(2);
end
study_time = toc
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on 
plot3(xout,yout,-fout,'marker','o')
subplot(2,1,2)
plot(-fout)
xlabel('ireration')
ylabel('function maximum')
grid on 

in that first we define the search space as x and y.

from theta created 2 dimensional mesh grid.

then evaluate the stalagmite function as f.

then using for loop for 50 iteration we get the random results for search input,by calling ga function without any boundry conditions.

using surf function we ploted 3 dimensional surface and -ve sign used to invert the graph.

shading interp gives smooth surfaces rather than grid lines.

using the plot3 function we ploted results over the surfaces.

without any boundry condition it looks too messy, so introduces subplot clear graph of fmax over the 50 iteartions.

above figure are results after running study 01

study 02 : boundry conditions (upper bound and lower bound)

clear all
close all
clc

% defining search space 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces =50;
%creating 2d dimensional mesh
[xx yy]=meshgrid(x,y);
%evaluating the sta;agmite 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

%surf(xx,yy,f)

%shading interp
%xlabel('xaxis')
%ylabel('yaxis')
%[x,fval]= ga(@stalagmite,2);


tic 
for i = 1:num_spaces
    [results,fout(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.3,0.3]);
    xout(i)=results(1);
    yout(i)=results(2);
end
study_time = toc
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on 
plot3(xout,yout,-fout,'marker','o')
subplot(2,1,2)
plot(-fout)
xlabel('ireration')
ylabel('function maximum')
grid on 

code explaination :

every line of code is same ,just we introduced the another arguments of ga function.

upper bound and lower bound.

due to that the ga algorithm search only within the boundry conditions.

we get improved results but we stuck at local maxima rather than finding global maxima.

after running study 02 we get above graph.

study 03 :

clear all
close all
clc

% defining search space 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces =50;
%creating 2d dimensional mesh
[xx yy]=meshgrid(x,y);
%evaluating the sta;agmite 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

%surf(xx,yy,f)

%shading interp
%xlabel('xaxis')
%ylabel('yaxis')
%[x,fval]= ga(@stalagmite,2);

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);
tic 
for i = 1:num_spaces
    [results,fout(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.3;0.3],[],[],options);
    xout(i)=results(1);
    yout(i)=results(2);
end
study_time = toc
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on 
plot3(xout,yout,-fout,'marker','o')
subplot(2,1,2)
plot(-fout)
xlabel('ireration')
ylabel('function maximum')
grid on 

code explainations :

all code line are as above sudies,also it search in the boundry condition only.

due we not more improved results,we increase the population size by 170.

in this case results are not stuck at local maxima,gives optimized results.

code takes more time compare to above studies.

after runing study 03 we get more optimized and improved results .

conclusion :

using genetic algorithem for stalagmite kind function we get so much optimized results.

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.