Challenge

Week 4.1 - Genetic Algorithm

AIM:

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • To explain the concept of GA , also to explain the syntax for ga in MATLAB in your report.
  • To write program for plotting 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.

Explanation: In computer science and operations research, a genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover and selection.

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 code generates its random population of defined size and from that population the code evaluates the fitness of the members of the population according to which it forms new population based on no. of iterations performed.

1.Fitness Function

It is that function which we have to optimize and its optimization is done with the help of number of generations which have been formed by the code accordance with the fitness value of each member generated, these fitness values are used for the reproduction of the children.

  1. Selection

The idea of the selection phase is to select the fittest individuals and let them pass their genes to the next generation. The main goal of this phase is to find the region where the chances of getting the best solution are more.

Inspiration for this is from the survival of the fittest.

It should be a balance between exploration and exploitation of search space.

GA tries to move the genotype to higher fitness in the search space.

Too strong fitness selection bias can lead to sub-optimal solutions.

Too little fitness bias selection results in unfocused search.

  1. Reproduction

Generation of offspring happen in 2 way:

Crossover

Mutation

1. A) Crossover

Crossover is the most significant stage in the genetic algorithm. During crossover, a random point is selected while mating a pair of parents to generate offspring.

There are 3 major types of crossover.

Single Point Crossover:

A point on both parents’ chromosomes is picked randomly, and designated a ‘crossover point’.

Two-Point Crossover: Two crossover points are picked randomly from the parent chromosomes.

Unifofm Crossover: In a uniform crossover, typically, each bit is chosen from either parent with equal probability.

  1. B) Mutation

In a few new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This indicates that some of the bits in the bit chromosome can be flipped. Mutation happens to take care of diversity among the population and stop premature convergence.

Syntax

x = ga(fun,nvars)

x = ga(fun,nvars,A,b)

x = ga(fun,nvars,A,b,Aeq,beq)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)

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

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

x = ga(problem)

[x,fval] = ga(___)

[x,fval,exitflag,output] = ga(___)

[x,fval,exitflag,output,population,scores] = ga(___)

Description

x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.

x = ga(fun,nvars,A,b) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').

x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options) requires that the variables listed in IntCon take integer values.

x = ga(problem) finds the minimum for problem, a structure described in problem.

