Challenge

Week 4.1 - Genetic Algorithm

clear var1
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
f = get_stalagmetic(x,y);
tic
% study 1 - statistical behaviour
for i = 1: num_cases
[inouts,fopt(i)] = ga(@stalagmetic,2);
xopt(i) = input(1);
yopt(i) = input(2);
end
study time = tac
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading intern
plot3(xopt,yopt,fopt,'marker','o','marker size',5,'marker face color','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('interation')
ylabel('function minimum')
tic
% study 2 -statistical behaviour -with upper and lower bounds
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmatic,2,[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i)= inputs(2);
end
study2_time = tac
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
plot 3 (xopt,yopt,fopt,'marker','0','markersize','s','markerface color,'r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('function minimum')
% study 3 increasing GA iteration
options = optimoptions('ga');
options = optimoptions(options,'population size',170);
tic
for
i = 1:num_cases
[inputs,fopt(i)] = ga (@stalagmetic,2,[1],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study 3 _time = tac
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
plot3(xopt,yopt,fopt,'marker','o','markersize','5','markerfactor','r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('functions minimum')
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.