Challenge

Week 4 - Genetic Algorithm

https://www.google.com/search?q=what+is+nvars+in+ga+syntax&oq=what+is+nvars+in+ga+&aqs=chrome.1.69i57j33.9626j0j4&sourceid=chrome&ie=UTF-8

clear all
close all
clc

% Input arrays
x= linspace (0,0.6,150);
y= linspace (0,0.6,150);



% Creating 2 dimentional mesh

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

% 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

num_cases= 50 % To run ga 50 times
% Study1- statistical behaviour
tic

for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study1_time = toc

figure(1)
subplot(2,1,1)% multiple plot inside same figure window
hold on
surfc(xx,yy,-f) % where f is optimum func.values that ga provides over 50 iteration
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbound inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')


% Study2- statistical behaviour- with upper and lower bounds
tic

for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study2_time = toc

figure(2)
subplot(2,1,1)% multiple plot inside same figure window
hold on
surfc(x,y,-f) % where f is optimum func.values that ga provides over 50 iteration
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')


% Study3- statistical behaviour- with upper and lower bounds & difference
% in size of the population

options = optimoptions('ga')
options = optimoptions(options,'populationsize',170);
tic

for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study3_time = toc

figure(3)
subplot(2,1,1)% multiple plot inside same figure window
hold on
surfc(x,y,-f) % where f is optimum func.values that ga provides over 50 iteration
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

% every -ve sign provides to make the plot in right position.If we provide
% without  -ve sign then it becomes upside down



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.