Challenge

Week 4.1 - Genetic Algorithm

AIM: Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

THEORY:

Global Algorithm:

The genetic algorithm repeatedly modifies a population of individual solutions. At each step the genetic algorithm selects individuals at random from the each current population to be parents and uses them to produce the children for the next generation.

The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:

  • Selection rules select the individuals called parents, that contribute to the population at the next generation.
  • Crossover rules combine twp parents to form children for the next generation.
  • Mutation rules apply random changes to individual parents to form children.

"Syntax For GA"

x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)
x = ga(problem)
[x,fval] = ga(___)
[x,fval,exitflag,output] = ga(___)
[x,fval,exitflag,output,population,scores] = ga(___)
FitnessfcnFitness Functions

nvars

Number of design variables

Aineq

A matrix for linear inequality constraints

Bineq

b vector for linear inequality constraints

Aeq

Aeq matrix for linear equality constraints

Beq

beq vector for linear equality constraints
lb

Lower bound on x

ub

Upper bound on x
nonlcon

Nonlinear constraint function

rngstate Optional field to reset the state of the random number generator
solver ga
options Options created using optimoptions or expotred from the optimization app
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,Aeq,beq,lb,ub,nonlcon,intcon) or x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options) requires that the variables listed in intcon take integer values.

x = ga(problem) finds the minimum for problem, a structure described in problem.

[x,fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.

[x,fval,exitflag,output] = ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.

[x,fval,exitflag,output,population,scores] = ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.

PROGRAM:

% Defining our search space to be optimized.
% Creating One dimensional array
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);
num_cases = 60;
% Creating two dimensional arrays from one dimensional array
[xx,yy]= meshgrid(x,y);

% Evaluating the 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_fun(input_vector);
    end
end

surf(xx,yy,f)
shading interp

xlabel('x value')
ylabel('y value')
[x_output,fval]= ga(@stalagmite_fun,2);

Inputs X and Y are assigned by using linspace command and afterwards 2 dimensional array is generated by using meshgrid command. "forloop" command is used for repetitive vector data formation and executed by calling hyperlinked function code fot stalagmite.

Function Code:

function [stalagmite]= stalagmite_fun(input_vector)
x= input_vector(1)
y= input_vector(2)
f1_x = (sin(5.1*pi*x+0.5)).^6;
f2_y = (sin(5.1*pi*y+0.5)).^6;
f3_x = exp((-4*0.6931).*(x-0.0667)^2/(0.64));
f4_y = exp((-4*0.6931).*(y-0.0667)^2/(0.64));
stalagmite = (f1_x.*f2_y.*f3_x.*f4_y)
end

% Defining our search space to be optimized.
% Creating One dimensional array
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);
num_cases = 60;
% Creating two dimensional arrays from one dimensional array
[xx,yy]= meshgrid(x,y);

% Evaluating the 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_fun(input_vector);
    end
end

% Study1 Statistical Behaviour
tic
for i = 1:num_cases
[inputs,fopt(i)]= ga(@stalagmite_fun,2);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end
study_time_1= toc;

% Plotting
figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,f);
shading interp

xlabel('x data');
ylabel('y data');
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',3,'MarkerFaceColor','r');
title('Unbounded Inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Maximum Function')


% Study2 Bounded Inputs

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

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('X data')
ylabel('Y data')
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',3,'MarkerFaceColor','g');
title('Bounded Inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Maximum Function')



% Study3 Bounding the valuesand increasing population size

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300);
tic
for i= 1:num_cases
    [inputs,fopt(i)]= ga(@stalagmite_fun,2,[],[],[],[],[0,0],[1,1],[],[],options);
    xopt(i)= inputs(1);
    yopt(i)= inputs(2);
end 

study_time_3= toc

figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('X data')
ylabel('Y data')
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',3,'MarkerFaceColor','g');
title('Bounded Inputs with Increasing population size');
subplot(2,1,2)
plot(fopt)
xlabel('Iterations');
ylabel('Maximum Functions');

Case Study 1: Unbounded inputs

Case Study 2: Bounded the values

Case Study 3: Bounded inputs with increased population size

Workspace Data:

Presented results shows data from 3 experimented investigations to examine statistical behavior which shows optimization of code has been done.

REFERENCE:

https://www.iitg.ac.in/rkbc/CE602/CE602/Genetic%20Algorithms.pdf

https://in.mathworks.com/videos/what-is-a-genetic-algorithm-100904.html

https://in.mathworks.com/help/gads/ga.html

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.