Challenge

Week 5 - Genetic Algorithm

GENETIC ALGORITHM

  • Write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
  • Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
%Writing the code in MATLAB to optimize the stalagmite function and find the global maxima of the function.

clear all
close all
clc

%assigning the required values
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%running the code for 50times
n= 50

%calling out the function
f = genetic_algorithm(x,y);

%case_study1
tic
for  i = 1:n
[inputs,fopt(i)] = ga(@stalgmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end 

study1_time = 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('statistical behavior')
subplot(2,1,2)
plot(-fopt)
xlabel('no. of iterations')
ylabel('function maximum')

%case_study2
tic
for  i = 1:n
[inputs,fopt(i)] = ga(@stalgmite,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(x, y, -f)
shading interp
plot3(xopt,yopt,-fopt,'marker','O','markersize',5,'markerfacecolor','r')

title('statistical behavior with lower boundary and higher boundary')
subplot(2,1,2)
plot(-fopt)
xlabel('no. of iterations')
ylabel('function maximum')

%case_study3

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

tic
for  i = 1:n
[inputs,fopt(i)] = ga(@stalgmite,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('statistical behavior with limits and population')
subplot(2,1,2)
plot(-fopt)
xlabel('no. of iterations')
ylabel('function maximum')


  • Here, I have used two inside functions "stalagmite" and "genetic_algorithm" those code can be given below.
  1. Stalagmite function:
function [f] = stalgmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1_x = sin(5.1*pi*x+0.5).^6;
f1_y = sin(5.1*pi*y+0.5).^6;

term1 = ((x-0.0667).^2/(0.640));
f2_x = exp(-4*log(2)*term1);
term2 =  ((y-0.0667).^2/(0.64));
f2_y = exp(-4*log(2)*term2);

f = -(f1_x*f2_x*f1_y*f2_y);
end

2. genetic algorithm:

function f = genetic_algorithm(x,y)
x = linspace(0,0.6,150);
y= linspace(0,0.6,150);
n = 50;
[xx  yy] = meshgrid(x, y);

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) = stalgmite(input_vector);

end
end
end
  • RESULTS:
  1. UNBOUNDED CASE:

2.BOUNDED CASE:

3.BOUNDED AND POPULATION CASE(FINAL RESULT):

  • AS PER THE QUESTION THE GLOBAL MAXIMA VALUE CAN BE GIVEN AS:

X=0.0668

Y=0.0668

Z=1

  • Make sure that your code gives the same output, even if it is made to run several times.
  • I had run the above code several times, but I got a similar answer.
  • Explain the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
  • The genetic Algorithm theory was first suggested by "CHARLES DARWIN" and developed by "JOHN HOLLAND".
  • John Holland is also known as the father of genetic algorithms.
  • The genetic algorithm is a technique for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution.
  • The genetic algorithm constantly modifies the population size of individual solutions.
  • The working process of ga can be given as:
  1. Initialization:
  • In this step, we have to decide the population size (the more the population size, the more we get accurate result).

2. Selection:

  • In this step, we have to select parents for sample data points on the basis of the high curve fit.

3. Cross-over:

  • In this step, take two parents and produce a child on the basis of interaction between genes.

4. Mutation:

  • In this step, the genes get replaced by random integers.

5. Fitness function:

  • In this step, we will identify how good the solution.
  • Flow chart of Genetic Algorithm Process:

  • Syntax of ga can be be given as:

ga(func,nvars,A,b,Aeq,Beq,lb,ub,nonlcon,Intcon,options)

  • fun - fitness function.
  • nvars- number of variables,
  • A,b= Linear inequality constraints
  • Aeq,beq=Linear equality constraints
  • lb- Lower bound(limit),
  • ub- Upper bound(limit),
  • nonlcon : Use for nonlinear constraints
  • IntCon : to get a solution in integer.
  • options- To set population size to minimize with non-default options
  • So, this was a detailed explanation of the genetic algorithm and its syntax.
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.