Challenge

Week 4.1 - Genetic Algorithm

Aim: To write a program in MATLAB to optimize the stalagmite function and find the global Maxima of the function.

Theory:

Genetic Algorithm:

  • A Genetic Algorithm is an optimization technique that solves nonlinear and non-differential optimization problems.
  • GA optimizes the problems through a natural selection process that uses biological evolution to search for global minima or maxima of an optimization problem.
  • GA starts with an initial generation of solutions that are tested to the objective function which generates parent variables from the first generation through selection, crossover, and mutation.
  • At each step, GA selects random variables to be parents which produce children for the next generation.
  • As the generations grow the population evolves to an optimum solution. This can be done by setting a fixed number of generations or getting the best fitness function value that is no longer changing or changing by a small amount.

Syntax:

ga (function, no. of variables)

Here ga is the solver which finds the minimum of the function

ga (function, no. of variables, A, B, Aeq, Beq, UB, LB, [],[], options)

A, B, Aeq, Beq- Co-eff of a function with inequalities

LB, UB- lower and upper boundaries on the variables of the function

Equation used:

`f1=[sin(〖5.1πx+0.5)]〗^6`

`f2=[sin(〖5.1πy+0.5)]〗^6`

`f3=exp[-4 ln⁡(2) ((〖x-0.1667)〗^2)/0.64]`

`f4=exp[-4 ln⁡(2) ((〖y-0.1667)〗^2)/0.64]```

Flow of the project:

1.Code to solve the stalagmite function.

2.Code with all 3 cases for F-maximum vs no. of iterations.

3.Code explanation.

4.Results.

1.Code to solve the stalagmite function.

function [F] = get_stalagmite(input)

x = input(1);

y = input(2) ;

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

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

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

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

F = -1*(f1*f2*f3*f4);

end

1.Code with all 3 cases for F-maximum vs no. of iterations.

close all

clear all

clc

%%

x = linspace(0,0.6,150);

y = linspace(0,0.6,150);

[xx, yy] = meshgrid(x,y);

tic

%creating the stalagmite

for i = 1:length(xx)

for j = 1:length(yy)

input(1) = xx(i,j);

input(2) = yy(i,j);

f(i,j) = get_stalagmite(input);

end

end

study_time = toc %to determine the time taken to execute the program

%%

%case-1

num_cases = 50 %no. of iterations

tic

%creating the stalagmite

for i = 1:num_cases

[inputs, fopt(i)] = ga(@get_stalagmite,2);

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

study_time = toc

figure(1)

subplot(2,1,1) %Dividing the graphs

surfc(xx,yy,-f)

shading interp

hold on

plot3(xopt,yopt,-fopt,'Marker','o','Markerfacecolor','b','Markeredgecolor','r')

title('unbounded input')

hold off

subplot(2,1,2)

plot(-fopt)

xlabel('Iterations')

ylabel('function maximum')

%%

%case-2

num_cases = 50

tic

%creating the stalagmite

for i = 1:num_cases

[inputs, fopt(i)] = ga(@get_stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]); %ga solver with boundaries

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

study_time = toc

figure(1)

subplot(2,1,1) %Dividing the graphs

surfc(xx,yy,-f)

shading interp

hold on

plot3(xopt,yopt,-fopt,'Marker','o','Markerfacecolor','b','Markeredgecolor','r')

title('unbounded input')

hold off

subplot(2,1,2)

plot(-fopt)

xlabel('Iterations')

ylabel('function maximum')

%%

%case-3

num_cases = 50

tic

%change in the population

options = optimoptions('ga')

options = optimoptions(options,'populationsize',250)

%creating the stalagmite

for i = 1:num_cases

[inputs, fopt(i)] = ga(@get_stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);%ga solver with boundaries

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

study_time = toc

figure(1)

subplot(2,1,1) %Dividing the graphs

surfc(xx,yy,-f)

shading interp

hold on

plot3(xopt,yopt,-fopt,'Marker','o','Markerfacecolor','b','Markeredgecolor','r')

title('unbounded input')

hold off

subplot(2,1,2)

plot(-fopt)

xlabel('Iterations')

ylabel('function maximum')

2.Code explanation.

  • Initially, a function is created with the equations which create the stalagmite and which has the inputs in the form of an array that contains input(1) and input(2).
  • Two sets of values are assumed and by using the mesh grid converted the sets into a square matrix.
  • Tic and toc command is used to find the time taken to execute the program.
  • By using the for loop and giving the values of the square matrix as an input to the function created we get the desired values with which we can plot the stalagmite. These values are stored in the variable “f”.
  • For case-1 we have set the no. of iterations to 50 which gives 50 generations and by using for loop and the genetic algorithm solver will give optimum values which are prefect values that form the stalagmite.
  • By using the subplot command we can divide the graphics window into two parts in which the stalagmite is created using the “surfc” and “plot3” command in the 1st part and a graph that depicts the F maximum for each iteration. Thus by creating this we can compare these graphs which shows us how the genetic algorithm gives the results beyond the limits.
  • In order to avoid the huge no. of outputs outside the given range the boundaries are set that limit the minimum and maximum values of the range are constrained.
  • In order to set the boundaries another syntax of GA used“ga(@get_stalagmite,2,[],[],[],[],[0,0],[0.6,0.6])”.
  • This narrows the outputs of the function which gives us the local minima at this stage.
  • In the 3rd case, we will be changing the population so that we can get a wide range of data that gives us the global maxima.
  • The population can be changed by using the “optimoptions” command and store the data in the options variable.
  • The syntax used for this case is “ga(@get_stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);.
  • By executing this case we will get the global maximum of the function.

RESULTS:

CASE-1

CASE-2

CASE-3

  • Time taken to run the 1st case: 9.7246
  • Time taken to run the 2nd case: 6.2499
  • Time taken to run the 3rd case: 1622`
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.