Challenge

Week 4.1 - Genetic Algorithm

Aim : To write a program to optimise stalagmite function and to find the global maxima of the function.

Objective : Objective is to build up a mathematical model that can help us generate local minimas and global maximas by using stalagmite function.

*PROGRAM :

clear all
close all
clc

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

% Creating a 2Dimensional mesh
[xx,yy] = meshgrid(x,y);

% Evaluating the 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


surfc(xx,yy,-f)
xlabel('x value')
ylabel('y value')

shading interp


[inputs,fval] = ga(@stalagmite,2)

*Steps:

1.For this problem we are going to be using 'GENETIC ALGORITHM' which was proposed by Charles Darwin in the evolution theory.

2.Here in Matlab it provides the function called 'ga' which helps to do the genetic algorithm more accurately.

3.First we start with clear all;close all;clc .

4.Then by using the linspace command we proceed by creating first the 1 dimensional arrays.

5.By using 1D array we are creating 2dimensional array.

6. Now for each value of x and y we are creating array called as input vector, basically this input vector is a row vector which has two columns.

7.The content for the input vector will be the x and y value.

8.Now, we have a main function called the stalagmite function which takes the input vector and give us the function called 'f'. We have used two for loops for creating the data.

9. Once we write the codes for stalagmite function we will use the surface plot for representing the model.

10.We are using here a command called 'shading interp' which when executing removes the grid lines from the plot figure and showing only the shading thing and makes the transition of the plot more smoother.

11. And finally, we are performing our Genetic Algorithm, whereas 'ga' is a in-built function of matlab, where ga is used to find the minimum value. So here, we are passing the inputs for 'ga' as stalagmite and no. of variables i.e 2 which is our x and y.

.

* Formulas used for creating stalagmite function are:

* Program for stalagmite function:

function z = stalagmite(input)

x = input(1);
y = input(2);

f1x = (sin(5.1*pi*x+0.5))^6;

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

f2x = exp(-4*log(2)*(x-0.0667)^2/0.64);


f2y = exp(-4*log(2)*(y-0.0667)^2/0.64);

z = -(f1x*f2x*f1y*f2y);    

*Description:

1.We are assigning a function variable called 'z' wher we define our stalagmite function which takes input as x and y.

2. As x has 2 inputs simultaneouly y also has 2 inputs which are f1x,f2x,f1y,f2y.

3. We then multiply all the values of x and y to determine the z value.

*PLOT:

*Conclusion:

Stalagmite function is. a mathematical model that actually help us generate local minimas and maximas.

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.