Week 4 - Genetic Algorithm
main program:
clear all
close all
clc
x = linspace(0, 0.6, 100);
y = linspace(0, 0.6, 100);
[xx yy] = meshgrid(x, y);
f = zeros(100);
%f = get_stalagmite(x,y);
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
option = optimoptions('ga');
option = optimoptions(option, 'populationsize',170);
option = optimoptions(option, 'functiontolerance',1e-4);
iteration = 25
coor = zeros(iteration, 2);
fmax = zeros(iteration, 1);
for i = 1:iteration
[coor(i,:),fmax(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],option);
%[input, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],option);
% where coor and fmax can replace by [input, fopt(i)]
% thnan ac replace upper coor and fmax to this function
%xopt = input(1);
%yopt = input(2);
end
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
%surfc(x,y,f)
shading interp
plot3(coor(:,1),coor(:,2),-fmax,'o')
%plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
%plot3(xopt,yopt,fopt,'marker','*')
title('bounded input')
subplot(2,1,2)
plot(-fmax)
axis([0 iteration 0 1.2])
xlabel('iteration');
ylabel('fmax');
stalagmite function
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1 = (sin(5.1*pi.*x+0.5)).^6;
f2 = exp(-4*log10(2).*(x-0.0667).^2/0.64);
f3 = (sin(5.1*pi*y+0.5))^6;
f4 = exp(-4*log10(2).*(y-0.0667).^2/0.64);
f = -f1.*f3.*f4;
end
output:
plot :