Week 4 - Genetic Algorithm
Aim:- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Introduction :- The genetic algorithm GA is a method for solving both constrained and unconstrained optimisation problems that is based on natural selction,the process that drives biological evolution. GA repeatedly modies the 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 children for the next generation. Over successive generations, the popultation evolves towards an optimal solution. GA can be applied to solve a variety of optimization problems that are not well suited for standard optimazation algorithms, including problems in which the objective function is discontinuous, non differentiable, stochastic, or highly non linear. The GA uses 3 main types of rules at each step to create the next gerneration from the current population :
- Selection rule selects the individuals, called parents, that contribute to the population at the next generation.
- crossover rules combine two parents to form children for the next generation.
- Mutation rules apply random changes to individual parents to form children.
Genetic Algorithms (GAs) come under the category of evolutionary algorithms with working principle based on the mechanics of natural genetics. The basic objective in natural genetics is the retention of the t genes & discard of the redundant. New generations created by manipulating the genetic code using the tools such as selection, crossover & mutation. GA also works in similar manner with the objective to search appropriate solution for the problems involving either minimization or maximization of the objective function. GA use similar tools as selection, crossover & mutation applied to a population of binary strings generated randomly. In every generation, a new set of articial species or strings is created using bits and pieces of the ttest among old; an occasional new part may be tried for good measure. Genetic algorithms are proven to yield robust search in complex spaces.
##### Stalagmite function used in this study :-
############# Matlab code to stalagmite function #############
% stalagmite function
function function_value = stalagmite(input_value)
x = input_value(1);
y = input_value(2);
function_1_x = (sin(5.1.*pi.*x + 0.5))^6;
function_1_y = (sin(5.1.*pi.*y + 0.5))^6;
function_2_x = exp(-4.*log(2).*((x - 0.0667)^2)/0.64);
function_2_y = exp(-4.*log(2).*((y - 0.0667)^2)/0.64);
function_value = -(function_1_x.*function_2_x.*function_1_y.*function_2_y);
end
clear all
close all
clc
% defining search space
x = linspace(0,0.4,300);
y = linspace(0,0.4,300);
number_of_cases = 200; % Number of Iteration Count
[xx, yy] = meshgrid(x, y); % Creating a 2D mesh
% Evaluating the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input_value(1)=xx(i,j);
input_value(2)=yy(i,j);
fun(i,j) = stalagmite(input_value);
end
end
for i = 1:number_of_cases
[inputs, function_val(i)] = ga(@stalagmite,2);
option_x(i) = input_value(1);
option_y(i) = input_value(2);
end
figure(1)
subplot(2,1,1)
surfc(xx,yy,-fun)
hold on
shading interp % Removing gridlines on the 3D surface plot
plot3(option_x,option_y,-function_val,'marker','.','markersize',2)
title('UNBOUNDED INPUT VALUE')
subplot(2,1,2)
plot(-function_val)
xlabel('Number Of Itetrations Occur')
ylabel('Function Maximum')
for i = 1:number_of_cases
[inputs, function_val(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
option_x(i) = inputs(1);
option_y(i) = inputs(2);
end
figure(2)
subplot(2,1,1)
surfc(xx,yy,-fun)
xlabel('x value')
ylabel('y value')
hold on
shading interp
plot3(option_x,option_y,-function_val,'marker','.','markersize',2)
title('BOUNDED INPUT VALUE')
subplot(2,1,2)
plot(-function_val)
xlabel('Number Of Itetrations Occur')
ylabel('Function Maximum')
% BOUNDED INPUTS WITH ALTERED POPULATION SIZE
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',500);
for i = 1:number_of_cases
[inputs, function_val(i)] =ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
option_x(i) = inputs(1);
option_y(i) = inputs(2);
end
figure(3)
subplot(2,1,1)
surfc(xx,yy,-fun)
xlabel('x value')
ylabel('y value')
hold on
shading interp
plot3(option_x,option_y,-function_val,'marker','.','markersize',2)
title('BOUNDED INPUTS WITH ALTERED POPULATION SIZE')
subplot(2,1,2)
plot(-function_val)
xlabel('Number Of Itetrations Occur')
ylabel('Function Maximum')
Output of the stalagmite function : -
Conclusions:- Thus a code was written in MATLAB to optimise the stalagmite function and to find the global maxima of the function.
References:-