Challenge

Week 4 - Genetic Algorithm

AIM

      To write a code in MATLAB to optimise the stalagmite function and to find the global maxima of the function. And to explain the concept of genetic algorithm and its syntax in MATLAB. 

GENETIC ALGORITHM

      Genetic algorithm is a search based optimisation technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions. It is frequently used to solve optimisation problems, in research, and in machine learning.

WORKING OF GENETIC ALGORITHM

      

Genetic Algorithm optimisation starts with population initialization. Population is the number of sample data points taken to perform the genetic algorithm operation. These sample data points undergoes the following three steps,

GA IS USED IN

      Genetic Algorithms are primarily used in optimisation problems of various kinds, but they are frequently used in other application areas as well. The areas in which genetic Algorithms are frequently used are listed below.

  • Optimisation - Genetic Algorithms are most commonly used in optimization problems wherein we have to maximize or minimize a given objective function value under a given set of constraints.
  • Economics - GAs are also used to characterize various economic models like the cobweb model, game theory equlibrium resolution, asset pricing, etc.
  • Neural Networks - GAs are also used to train neural networks, particularly recurrent neural networks.
  • Image Processing - GAs are used for various digital image processing (DIP) tasks as well like dense pixel matching.

      The above are some of the areas where Genetic Algorithm is extensively used for optimization.

SYNTAX

 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,[],[],lb,ub,nonlcon,IntCon)

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

x = ga(problem)

[x,fval] = ga(___)

[x,fval,exitflag,output] = ga(___)

[x,fval,exitflag,output,population,scores] = ga(___)

Explanation

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.

PROGRAM DESCRIPTION

STALGAMITE FUNCTION  

      Genetic algorithm is basically used to find Global minima value. Now our objective is to find the global maxima so we are going to add the negative sign in the stalagmite function and also in the surf command in the main program and this will be plotted in the positive quadrant of the graph. So we can find the global maxima value.

Function code for this is attached below.

MATLAB CODE

function [f] = stalagmite(input_vector) %Stalagmite function

x = input_vector(1);
y = input_vector(2);

f1 = (sin(5.1*pi*x + 0.5)).^6;
f2 = exp(-4*log(2).*(((x -0.0667).^2)./0.64));
f3 = (sin(5.1*pi*y + 0.5)).^6;
f4 = exp(-4*log(2).*(((y -0.0667).^2)./0.64));

f = -(f1.*f2.*f3.*f4); %negative sign is used to get the global maxima
end

For getting the stalagmite function in the main program get_stalagmite funtion is used.

MATLAB CODE

function [f1] = get_stalagmite(x,y)
[xx,yy] = meshgrid(x,y)

for i = 1:length(xx)
    for j = 1:length(yy)
        input(1)=xx(i,j);
        input(2)=yy(i,j);
        f1(i,j)=stalagmite(input);
    end
end

Now coming to the main program, it is taken under 3 cases like Unbounded inputs, Bounded inputs and increased iterations.

MATLAB CODE

clear all
close all
clc

%Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

num_cases = 50;
f1=get_stalagmite(x,y);
F=-f1

figure(1)
surfc(x, y, F)
xlabel('x value');
ylabel('y value');
shading interp


%Study 1 : Statistical behaviour
tic
for i=1:num_cases
    [inputs, fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

study1_time = toc
fopt=-fopt;
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt, yopt, fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations');
ylabel('Function maximum');

tic
%Study 2 : Bounded inputs
tic
for i=1:num_cases
    [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

study2_time = toc
fopt=-fopt;
F=-f1
figure(3)
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')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations');
ylabel('Function maximum');


%Study 3 : Increased iterations
options=optimoptions('ga')
options=optimoptions(options,'populationsize',170)

tic
for i=1:num_cases
    [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

study3_time = toc
fopt=-fopt;
F=-f1
figure(4)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt, yopt, fopt,'marker','o','markersize',6,'markerfacecolor','r')
title('Increased Iterations')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations');
ylabel('Function maximum');

Here 3 studies were made to find the global maxima with the stalagmite function and genetic algorithm. Study time were calculated with tic and toc command. 

Unbounded inputs - x = ga(fun,nvars) is used in our program.

Bounded inputs - x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) is used.

Increased iteration with option command - x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options).

So we are going to obtain 3 different graphs for this program with their study time.

GRAPH

Graph 1

 

This graph is for the fitness function.
 
 
Graph 2
This is the graph of Study 1 Unbounded inputs.
 
 
Graph 3
 
This is the graph of Study 2 Bounded inputs.
 
 
Graph 4
This is the graph of Study 3 Increased Iteration. From this graph the global maxima is found.
 
 
WORKSPACE WINDOW
 
Here the study times for the 3 cases were mentioned as
1st case : 6.2633 seconds.
2nd case : 8.1272 seconds.
3rd case : 20.3902 seconds.
 
 
REFERENCES
 
 
RESULTS AND CONCLUSION
      A code in MATLAB to optimise the stalagmite function is written and The global maxima of the function is found. 
 
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.