Genetic Algorithm
Section 1
The genetic algorithm is a method (function in MATLAB) for solving both constrained and unconstrained optimization problems that is based on Charles Darwin\'s Theory of Natural Selection, which says the fittest among the group survives. GA generates a number of points at each iteration. The best point in the population approaches an optimal solution.
The GA repeatedly modifies a population of individual solutions to get the best possible solution. 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.
Section 2
stalagmite.m
function [max_val] = stalagmite(inp_vector)
x = inp_vector(1);
y = inp_vector(2);
f1_x = (sin(5.1*pi*x+0.5)).^6;
f1_y = (sin(5.1*pi*y+0.5)).^6;
f2_x = exp(-4*log(2)*((x-0.0667).^2/0.64));
f2_y = exp(-4*log(2)*((y-0.0667).^2)/0.64);
max_val = -(f1_x*f1_y*f2_x*f2_y); % negative indicates maximum value
end
get_stal.m
function [f] = get_stal(x,y)
[X,Y] = meshgrid(x,y);
for i=1:length(X)
for j=1:length(Y)
inp_vector(1) = X(i,j);
inp_vector(2) = Y(i,j);
f(i,j) = stalagmite(inp_vector);
end
end
end
stalagmite_hw.m
clear all
close all
clc
% Defining search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Perform stalagmite function
f = get_stal(x,y);
% Number of iterations
num_it = 100;
% GA population size modifying
options = optimoptions(\'ga\');
options = optimoptions(options,\'PopulationSize\',300);
for i = 1:num_it
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
% Graphs
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded Input Values\')
subplot(2,1,2)
plot(fopt)
xlabel(\'No. of iterations\')
ylabel(\'Function Maxima\')
% Maximum Values
fprintf(\'x value: %g\\n\', inputs(1));
fprintf(\'y value: %g\\n\', inputs(2));
saveas(figure(1),\'stalagmite_hw.jpg\');
x value: 0.0668316
y value: 0.0668319