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

Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning.

Introduction to Optimization

Optimization is the process of making something better. In any process, we have a set of inputs and a set of outputs as shown in the following figure.

Optimization refers to finding the values of inputs in such a way that we get the “best” output values. The definition of “best” varies from problem to problem, but in mathematical terms, it refers to maximizing or minimizing one or more objective functions, by varying the input parameters.

The set of all possible solutions or values which the inputs can take make up the search space. In this search space, lies a point or a set of points which gives the optimal solution. The aim of optimization is to find that point or set of points in the search space.

Genetic Algorithms

Nature has always been a great source of inspiration to all mankind. Genetic Algorithms (GAs) are search based algorithms based on the concepts of natural selection and genetics. GAs are a subset of a much larger branch of computation known as Evolutionary Computation.

In GAs, we have a pool or a population of possible solutions to the given problem. These solutions then undergo recombination and mutation (like in natural genetics), producing new children, and the process is repeated over various generations. Each individual (or candidate solution) is assigned a fitness value (based on its objective function value) and the fitter individuals are given a higher chance to mate and yield more “fitter” individuals. This is in line with the Darwinian Theory of “Survival of the Fittest”.

In this way we keep “evolving” better individuals or solutions over generations, till we reach a stopping criterion.

Genetic Algorithms are sufficiently randomized in nature, but they perform much better than random local search (in which we just try various random solutions, keeping track of the best so far), as they exploit historical information as well.

Advantages of GAs

GAs have various advantages which have made them immensely popular. These include −

  • Does not require any derivative information (which may not be available for many real-world problems).
  • Is faster and more efficient as compared to the traditional methods.
  • Has very good parallel capabilities.
  • Optimizes both continuous and discrete functions and also multi-objective problems.
  • Provides a list of “good” solutions and not just a single solution.
  • Always gets an answer to the problem, which gets better over the time.

Syntax for genetic algorithm

We can give the inputs by any one of the following ways

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,option)

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(__)

Output from the function

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

[x,fval,exitflag,output] = ga(__) also return exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contain 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

close all 
clear all
clc

%input 
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);
num_spaces = 50;

%forming 2d araay
[xx,yy] = meshgrid(x,y);
for i=1:length(x)
    for j=1:length(y)
        input_vector(1)=xx(i,j);
        input_vector(2)=yy(i,j);
        f(i,j)=stagalmite(input_vector);
    end
end

%study 1-unbounded
tic
for i=1:num_spaces
    [results,fopt(i)]=ga(@stagalmite,2)
    xopt(i)=results(1)
    yopt(i)=results(2)
end
study_time1=toc
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')

%study 2 bounded
tic
for i=1:num_spaces
    [results,fopt(i)]=ga(@stagalmite,2,[],[],[],[],[0;0],[0.6;0.6])
    xopt(i)=results(1)
    yopt(i)=results(2)
end

study_time2=toc
figure(2)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'maker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')

%study3 - increased initial population
tic
option= optimoptions('ga')
options=optimoptions(options,'populationsize',250)
for i=1:num_spaces
    [result,fopt(i)]=ga(@stagalmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
    xopt(i)=results(1)
    yopt(i)=results(2)
end
study_time3=toc
figure(3)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'maker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')

STALAGMITE FUNCTION CODE

function A = stagalmite(input_vector)
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);
A=-(f1*f2*f3*f4);
end
        

Program explanation

close all

clear all

clc

%input

x= linspace(0,0.6,150);

y= linspace(0,0.6,150);

num_spaces = 50;

first we initialise the value of x and y taking 150 values between the interval 0 to 0.6

we consider no of iteration num_spaces=50.

%forming 2d araay

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

for i=1:length(x)

for j=1:length(y)

input_vector(1)=xx(i,j);

input_vector(2)=yy(i,j);

f(i,j)=stagalmite(input_vector);

end

end

Since we need to plot a 3D surface plot we take the 2d array of x and y using meshgrid command and store them in input_vector array, so that iit can be used as an argument to call the function stalagmite.

We create another function file the stalagmite function.here we take the output as -f since ga minimises a given function.so if -f is considered it automatically gives us the maxima.

function A = stagalmite(input_vector)

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);

A=-(f1*f2*f3*f4);

End

We take no of iteration = num_spaces and we use the genetic algorithm function to calculate the maxima of the function. We store the output into results which contain maximas and fopt which contain the values at which the maxima is obtained.

Here the ga is initiated with random population.

tic

for i=1:num_spaces

[results,fopt(i)]=ga(@stagalmite,2,[],[],[],[],[0;0],[0.6;0.6])

xopt(i)=results(1)

yopt(i)=results(2)

end

study_time2=toc

figure(2)

subplot(2,1,1)

surf(x,y,-f)

shading interp

hold on

plot3(xopt,yopt,-fopt,'maker','o')

subplot(2,1,2)

plot(-fopt)

xlabel('iteration')

ylabel('function maximum')

now we store the result into xopt ,yopt. Now we need to plot the surface of x,y,-f to get the 3d plot

we use the plot 3 command to make a plot of xopt,yopt and -fopt to plot the optimum maximum

also let us plot the fopt function so that we can visualise the optimum values at each iteration.

To get the time taken to complete cyclwe we used the ticc toc command to record the time.

Study has executed the output as displayed---

Here we can see the optimal values went outside the values of x and y resulting in falling away from stagalmite surface.

So we failed to reach the global maxima in this case.

The study time is = 2.2347

Now lets make our third study with initial population which is default as 50to 250

tic

option= optimoptions('ga')

options=optimoptions(options,'populationsize',250)

for i=1:num_spaces

[result,fopt(i)]=ga(@stagalmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)

xopt(i)=results(1)

yopt(i)=results(2)

end

study_time3=toc

figure(3)

subplot(2,1,1)

surf(x,y,-f)

shading interp

hold on

plot3(xopt,yopt,-fopt,'maker','o')

subplot(2,1,2)

plot(-fopt)

xlabel('iteration')

ylabel('function maximum')

here in this case to set a initial population we need to use command called optimum option and get our values of populationsize to 250

now we store the result into xopt,yopt. Now we need to plot the surface of x,y,-f to get the 3D plot

we use the plot3 command to make a plot of xopt,yopt and -fopt to plot the optimum maxima

if the study is executed the output is displayed as

through the time of execution is more in this case we are successfully to find out the correct maxima for the stalagmite function.

The time study = 11.7699 which is more than above two studies

OUTPUT


All the explanations for the output graphs is given in the program explanation section individually

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.