Week 4 - Genetic Algorithm
Aim: To write a code in matlab to optimise the stalagmite function and the global maxima of the function.
What is genetic algorithm :
The genetic algorithm is a method for solving both constraints and unconstraints optimization problems that is based on natural selection the genetic algorithm repeatedly modifies a population of individual solution At each step the genetic algorithm selects individuals at random front current population to be prevent and uses them to produce children for next generation. Over successive generations the population evolves towards an optimal solution
Function Used:
Two function were utilized for this program one function defines required stalagmite function and the other function was used to create a meshgrid from provided search space evaluate stalagmite function for each instance.
Program
clear all
close all
clc
% Defining Search Space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
f = get_stalagmite(x,y);
%study1 :Unbounded Statistical Behaviour
tic
for i = 1:num_cases
[inputs1,fopt1(i)] = ga(@stalagmite,2);
xopt(i) = inputs1(1);
yopt(i) = inputs1(2);
end
study1_time = toc;
%Plotting
figure(1)
subplot (2,1,1)
hold on
surfc(x,y,-f)
xlabel('x-value')
ylabel('y-value')
zlabel('Function-value')
shading interp
plot3(xopt,yopt,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Inputs')
subplot (2,1,2)
plot (-fopt1)
xlabel ('Iterations')
ylabel ('Function Maximum')
tic
%study2 : Bounded Statistical Behaviour
for i = 1:num_cases
[inputs2,fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs2(1);
yopt(i) = inputs2(2);
end
study2_time = toc;
%Plotting
figure(2)
subplot (2,1,1)
hold on
surfc(x,y,-f)
xlabel('x-value')
ylabel('y-value')
zlabel('Function-value')
shading interp
plot3(xopt,yopt,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Inputs')
subplot (2,1,2)
plot (-fopt2)
xlabel ('Iterations')
ylabel ('Function Maximum')
%study3 : Bounded Statistical Behvaiour with Optimization Options
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);
tic
for i = 1:num_cases
[inputs3,fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i) = inputs3(1);
yopt(i) = inputs3(2);
end
study3_time = toc;
%Plotting
figure(3)
subplot (2,1,1)
hold on
surfc(x,y,-f)
xlabel('x-value')
ylabel('y-value')
zlabel('Function-value')
shading interp
plot3(xopt,yopt,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Inputs With Optimized Options')
subplot (2,1,2)
plot (-fopt3)
xlabel ('Iterations')
ylabel ('Function Maximum')
Total_Time = study1_time+study2_time+study3_time;
Function
function [f] = get_stalagmite(x,y)
% Creating a 2D Mesh
[xx yy] = meshgrid(x,y);
% Evaluating Stalagmite 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
function [f] = stalagmite(input_vector)
f_1_x = (sin(5.1*pi*input_vector(1) + 0.5))^6;
f_1_y = (sin(5.1*pi*input_vector(2) + 0.5))^6;
f_2_x = exp(-4*log(2)*((input_vector(1) - 0.0667)^2/0.64));
f_2_y = exp(-4*log(2)*((input_vector(2) - 0.0667)^2/0.64));
f = -(f_1_x*f_1_y*f_2_x*f_2_y);
Explanation :
initially to clear any previously defined values close any open windows and clear command window commands are provide.
Following that basic input are provided and through functions values of stalagmite function is computed for varurying values of input.
As per above mensioned key points three studies are programmed with their respective coded is used with a marker.
The tictoc command is used to measure time elapsed for running program.
To map varying positions of global maxima obtained through gentic algorithm
The label command and title commands are used to make plots easier to understand.
finally after three studies have finished running it is programmed to diploy folllowing information in command window for each study
value of global maxima.
values of x and y at which value of function becomes global maxima.
Time taken to evaluate program.
and also total time elapsed for evaluating all 3 studies
Result:
The surface contour plot with location of global maxima wrt different iterations as shown on top plot in bottom plot the values of global maxima varying over different iteration becomes a plotted