Challenge

Week 4 - Genetic Algorithm

Aim :- To find the global maxima of the function.

Genetic algorithm is under evolutionary algorithms with principle based on the mechanics of nautral genetics.The objective is natural renetics is the retention of the fit genes & discard of the redundant.A method for solving both constrained and unconstraint optimization problems.

New generations created by manipulating the genetic code using the tools such as selectioncrossover & mutation GA also works in similar manner with the objective to search appropriate solution for the problems involving either minization or maximization of the objective function.In every generation, a new set of artificial species or strings is created using bits and pieces of the fittest among old, an occasional new part may be tried for good measure.

By defaultin MATLAB ga calculates the minimum value of a function.So to find the global maximum of a functionwe can invert the function whose global maximum is to be found. find the minimum value of the inverted function, the negative of the minimum value will give the global maximum of the function.

So for our first study we will use for loop and iterate it around 50 times

we will use the below syntax

[inputs,fopt]=ga(function,number of variables)

here the number of variables are 2 thar is x and y

in inputs we will get 1*2matrix for each iteration,it will basically give the value of x and y and foot will give the optimum value of the function in these.

we can store the inputs and can use to surf command to create a surface to visualize the function.

using marker command to find the pint of maximum, tic toc command to look at the time taken for evaluation.

drawbacks of this is ga command does not know the bound of these x and y values so it can take the values greater than 0.6 or less for this we are settign second study to restrict the values of x and y.

clear all
close all
clc


% find the maxima of the input using genetic algorithm
% input data

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

figure(12)
surfc(XX,YY,-F)


xlabel('x value')
ylabel('y value')


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

 % study1- statistical behaviour
 num_case = 50
 tic
 for i =1:num_case
     [inputs,fopt(i)] = ga(@stalagmite,2);
     xopt(i) = inputs(1)
     yopt(i) = inputs(2)
 end

study_time_1 = toc
figure(1)
subplot(2,1,1);
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

% Study 2- statistical behaviour with upper and lower bounds
for i = linum_case
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_time_2 = toc
figure(2)
subplot(2,1,1);
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')


%study 3 increasing the ga iterations
tic
options = optimoptions('ga');
options = optimoptions(options,'populationsSize',270);
for i = 1:num_case
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1)
    yopt(i) = inputs(2)

end
study3_time = toc
figure(3)
subplot(2,1,1);
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

Function

function [F] = stalagmite(input_vector);
a= input_vector(1)
b= input_vector(2)
f1_x = [sin(5.1*pi*a+0.5)]^6
f1_y = [sin(5.1*pi*b+0.5)]^6
f2_x = exp(-4*log(2)*(((a=0.06667)^2/(0.64)));
f2_y = exp(-4*log(2)*(((a=0.06667)^2/(0.64)));



F= -(f1_x * f1_y* f2_x * f2_y )

end

Stalagmite function

Surface generated by GA plot

The plot generated by GA unbounded inputs

the PLot generated

upper & lower

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.