Optimizing the stalagmite function using GA
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions.
At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.
clc
clear all
close all
num_cases = 100;
% Defining our space search
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Creating a 2dimensional mesh
[xx yy] = meshgrid(x,y);
% Evaluating the 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
%setting population size
options=optimoptions('ga');
options=optimoptions(options,'populationSize',470);
%finding out minimum value for iterations=numcases
tic
for j=1:num_cases
[inputs,fopt(j)]=ga(@Stalagmite,2,[],[],[],[],[0;0],[1,1],[],[],options);
x_opt(j)= inputs(1);
y_opt(j)=inputs(2);
end
study_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,S);
shading interp;
xlabel( 'x ');
ylabel( 'y ');
plot3(x_opt,y_opt,fopt, 'marker ', 'o ', 'markersize ',5)
subplot(2,1,2)
plot(fopt)
xlabel( 'iteration ')
ylabel( 'fmin ')
maxvalue=1 % from figure as minimum value of -f is -1 so max value of f is 1
The changed stalagmite function is given as
function [stal] = Stalagmite(input_vector)
x_val = input_vector(1);
y_val = input_vector(2);
f1 = (sin(5.1*pi*x_val + 0.5)).^6; % f1x
f2 = (sin(5.1*pi*y_val + 0.5)).^6; % f1y
f3 = exp((-4*log(2))*((x_val - 0.0667).^2)/0.64); % f2x
f4 = exp((-4*log(2))*((y_val - 0.0667).^2)/0.64); % f2y
stal = -f1.*f2.*f3.*f4;
end