Challenge

Week 4.1 - Genetic Algorithm

% This is the code for finding the global maximum of the mathematical
% function called stalgmite using genetic algorithm(ga) in the matlab. In this
% code we will be three studies for comparing outputs obtained and time 
% taken by the three types of syntax in the ga.

%Genetic algorithm is used to optimization problem it start with the inital
%set of random solutions called population, each individual in the
%population are called chromosome which evolve through succesive iteration
%and during iteration the chromosome are evaluated using some fitness
%measure and the next generation chromosome called offsprings are created
%using crossover operator or mutation to keep the randomness and not
%converge the solution immediately and then the best fit chromosome have
%the higher probabilty to move on to next generation and other chromosomes
%are rejected to keep the population size constant and when the solution is
%converged upto certain criterion and exiting condition is satisfied the 
%algorithm is terminated and solution is obtained 

%Regarding syntax of genetic algorithm
%[x,fval,exitflag,output,population,scores] = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)

%for R.H.S
%fun = function of which global minimum value is to be obtained.
%nvars = number of variable in the function.
%A,b are the linear inequality constraints of the function
%Aep, beq are the linear equality constraints of the function
%lb, ub are the lower and upper bounds of the solution of the function
%nonlcon subjects the minimization to the constraints defined in nonlcon it
%is used for constraint which are non linear.
%intcon is used when the constraints only has an integer value using this ga
%does not accept nonlinear equality constraints, only nonlinear inequality
%constraints.

%for L.H.S
%x= inputs variables provided to solve the function.
%fval = minimum value obtained from the solution of the function.
%exitflag = it is the integer at which the algorithm was terminated
%output = a structure that contains output from each generation and other 
% information about the performance of the algorithm.
%population = size of the population used to obtain the solution
%scores =  the scores of the final population.


% x and ymatrix for creating 2d matrix
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%creating 2d matrix for the input values in the stalgmite function
[xx, yy] = meshgrid(x,y);

%for loop for different input values in the stalgmite function and
%obtaining the solution and storing it in f variable.
for i = 1:length(xx)
    for j = 1:length(yy)
        inputvector(1) = xx(i,j);
        inputvector(2) = yy(i,j);
        f(i,j) = stalagmite_func(inputvector);
    end
end


%Study 1 unbounded ga

tic     %tic toc study for calcuating time taken by the algorithm to run 50 interations
for i = 1:50
    %here in the ga on the lhs inputs given to the function and minimum 
    % value of the solution are obtained and stored in the variable inputs and fopt
    %and on the rhs only function for which minimum values is to be obtained 
    % and number of variables in the fuctoin is provided, hence it is
    % unconstrained and unbounded study
    [inputs, f1opt(i)] = ga(@stalagmite_func,2);

    %x and y inputs are separated
    x1opt(i) = inputs(1);
    y1opt(i) = inputs(2);
end
studytime1 = toc;

%plotting for study 1 and storing it in figure 1
figure(1)
subplot(2,1,1)
hold on
surf(x,y,f);
shading interp
plot3(x1opt,y1opt,f1opt,'marker','o','markersize',5,'markerfacecolor','r');
title('Unbounded and unconstrained study')
subplot(2,1,2)
%min. value is multiplied with negative sign to obtain max. which is the aim of the study
actf1opt = -f1opt; 
plot(actf1opt)
xlabel('Iterations')
ylabel('Function Minimum')


%Study2 bounded solution

tic  %tic toc study for calcuating time taken by the algorithm to run 50 interations
for i = 1:50
    %In this study the lower and upper bounds for the inputs ar provided to
    %constraint the input of the algorithm betwwen 0 and 0.6
    [inputs, f2opt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.6;0.6]);
    x2opt(i) = inputs(1);
    y2opt(i) = inputs(2);
end
studytime2 = toc;

%plotting for study 2 and storing it in figure 2
figure(2)
subplot(2,1,1)
hold on
surf(x,y,f);
shading interp
plot3(x2opt,y2opt,f2opt,'marker','o','markersize',5,'markerfacecolor','r');
title('Bounded study')
subplot(2,1,2)
%min. value is multiplied with negative sign to obtain max. which is the aim of the study
actf2opt = -f2opt;
plot(actf2opt)
xlabel('Iterations')
ylabel('Function Minimum')


%Study 3

%optimoptions function is used to tell the matlab in which solver algorithm we
%need to change the parameters
options = optimoptions('ga');

%using options we change and set the definite population size for each iteration of the ga 
options = optimoptions(options,'PopulationSize',350);

tic
for i = 1:50
    %here the options part of the syntax is used to define the population
    %size of each iteration so the higher cycles are performed by ga to
    %obtain the global minimum value and get rid of other values in
    %crossover and mutations operations
    [inputs, f3opt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    x3opt(i) = inputs(1);
    y3opt(i) = inputs(2);
end
studytime3 = toc;

%plotting for study 3 and storing it in figure 3
figure(3)
subplot(2,1,1)
hold on
surf(x,y,f);
shading interp
plot3(x3opt,y3opt,f3opt,'marker','o','markersize',5,'markerfacecolor','r');
title('Bounded study with increased population size')
subplot(2,1,2)
%min. value is multiplied with negative sign to obtain max. which is the aim of the study
actf3opt = -f3opt;
plot(actf3opt)
xlabel('Iterations')
ylabel('Function Minimum')

%Creation of stalagmite function from the given mathematical relation
function stalagmite = stalagmite_func(inputvector)

m = inputvector(1);
n = inputvector(2);

f1_x = (sin(0.5+(5.1*m.*pi)))^6 ;
f1_y = (sin(0.5+(5.1*n.*pi)))^6 ;

f2_x = exp(((-(4*0.6931))*((m-0.0667).^2))/(0.64));
f2_y = exp(((-(4*0.6931))*((n-0.0667).^2))/(0.64));
%here the function is multiplied by -1 to obtain the global max. as ga only 
% identifies the global minimum
stalagmite = -(f1_x*f1_y*f2_x*f2_y); 
end
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.