[x,fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.

[x,fval,exitflag,output] = ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.

[x,fval,exitflag,output,population,scores] = ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.

BODY OF THE CONTENT:

FIG1 [formulae for stalagmite function to plot the graph in the main functions]

Program for Stalagmite function :

function [f] = stalagmite_function(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;
t1 = ((x-0. 0667)^2)/(0.64);
f2_x = exp(-4*log(2)*t1);
t2 = ((y-0.0667)^2)/(0.64);
f2_y = exp(-4*log(2)*t2);
f = -(f1_x*f2_x*f1_y*f2_y);
end

FIg1.1 function for stalagmite.

Main program :

clear
close all
clc
%% PROVIDING THE SEARCH SPACE FOR THE FUNCTION
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[X,Y]=meshgrid(x,y);
num_cases=50; %Number of itreation for which the function will evaluate the fitness

%% CREATING THE SURFACE PLOT OF STALAGEMITE FUNCTION
for i=1:length(X)
for j=1:length(Y)
input_vector(1)=X(i,j);
input_vector(2)=Y(i,j);
f(i,j)=stalagmite(input_vector); %values of function in given search space
end
end
surfc(X,Y,-f); %generating the surface
xlabel ('X-value')
ylabel ('Y-label')
zlabel ('Function value')
shading interp %To remove the lines produced by the mesh grid

% STATISTICAL BEHAVIOUR (UNBOUNDED)
tic %calculating the study time through tic and toc command
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2); %calculating the optimized value of the function
xopt(i)=inputs(1); %at each iteration
yopt(i)=inputs(2);
end
study1_time=toc %displaying the time taken to evaluate
% PLOTING THE GRAPH
figure(1)
subplot(2,1,1)
surfc(X,Y,-f);
hold on
shading interp
plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
xlabel(‘x’);
ylabel(‘y’);
grid on
subplot(2,1,2)
plot(-fopt);
xlabel(‘iterations’)
ylabel(‘function min value’)


% STATISTICAL BEHAVIOUR (BOUNDED)
tic %calculating the study time through tic and toc command
for j=1:num_cases
[inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); %calculating the optimized value of the function
xopt(j)=inputs(1); %at each iteration with boundation
yopt(j)=inputs(2);
end
study2_time=toc

%PLOTTING
figure(2)
subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
xlabel(‘x’);
ylabel(‘y’);
grid on
subplot(2,1,2)
plot(-fopt);
xlabel(‘iterations’)
ylabel(‘function min value’)

%optimoptions function
tic
for k=1:num_cases
[inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study3_time=toc
%PLOTTING
figure(3)
subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
xlabel(‘x’);
ylabel(‘y’);
grid on
subplot(2,1,2)
plot(-fopt);
xlabel(‘iterations’)
ylabel(‘function min value’)

% PROVIDING THE SEARCH SPACE FOR THE FUNCTION
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[X,Y]=meshgrid(x,y);
num_cases=50; %Number of itreation for which the function will evaluate the fitness
%% CREATING THE SURFACE PLOT OF STALAGEMITE FUNCTION
for i=1:length(X)
for j=1:length(Y)
input_vector(1)=X(i,j);
input_vector(2)=Y(i,j);
f(i,j)=stalagmite(input_vector); %values of function in given search space
end
end
surfc(X,Y,-f); %generating the surface
shading interp %To remove the lines produced by the mesh grid

% STATISTICAL BEHAVIOUR (UNBOUNDED)
tic %calculating the study time through tic and toc command
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2); %calculating the optimized value of the function
xopt(i)=inputs(1); %at each iteration
yopt(i)=inputs(2);
end
study1_time=toc %displaying the time taken to evaluate

% PLOTING THE GRAPH
figure(1)
subplot(2,1,1)
surfc(X,Y,-f);
hold on
shading interp
plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
xlabel(‘x’);
ylabel(‘y’);
grid on
subplot(2,1,2)
plot(-fopt);
xlabel(‘iterations’)
ylabel(‘function min value’)

%% STATISTICAL BEHAVIOUR (BOUNDED)
tic %calculating the study time through tic and toc command
for j=1:num_cases
[inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); %calculating the optimized value of the function
xopt(j)=inputs(1); %at each iteration with boundation
yopt(j)=inputs(2);
end
study2_time=toc

%PLOTTING
figure(2)
subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
xlabel(‘x’);
ylabel(‘y’);
grid on
subplot(2,1,2)
plot(-fopt);
xlabel(‘iterations’)
ylabel(‘function min value’)

% MODIFYING THE NUMBER OF POPULATION
options=optimoptions(‘ga’,’populationsize’,300) %increaing the sixe of population by using

%optimoptions function
tic
for k=1:num_cases
[inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study3_time=toc

%PLOTTING
figure(3)
subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,’marker’,’o’,’markersize’,5,’markerfacecolor’,’r’);
xlabel(‘x’);
ylabel(‘y’);
grid on
subplot(2,1,2)
plot(-fopt);
xlabel(‘iterations’)
ylabel(‘function min value’)

FIG2 : Main program for optimisation of staligmite function

Output :

  • Stalamite function o/p

FIG 2: O/P for stalagmite function

  • Bounded inputs

  • Unbounded inputs

  • Study3 O/P

References:

  1. https://www.youtube.com/watch?v=MLPHLm56inQ&t=324s
  2. https://in.mathworks.com/help/gads/ga.html

*********************************************THANK YOU**********************

Vigneshwar Viswanathan

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.