Challenge

Week 4.1 - Genetic Algorithm

Aim: (Week-4.1 Genetic Algorithm)

To code and optimize the Stalagmite function by using Genetic Algorithm ‘ga’ with the help of MatLab.

EQUATIONS/FORMULAE:

For the Stalagmite function, we use

`f(x,y)=f_(1,x)f_(2,x)f_(1,y)f_(2,y)`

where;

`f_(1,x)=[sin(5.1pix+0.5)]^6`

`f_(1,y)=[sin(5.1piy+0.5)]^6`

`f_(2,x)=exp[-4ln(2)((x-0.0667)^2/0.64)]`

`f_(2,y)=exp[-4ln(2)((y-0.0667)^2/0.64)]`

OBJECTIVES:

  • Write a code in MatLab to optimize the Stalagmite function and find the global maxima of the function.
  • Clearly explain the concept of Genetic Algorithm and syntax in MatLab.
  • Plot graphs of 3 studies for F.maximum vs Iteration.

BODY OF THE CONTENT:

GENETIC ALGORITHM

What is ga?

The word ‘ga’ means Genetic Algorithm, which is used to find the optimum solution to any problem.it is inspired by Charles Darwin’s Theory of evolution. This algorithm is meant to produce the offspring by the process of natural selection from the fittest individual to the next generation.

This process is also used in the optimization problem, in research, and in machine learning…

The genetic algorithm (ga) is a search-based optimization technique based on the principle of genetic and natural selection.

THE FLOW DIAGRAM OF THE GENETIC ALGORITHM

ga syntax in MatLab is given below;

[x, fval] = ga(fun, nvars, A, B, Aeq, Beq, lb, ub, nonlcon, intcon, option)

Where;

[x, fval] = for any previous input arguments, also returns fval, the value of the fitness function at x.

ga = is a calling function.

fun = function that we going to use in ‘ga’.

Nvars = number of variables that we used in the function.

A.B = subjected to the linear inequalities.

Aeq,Beq = subjected to the linear equalities

lb,ub = defines a set of lower and upper bounds on the design variables.

nonlcon,options = nonlinear constraints (Set nonlcon=[] if no nonlinear constraints exist Create options using optimoption.)

intcon = intcon take integer values.

[x,fval,exitflag,output] = ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.

[x,fval,exitflag,output,population,scores] = ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.

reference link:https://in.mathworks.com/help/gads/ga.html

Stopping criteria for 'ga';

  • When there has been no improvement in the population for x-number of generations.
  • When we reach an absolute number of generations.
  • When the objective function value has reached a certain predefined value.

Because of the random selection of 'ga', it's better to do with 50 iterations for a better result. Here we going to optimize the stalagmite function in a total of three cases that actually help us to get the closest optimum value.

First case:

In the very first case, we are going to do 'ga' for 50 iterations to get the global maxima with help of the stalagmite function. the result will be given below.

Second case:

In the second case, we are going to do what we did in the first case, but within the area limit that we want so we get much better results than before.

Third case:

To improve our result, we increase the population of our data to get a precise result so that we get the best result than the above two cases.

in this way, we are going to optimize the stalagmite function by doing it for 50 iterations of each case in 'ga' process.

The code for optimization of the stalagmite function are given below;

CODE:

MAIN CODE

%optimization of the stalagmite function
clear all
close all
clc

%search space limites
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%crating 2d array
[xx yy] = meshgrid(x,y);

%atalagmite 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
%plotting stalagmite funcrion figure(1)
figure(1)
surfc(xx,yy,f)
shading interp
title('Stalagmite function')
xlabel('x-axis')
ylabel('y-axis')

%itrate ga for 5o times
nu_of_it = 50;
tic
for i = 1:nu_of_it
    [at0,g0(i)] = ga(@stalagmite,2);
    xopt0(i) = at0(1);
    yopt0(i) = at0(2);
