Project

genetic algorithm

function [fmax] = stalagmite(input_vector);

f1x = (sin((5.1*pi*input_vector(1))+0.5))^6;
f1y = (sin((5.1*pi*input_vector(2))+0.5))^6;
f2x = exp((-4*log(2)*(input_vector(1)-0.0667)^2)/0.64);
f2y = exp((-4*log(2)*(input_vector(2)-0.0667)^2)/0.64);


fmin = (f1x*f2x*f1y*f2y);


fmax = -1*fmin;


end

clear all
close all
clc


x = linspace(0,0.6,100);
y = linspace(0,0.6,100);

LB = [0 0];
UB = [1 1];


option = optimoptions(@ga,\'PlotFcn\',{@gaplotbestf,@gaplotstopping},...
\'CrossoverFcn\', {@crossoverscattered},\'MaxGenerations\',30,...
\'MaxStallGenerations\', 15, \'MaxTime\', 8, \'MaxStallTime\', 5, \'PopulationSize\', 200, \'SelectionFcn\',...
@selectiontournament,\'FitnessScalingFcn\',@fitscalingprop, ...
\'Display\',\'diagnose\');


nvars = 2;

fitnessfcn = @stalagmite;

[X Y] = meshgrid(x,y);

for i = 1:length(X);
for j = 1:length(Y);
input_vector(1) = X(i,j);
input_vector(2) = Y(i,j);
fmax(i,j) = stalagmite(input_vector);
end
end

surfc(X,Y,fmax)
title(\'Stalagmite function\')
xlabel(\'x values\')
ylabel(\'y values\')
zlabel(\'fitness function values\')
shading interp

[inputs, f_max_val, exitflag, Output] = ga(fitnessfcn,nvars,[],[],[],...
[],LB,UB,[],option);


fprintf(\'The number of generations was: %d\\n\', Output.generations);
fprintf(\'The number of function evaluations was: %d\\n\', Output.funccount);
fprintf(\'The best function value (maximum value) found was: %g\\n\', f_max_val);
fprintf(\'Final x value was: %g\\n\', inputs(1));
fprintf(\'Final y value was: %g\\n\', inputs(2));

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.