Challenge

Week 4.1 - Genetic Algorithm

 clear all
 close all
 clc

% Plotting stalagmite funciton 

% define the search space 
x = linspace(0, 0.6, 150);
y = linspace(0, 0.6, 150);

num_cases = 60;

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

% Evaluating 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 

surfc(xx, yy, f)

shading interp 

% Study case 1 Statistical behaviour without bounds

tic
for i = 1:num_cases
    [opt_inputs, fval_opti(i)] = ga(@stalagmite, 2); 
    x_opti(i)= opt_inputs(1);
    y_opti(i)= opt_inputs(2);
end 
studytime1= toc 

figure(1)
subplot (2, 1, 1);
hold on
surfc(xx, yy, f);
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
shading interp 
plot3(x_opti, y_opti,fval_opti, 'marker', 'o', 'markersize',5, 'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fval_opti)
xlabel('No. of iterations');
ylabel('F maximum');

% study case 2 Statistical behaviour with bounds 

tic 
for i = 1:num_cases 
    [optimum_inputs, fval_opti(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    x_opti(i)= optimum_inputs(1);
    y_opti(i)= optimum_inputs(2);
end 
study_time2 =toc

figure(2)
subplot (2, 1, 1);
hold on
surfc(xx, yy, f);
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
shading interp 
plot3(x_opti, y_opti,fval_opti, 'marker', 'o', 'markersize',5, 'markerfacecolor','g')
title('bounded inputs')
subplot(2,1,2)
plot(fval_opti)
xlabel('No. of iterations');
ylabel('F maximum');


% study case 3 Statistical behaviour with bounds and increased population 

ct = 3;
for population_size = [150:50:250]; 
    
    options =optimoptions('ga');
    options= optimoptions(options, 'populationSize', population_size);
    
    tic
    for i= 1:num_cases
        [optimum_inputs, fval_opti(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    x_opti(i)= optimum_inputs(1);
    y_opti(i)= optimum_inputs(2)
    end 
    
    study_time(ct)= toc 
    
          
figure(ct)
subplot (2, 1, 1);
hold on
surfc(xx, yy, f);
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
shading interp 
plot3(x_opti, y_opti,fval_opti, 'marker', 'o', 'markersize',5, 'markerfacecolor','g')
title('bounded inputs with increase in population size')
subplot(2,1,2)
plot(fval_opti)
xlabel('No. of iterations');
ylabel('F maximum');
ct=ct+1
end 



%% Stalagmite function

function T = stalagmite(x)

term1 = (sin(5.1*pi*x(1)+0.5)).^6

term2 = (sin(5.1*pi*x(2)+0.5)).^6 

term3 = exp(((-4)*log(2)*((x(1)-0.0667).^2))/0.64)

term4 = exp(((-4)*log(2)*((x(2)-0.0667).^2))/0.64)

T = (-1)*(term1.* term2.* term3.* term4 )

end

% Explaination about the Program: First of all, the area for plot is selected by linspace command. Meshgrid is generated by meshgrid command. Then for function to implement, we are using two for loops. One for considering x values and other for y values. Inside of loops, input vectors are created for x and y. Then function stalagmite is plotted giving input vectors as input to function. surfc command is used to plot the 3D plot of the function. Shading interp is used for shading the plot. Then genetic algorithm which is inbuilt function gives the optimum value of the function. Here in this case, as we have to find out the maximum value of the function, we are multiplying the function by (-1). Because ga always finds out minimum value. 

% About genetic algorithms : This term is related to natural selection and genetics. They are used to find out optimal or near optimal solution to difficult problems which might take very long time to solve. In GA, we have all possible solutions to the problem and then these solutions have to undergo recombination or mutation to see optimal solution and process is repeated for no of iterations and combinations. Now, each solution is rated on fitness value which depends upon the objective function. Fittest will survive which resembles with Darwinian theory of "survival of fittest."
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.