Challenge

Week 4 - Genetic Algorithm

The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution. It is inspired by Charles Darwin's theory of natural evolution. Here genetic algorithm is used to find the global maxima of the stalagmite function.

STEPS USED IN THE GENETIC ALGORITHM (GA)

  • Initialize population
  • selection according to fitness
  • crossover between the selected chromosomes
  • perform the mutation

Syntaxt for ga

x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
x = ga(fun,nvars,A,b) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').

x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options) requires that the variables listed in IntCon take integer values.

Stalagmite Function Matlab Code

function formaximum = stalagmite(input_vector)
x=input_vector(1)
y=input_vector(2)

f1x = (sin(5.1*pi.*x+0.5))^6;
f1y = (sin(5.1*pi.*y+0.5))^6;
f2x = exp(-4*log(2).*(x-0.0667)^2 /0.64);
f2y = exp(-4*log(2).*(y-0.0667)^2 /0.64);
for_minimum = f1x.*f1y.*f2x.*f2y;
formaximum= -for_minimum 
end

Ga Optimisation codes

clear all 
close all
clc

x = linspace(0,0.6,150); % crating row matrix with 150 column
y = linspace(0,0.6,150); % crating row matrix with 150 column
[xx yy]= meshgrid(x,y); % creating 2D matrix
num_cases = 50; % number of times we want to run "ga"

% calculation of 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


% study-1 simply calling ga

% study-1 gives result of an unbounded system although we have given ga the
% boundary ie between the 0 and 0.6 but it does not takes those into
% account here 

% Now since the system is unbounde so the result we get will be different
% every time exexute the code

for i= 1:num_cases % i goes from 1 to 50
    % here inputs = the input at which the function is maximum
    
    % f_optimum is the optimum value of the function at those inputs
    
    %ga(@function name, number of variables in the function) so here we
    %have 2 variables these are x and y 
    
    [inputs, f_optimum(i)] = ga(@stalagmite, 2); % using ga
   % storing the input values as x and y optimum
   x_optimum(i)= inputs(1);
   y_optimum(i)= inputs(2);
end

figure(1)
% subplot is used to plot multiple plots ata time 
subplot(2,1,1)
%holding all the plots 
hold on
%generating the suface plot between all the inputs and the function 
 surfc(x,y,f)
 % shading interp removes the grid on the graph
shading interp
%ploting all the optimum values of x y and of the function for 50 times
%produced by ga

% they are shown by red dots and are connected though lines 
plot3( x_optimum, y_optimum, f_optimum,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
% plotting the optimum values to the iterations and seeing the deviation in
% the optimum value 
plot( f_optimum)
 xlabel(' iterations')
 ylabel(' function maximum')
 

% study-2 stastical behaviour whith forced upper and lower boundation
% study-2 gives result of an bounded system 


for i= 1:num_cases % i goes from 1 to 50
    % here inputs = the input at which the function is maximum
    
    % f_optimum is the optimum value of the function at those inputs
    
    %ga(@function name, number of variables in the function) so here we
    %have 2 variables these are x and y 
    
    %[0;0] is the lower bound value
    %[1;1] is the upper bound value
    
    [inputs, f_optimum(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[1;1]); % using ga
   % storing the input values as x and y optimum
   x_optimum(i)= inputs(1);
   y_optimum(i)= inputs(2);
end

figure(2)
% subplot is used to plot multiple plots at a time 
subplot(2,1,1)
%holding all the plots 
hold on
%generating the suface plot between all the inputs and the function 
 surfc(x,y,f)
 % shading interp removes the grid on the graph
shading interp
%ploting all the optimum values of x y and of the function for 50 times
%produced by ga

% they are shown by red dots and are connected though lines 
plot3( x_optimum, y_optimum, f_optimum,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
% plotting the optimum values to the iterations and seeing the deviation in
% the optimum value 
plot( f_optimum)
 xlabel(' iterations')
 ylabel(' function maximum')


% study-3 stastical behaviour whith forced upper and lower boundation with
% more number of sample sapace or with more no of samples
% study-2 gives result of an bounded system 

% increasing the no of sampleas y increasing the populationsize of 170
options = optimoptions('ga')
options = optimoptions(options,'populationSize',170);


for i= 1:num_cases % i goes from 1 to 50
    % here inputs = the input at which the function is maximum
    
    % f_optimum is the optimum value of the function at those inputs
    
    %ga(@function name, number of variables in the function) so here we
    %have 2 variables these are x and y 
    
    %[0;0] is the lower bound value
    %[1;1] is the upper bound value
    % now searching from a large populatuonsize although 
    [inputs, f_optimum(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[1;1],[],[],options); % using ga
   % storing the input values as x and y optimum
   x_optimum(i)= inputs(1);
   y_optimum(i)= inputs(2);
end

figure(3)
% subplot is used to plot multiple plots at a time 
subplot(2,1,1)
%holding all the plots 
hold on
%generating the suface plot between all the inputs and the function 
 surfc(x,y,f)
 % shading interp removes the grid on the graph
shading interp
%ploting all the optimum values of x y and of the function for 50 times
%produced by ga

% they are shown by red dots and are connected though lines 
plot3( x_optimum, y_optimum, f_optimum,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with more no of samples')
subplot(2,1,2)
% plotting the optimum values to the iterations and seeing the deviation in
% the optimum value 
plot( f_optimum)
 xlabel(' iterations')
 ylabel(' function maximum')

Results

Result-1 - Plots and results for unbounded system

Result-2 - Plots and results for Bounded System with lower and upper limit

Result-3 - Plots and results for unbounded system with lower and upper limit and a populationsize (sample space) of 170

Error and correction-1

The error came because of wrong syntax used it was supposed to be written as plot3 but I wrote plot(3) which made the syntax incorrect

Error and correction-2

Haven’t found any code for the get_stalagmite function so unless of making a new function I generated the stalagmite function in the codes itself

Error and correction-3

Syntaxt error as I wrote populationSize as poopilationsize out of excitement and corrected it afterwords

Refferences

https://in.mathworks.com/help/gads/ga.html?s_tid=mwa_osa_a#mw_5430547e-4111-4753-a80d-727f85b8f63e

https://www.youtube.com/watch?v=Q530u_g2CWE&list=PLYwpaL_SFmcDHUTN26NXKfjg6wFJKDO9R

https://skill-lync.com/challenges/question/SVhmM2VBYTRTREcyeEpHYUE4MUNFUT09OjpGWTZQb2E5ZmxTMW9xa1ZT/Z3BNUzRCdFkxdVFyTWlLZEx0OFpYZz09OjpKU2xIbWdsb3RuMENzNDdB/SW0yTUlPSWRjbWU3MjRDNXdnU0lCQT09OjpWVWViWExBdHVkYmRxc1Zu

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.