Week 4 - Genetic Algorithm
AIM:- TO FIND GLOBAL MAXIMA OF STALAGMITE FUNCTION USING GENETIC ALGORITHM ‘ga’ IN MATLAB
OBJETIVE
- To define the stalagmite function
- To find the optimum value of the stalagmite function through
- Statistical behaviour method
- Statistical behaviour with bounds method
- Statistical behaviour with bounds and population size method
- Calculate Global maxima
INTRODUCTION
The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, non differentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.
The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:
- Selection rules
- Crossover rules
- Mutation rules
SYNTAX FOR ‘ga’
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').
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').
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.)
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.)
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
finds the minimum for problem, where problem is a structure.
for any previous input arguments, also returns fval, the value of the fitness function at x.
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.
returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.
Equation of stalagmite function
`f1x= [sin(5.1πx+0.5)]^6`
`f1y= [sin(5.1πy+0.5)]^6`
`f2x=exp[-4ln(2)((x-0.0667)^2)/0.64]`
`f2y=exp[-4ln(2)((y-0.0667)^2)/0.64]`
`f(x,y)=f1xf2xf1yf2y`
STEPS FOR MATLAB CODE
Step1:- Defining the stalagmite function
- Function named stalagmite was defined with input_vector as argument that accepts the vector passed from function call
- f1x, f1y, f2x, f2y and f were define as per stalagmite formula
-ve sign is used to obtain global maxima
function [f] = stalagmite(input_vector)
x= input_vector(1);
y= input_vector(2);
f1x = (sin(5.1*pi*x+0.5)).^6;
f1y = (sin(5.1*pi*y+0.5)).^6;
f2x = exp(-4*log(2)*((x-0.0667)^2/0.64));
f2y = exp(-4*log(2)*((y-0.0667)^2/0.64));
f = -(f1x*f1y*f2x*f2y)
end
Step2:- Call function to main body
- Create 2D array of x and y by using meshgrid command
- Evaluating the stalagmite function by defining the input_vector at instant of xx and yy
clear all close all clc %inputs x = linspace(0,0.6,150); y = linspace(0,0.6,150); % creat 2D array [xx yy] = meshgrid(x,y); %Evaluating the satalagmite 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 surfc(xx,yy,-f) shading interp % applying genetic algorithm to find maximum value [input, fvalue] = ga(@stalagmite,2);
OUTPUT
CASE STUDY 1:- STATICAL BEHAVIOUR WITH UNBOUNDED
- num_case = 50, that will run 50 iteration
- tic-toc command is used to find the time taken to run the case study
- for loop run the function as 50 times and output value is stored in xopt(k) and yopt(k)
- ‘ga’ syntax used, [x,fval] = ga(fun,nvars)
%csae-1 statical behaviour
num_case = 50
tic
for k = 1:num_case
[input, fopt(k)] = ga(@stalagmite,2);
xopt(k) = input(1);
yopt(k) = input(2);
end
study_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('statistical behaviour of unbouunded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('maximum function value')
OUTPUT
CASE STUDY 2:- STATISTICAL BEHAVIOUR WITH BOUNDED
• num_case = 50, that will run 50 iterationk
• tic-toc command is used to find the time taken to run the case study
• ‘ga’ syntax used [x,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
% case-2 statical behaviour with boundary condition
tic
for m = 1:num_case
[input, fopt(m)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(m) = input(1);
yopt(m) = input(2);
end
study2_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('statistical behaviour of bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('maximum function value')
OUTPUT
CASE STUDY 3:- STATISICAL BEHAVIOUR WITH BOUNDED
- num_case = 50,
- tic-toc command is used to find the time taken to run the case study
- ‘ga’ syntax used [x,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub,noncolon,lntCo,options)
- Population size increase to 300 to obtain global maxima
% case-3 increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'populationsize',300);
tic
for n = 1:num_case
[input, fopt(n)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(n) = input(1);
yopt(n) = input(2);
end
study3_time = toc
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('statistical behaviour of bounded inputs by increasing ga iteration')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('maximum function value')
OUTPUT
COMPLETE CODE
clear all
close all
clc
%inputs
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% creat 2D array
[xx yy] = meshgrid(x,y);
%Evaluating the satalagmite 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
surfc(xx,yy,-f)
shading interp
% applying genetic algorithm to find maximum value
[input, fvalue] = ga(@stalagmite,2);
%csae-1 statical behaviour
num_case = 50
tic
for k = 1:num_case
[input, fopt(k)] = ga(@stalagmite,2);
xopt(k) = input(1);
yopt(k) = input(2);
end
study_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('statistical behaviour of unbouunded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('maximum function value')
% case-2 statical behaviour with boundary condition
tic
for m = 1:num_case
[input, fopt(m)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(m) = input(1);
yopt(m) = input(2);
end
study2_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('statistical behaviour of bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('maximum function value')
% case-3 increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'populationsize',300);
tic
for n = 1:num_case
[input, fopt(n)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(n) = input(1);
yopt(n) = input(2);
end
study3_time = toc
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('statistical behaviour of bounded inputs by increasing ga iteration')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('maximum function value')
CONCULUSION
time taken for caase study
STUDY1 - 26.0758
STUDY2- 23.0905
STUDY3- 119.3913
Global maxima of the stalagmite function at x= 0.06683 and y= 0.06683 is 1.
If we increase population size the time for case study also increase
Reference
https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html