Challenge

Week 5 - Genetic Algorithm

Aim:

To write a code in MATLAB to optimise the stalagmite function and find gobal maxima of the function.

Theory:

Genetic algoritham (GA) is a search based optimization technique based on the principle of genetic and natural selection. It is frequrntly used to find optimal or near optimal solution to difficult problem which otherwise would take a lifetime to solve. It is frequently used research and in machine learning.

Optimization referes to finding the value of inputs in such a way that we get the best output values. The defination of best varies from problem to problem, but in maximizing or minimizing one or more objective function by varying the input parameters.

The set of all possible solution or values which the inputs can take up the seach space. in this seach space, lies a optimal solution. The aim of optimization to find that point or set of point in the search space.

Stalagmite function:

1. A stalagmite is an upward-growing mount of miniral deposite that have precipitated from water dripping onto the floor of a curve. Most stalagmites have rounded of flattened tips.

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

3. For this in MATLAB we are using a method called 'Optimization'. It is a programming which does a selectin of the best element with regard to some criterion from some set of population.

4. Optimization is done by systematically choosing input values from within an allowed set and computing the results of the function.

5. In MATLAb to perform optimization we use genetic algoritham method which is a inbuilt function in MTLAB to find the global maxima of that given stalagmite function.

Code

clear all
close all
clc

% Defining our search space

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

% Creating a 2-dimensional 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)

shading interp

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

Case-1

clear all
close all
clc

% Defining our search space

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50; 


% Creating a 2-dimensional 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

% Study1 : Statistical behaviour

tic

for i = 1:n
    [inputs, fout(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

xlabel('X-axis')
ylabel('Y-asis')
title('Statistic Bahaviour with Unbound inputs')
plot3(xopt,yopt,-fout,'marker','o', 'markersize', 5, 'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fout)
xlabel('No. Of Iterations')
ylabel('Function Maximum')

Case-2

clear all
close all
clc

% Defining our search space

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50; 


% Creating a 2-dimensional 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

% Study2 : Statistical behaviour with upper and lower bounds

tic

for i = 1:n
    [inputs, fout(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[1;1]);
    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

xlabel('X-axis')
ylabel('Y-asis')
title('Statistic Bahaviour with Unbound inputs')
plot3(xopt,yopt,-fout,'marker','o', 'markersize', 5, 'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fout)
xlabel('No. Of Iterations')
ylabel('Function Maximum')

Case-3

clear all
close all
clc

% Defining our search space

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50; 


% Creating a 2-dimensional 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

% Study3 : Statistical behaviour with upper and lower bounds

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300);


tic

for i = 1:n
    [inputs, fout(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

study3_time = toc

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp

xlabel('X-axis')
ylabel('Y-asis')
title('Statistic Bahaviour with Unbound inputs')
plot3(xopt,yopt,-fout,'marker','o', 'markersize', 5, 'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fout)
xlabel('No. Of Iterations')
ylabel('Function Maximum')
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.