Challenge

Week 5 - Genetic Algorithm

clear all    
close all    
clc          

% Input for search space
x= linspace(0,0.6,150);   
y= linspace(0,0.6,150);   

% use meshgrid command to extract array in 2D array
[xx,yy] = meshgrid(x,y); 
% Evaluating objective function by using nested loop.

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_func(input_vector); 
    end 
end   
% Converting Fitting Function to change the graph upwards
F = -f;
% plot the results in the form of surface plot
surf(xx,yy,-f);   
shading interp   

% performing GA study
% 1) Unbounded study
num_cases = 50;      
tic                  
for i=1:num_cases     
    [inputs,fopt(i)]= ga(@stalagmite,2); 
    xopt(i)=inputs(1);
    yopt(i)=inputs(2); 
end
study_time_1 = toc;  

% plot the results of study 1
figure(1)   
subplot(2,1,1) 
hold on      
surfc(x,y,-f)   
shading interp  
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r') 
title('Bounded inputs')   % for heading or naming the graph.

%figure(2)
subplot(2,1,2) 
plot(-fopt)    
xlabel('Iteration')   
ylabel('Function minimum') 

% Study 2

tic    
for i=1:num_cases  
    [inputs2,fopt2(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]); 
    xopt2(i)=inputs2(1); 
    yopt2(i)=inputs2(2);
end
fopt2=fopt2;
time_study_2=toc; 

figure(2)
subplot(2,1,1)
hold on         
surfc(x,y,-f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')

%figure(2)
subplot(2,1,2)
plot(-fopt2)     
xlabel('Iteration')
ylabel('Function minimum')

% Study 3:- Maximising the population size to optimmise the result
options=optimoptions ('ga');
options=optimoptions(options,'PopulationSize',300); 

tic              
for i=1:num_cases
  [inputs3,fopt3(i)]= ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
    xopt3(i)=inputs3(1); 
    yopt3(i)=inputs3(2);
end
fopt3=fopt3;
time_study_3=toc  

figure(3)
subplot(2,1,1)
hold on       
surfc(x,y,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')

%figure(2)
subplot(2,1,2)
plot(-fopt3)         
xlabel('Iteration')
ylabel('Function 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.