Challenge

Week 4.1 - Genetic Algorithm

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%initiate the valuue from 0 to 0.6 with 150 intervals
num_spaces=50;

[xx yy]= meshgrid(x,y);

for i= 1:length(x)
for j=1:length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j); %we need to obtain firstly for 2D
f(i,j) = stalagmite_newm(input_vector);%stalagmite function which gives ud maxima
end
end
tic


for i= i:num_spaces
[input,fopt(i)]=ga(@stalagmite,2);%here ga intoduce random values
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end

study_time=toc

figure(1)
subplot(2,1,1)
surfc(x,y,-f)
xlabel('x')
ylabel('y')
title('unbounded','color','k')
shading interp
hold on;
plot3(xopt,yopt,-fopt,'marker','o','marker_size',5,'marker_face_color','r')
subslot (2,1,2)
plot(-fopt);
xlabel('iteration')
ylabel('function minimum')

%for figure 2

for i= i:num_cases
[input,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);%here ga intoduce random values
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end

study_time2=toc

figure(2)
subplot(2,1,1)
surfc(x,y,-f)
xlabel('x')
ylabel('y')
title('bounded','color','k')
shading interp
hold on;
plot3(xopt,yopt,-fopt,'marker','o','marker_size',5,'marker_face_color','r')
subslot (2,1,2)
plot(-fopt);
xlabel('iteration')
ylabel('function maximum')

%figure 3
tic
options=optimoptions('ga')
options=optimoptions(options,'populationsize',250)

for i= i:num_spaces
[input,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);%here ga intoduce random values
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end

study_time3=toc

figure(3)
subplot(2,1,1)
surfc(x,y,-f)
xlabel('x')
ylabel('y')

shading interp
hold on;
plot3(xopt,yopt,-fopt,'marker','o')
subslot (2,1,2)
plot(-fopt);
xlabel('iteration')
ylabel('function maximum')

answers

1) ga is adopting natural values

and syntax - [input,fopt(i)]=ga(@stalagmite,2) i use this syntax to call stalagmite function with random population

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.