end
time_taken_for_g0 = toc;
%plotting figure 2
figure(2)
subplot(2,1,1)
surfc(xx,yy,f)
title('Genetic algorithm')
xlabel('x-axis')
ylabel('y-axis')
hold on
shading interp
plot3(xopt0,yopt0,g0,'Marker','o','MarkerSize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(g0)
xlabel('Iterations')
ylabel('Function minimum')

%code ga for 50 times within limited area
u_of_it = 50;
tic
for i = 1:nu_of_it
    [at,g1(i)] = ga(@stalagmite,2,[],[],[],[],[0 0],[0.6 0.6]);
    xopt1(i) = at(1);
    yopt1(i) = at(2);
end
time_taken_for_g1 = toc;
%plotting figure 3
figure(3)
subplot(2,1,1)
surfc(xx,yy,f)
title('Genetic algorithm with bounded inputs')
xlabel('x-axis')
ylabel('y-axis')
hold on
shading interp

plot3(xopt1,yopt1,g1,'Marker','o','MarkerSize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(g1)
xlabel('Iterations')
ylabel('Function minimum')

%code ga for 50 time within limtied area and more population

option = optimoptions('ga');
option = optimoptions(option,'PopulationSize',200)
u_of_it = 50;
tic
for i = 1:nu_of_it
    [at,g2(i)] = ga(@stalagmite,2,[],[],[],[],[0 0],[0.6 0.6],[],[],option);
    xopt2(i) = at(1);
    yopt2(i) = at(2);
end
time_taken_for_g2 = toc;
%plotting figure 4
figure(4)
subplot(2,1,1)
surfc(xx,yy,f)
title('Genetic algorithm with bounded inputs and more population')
xlabel('x-axis')
ylabel('y-axis')
hold on
shading interp

plot3(xopt2,yopt2,g2,'Marker','o','MarkerSize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(g2)
xlabel('Iterations')
ylabel('Function minimum')

%print time intervale of each case
time_taken_for_g0
time_taken_for_g1
time_taken_for_g2

FUNCTION CODE

function [result] = stalagmite(inputs)
    f1x = (sin(5.1*3.14*(inputs(1,1))+0.5))^6;
    f1y = (sin(5.1*3.14*(inputs(1,2))+0.5))^6;
    f2x = exp(-4*log(2)*(((inputs(1,1))-0.0667)^2)/0.62);
    f2y = exp(-4*log(2)*(((inputs(1,2))-0.0667)^2)/0.62);
    result = -(f1x*f1y*f2x*f2y);
end

EXPLANATION OF CODE:

MAIN CODE

  • From code lines 1 to 4 – clearing the previous data.
  • From code lines 6 to 8 – set the limits of x and y coordinates for the stalagmite function.
  • From code lines 10 to 11 – Creating a 2d array of x and y.
  • From code lines 13 to 20 – For each value of the 2d array, we solve the stalagmite function.
  • From code lines 21 to 27 – plotting the surface plot for xx, yy, and f.
  • From code lines 29 to 37 – Iterate the ‘ga’ in MatLab with stalagmite function 50 times and the results were stored as xopt0, yopt0, and g0(global maxima) in array order. And calculate the time duration for this process.
  • From code lines 38 to 51 – plotting the global maxima for the 50 iterations with stalagmite surface plot.
  • From code lines 53 to 61 - – Iterate the ‘ga’ for 50 times with limited area(required area limit) and the results were stored as xopt1, yopt2, and g1(global maxima) in array order. And calculate the time duration for this process.
  • From code lines 62 to 76 - plotting the global maxima for the 50 iterations of the above case with stalagmite surface plot.
  • From code lines 78 to 89 – again we do ‘ga’ for 50 iterations of required area limits but increased the population.
  • From code lines 90 to 104 – plotting the above case as we did for the previous 2 cases.
  • From code lines 106 to 109 – code to show the time taken for each case

FUNCTION CODE

  • Line 1 – Defining the function with input parameters.
  • Line2 – calculating f1x for stalagmite function.
  • Line3 – calculating f1y for stalagmite function.
  • Line4 – calculating f2x for stalagmite function.
  • Line5 – calculating f2y for stalagmite function.
  • Line6 – calculating the stalagmite function with the above variables.
  • Line7 – end the function program.

RESULTS:

Time duration for each 'ga' case;

ERRORS:

I got this error because of forgetting to put square brackets on the left side while defining the function. It’s just a careless mistake.

This error occurred because initially, I thought that input_vector is a 2*2 matrix. After I understand that input_vector is 1*2, I solved this error.

I got this error when I check whether the markers commands are working in surfc() command or not.

This error occurred that I forgot to out 3 after the plot command for 3d plot(x,y,z)

CONCLUSION:

We successfully coded to find the global maxima for the stalagmite function and plotted the graphs for .F.Maxima(global maxima) vs Iteration, the global maxima of the stalagmite function lies somewhere between -0.999 to -1.

REFERENCE:

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.