Challenge

Global Maxima of Stalagmite function using Genetic Algorithm

Genetic Algorithm:

Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is inspired by Charles Darwin's theory of natural evolution. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve.

The genetic algorithm converges to solution by following below steps for creation of childrens for next generation:

  1. Elite or selection are the individuals in the current generation with the best fitness values. These individuals automatically survive to the next generation.
  2. Crossover are created by combining the vectors of a pair of parents.
  3. Mutationchildren are created by introducing random changes, or mutations, to a single parent.

Explaination of ‘ga’ syntax:

x = ga(fun,nvars,A,b,Aeq,Beq,lb,ub,nonlcon,IntCon,options)

fun = function

nvars = number of variables

A,b = Linear inequality constraints

Aeq, Beq = Linear inequality constraints

lb,ub = lower & upper bounds for variables

nonlcon = Nonlinear constraint function

IntCon = Integer variables, specified as a vector of positive integers taking values from 1 to nvars.

Options = Create options using optimoptions to set additional constrains like population size etc.

Output Images:

Case 1: With No bounds

Case 2: With Bounds

Case 3: With bounds and population=100

Case 4: With bounds and population=150

X & Y coordinates of global maxima:

Maxima value = 1

MATLAB code:

Stalagmite function script:

%Stagmite function
function[f]=stalagmite(input_vector)

f1x= (sin(5.1*pi.*(input_vector(1))+0.5)).^6;
f1y= (sin(5.1*pi.*(input_vector(2))+0.5)).^6;

f2x= exp(-4*log(2).*(((input_vector(1)-0.0667).^2)/0.64));
f2y= exp(-4*log(2).*(((input_vector(2)-0.0667).^2)/0.64));

%Since ga function gives minima function sign is reveresed
f=-1*(f1x.*f2x.*f1y.*f2y);

end

Optimization script:

clear all
close all
clc

%Inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%Set number of iterations to 50
iterations=50;

%Convert to 2D array
[xx, yy]=meshgrid(x,y);

%For loop to calculate stalagmite function f
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

%Case 1 - With no bounds
%For loop to determine global minima for each iterations
for i=1:iterations
    
    [opt_vector1,fval1(i)]=ga(@stalagmite,2);
    
end

%Plot for case 1 with subplots
figure(1)
subplot(2,1,1)
surfc(xx,yy,f)
shading interp
xlabel('x')
ylabel('y')
title('Case 1 - With no bounds')
hold on
plot3(opt_vector1(1),opt_vector1(2),fval1,'marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2)
plot(fval1);
axis([0 50 -1.2 0])
xlabel('No. of iterations')
ylabel('Global maximum')

%Case 2 - With bounds from 0 to 0.6
%For loop to determine global minima for each iterations
for i=1:iterations
    
[opt_vector2,fval2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
end

%Plot for case 2 with subplots
figure(2)
subplot(2,1,1)
surfc(xx,yy,f)
shading interp
xlabel('x')
ylabel('y')
title('Case 2 - With bounds')
hold on
plot3(opt_vector2(1),opt_vector2(2),fval2,'marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2)
plot(fval2);
axis([0 50 -1.2 0])
xlabel('No. of iterations')
ylabel('Global maximum')

%Case 3 - With bounds from 0 to 0.6 and population size set to 100

options=optimoptions('ga','populationsize',100);

%For loop to determine global minima for each iterations
for i=1:iterations
    
[opt_vector3,fval3(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
end

%Plot for case 3 with subplots
figure(3)
subplot(2,1,1)
surfc(xx,yy,f)
shading interp
xlabel('x')
ylabel('y')
title('Case 3 - With bounds and Population size=100')
hold on
plot3(opt_vector3(1),opt_vector3(2),fval3,'marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2)
plot(fval3);
axis([0 50 -1.2 0])
xlabel('No. of iterations')
ylabel('Global maximum')

%Case 3 - With bounds from 0 to 0.6 and population size set to 150

options=optimoptions('ga','populationsize',150);

%For loop to determine global minima for each iterations
for i=1:iterations
    
[opt_vector4,fval4(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
end

%Plot for case 3 with subplots
figure(4)
subplot(2,1,1)
surfc(xx,yy,f)
shading interp
xlabel('x')
ylabel('y')
title('Case 3 - With bounds and Population size=150')
hold on
plot3(opt_vector4(1),opt_vector4(2),fval4,'marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2)
plot(fval4);
axis([0 50 -1.2 0])
xlabel('No. of iterations')
ylabel('Global maximum')

%To display x and y values of global maxima
opt_vector4
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.