Project

GENETIC ALGORITHM

AIM:

To calculate the Global maxima of stalagmite function using MATLAB.

INTRODUCTION:

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.

WHAT ARE 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.

GAs were developed by John Holland and his students and colleagues at the University of Michigan, most notably David E. Goldberg and has since been tried on various optimization problems with a high degree of success.

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 GA'S:

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.

  • Useful when the search space is very large and there are a large number of parameters involved.

LIMITATIONS OF GA'S:

Like any technique, GAs also suffer from a few limitations. These include −

  • GAs are not suited for all problems, especially problems which are simple and for which derivative information is available.

  • Fitness value is calculated repeatedly which might be computationally expensive for some problems.

  • Being stochastic, there are no guarantees on the optimality or the quality of the solution.

  • If not implemented properly, the GA may not converge to the optimal solution.

SYNTAX DESCRIPTION:

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:

OBJECTIVES:

1) To optimize the stalagmite function.

2) To determine the Global Maxima using Genetic Algorithm.

stalagmite function code:

The code is used to get the value of the function (f_1) at different grid points on the x-y plane which will be plotted in the z-direction. we have multiplied the -ve sign to convert the function into maxima since the genetic algorithm function can only calcualte the minima value for a function.

function [f_1]=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);

f_1 = -(f1x*f1y*f2x*f2y);

end

get_stalagmite function code

This code for get_stalagmite() will help us in generating the stalagmite 3D surface using meshgrid, in which the points xx and yy are used to create 2d array and this is used to input in the stalagmite function using two for loops within which it is automatically called when the get_stalagmite function is initiated.

function [f]= get_stalagmite(x,y)

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

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
end

Genetic algorithm code:

clear all
close all
clc

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

num_cases=60;
f= get_stalagmite(x,y);

%study 1- statistical behaviour without bounds
tic
for i=1:num_cases
    [optimum_inputs,fval_opti(i)]= ga(@stalagmite,2);
    x_opti(i)=optimum_inputs(1);
    y_opti(i)=optimum_inputs(2);
end
study_time1=toc

figure(1)
subplot(2,1,1);
hold on
surfc(x,y,-f);
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
shading interp
plot3(x_opti,y_opti,-(fval_opti),'marker','o','markersize',5,'markerfacecolor','r');
title('Unbounded inputs');
subplot(2,1,2);
plot(-fval_opti);
xlabel('No of Itrations');
ylabel('F maximum');


%study 2- statistical behaviour with bounds
tic
for i=1:num_cases
    [optimum_inputs,fval_opti(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    x_opti(i)=optimum_inputs(1);
    y_opti(i)=optimum_inputs(2);
end
study_time2=toc


figure(2)
subplot(2,1,1);
hold on
surfc(x,y,-f);
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
shading interp
plot3(x_opti,y_opti,-(fval_opti),'marker','o','markersize',5,'markerfacecolor','r');
title('Bounded inputs');
subplot(2,1,2);
plot(-fval_opti);
xlabel('No of Itrations');
ylabel('F maximum');

% study 3- statistical behaviour with bounds and increased population method
ct=3;
for population_size= [150:50:250]
    
options=optimoptions('ga');
options=optimoptions(options,'populationSize',population_size);


tic
for i=1:num_cases
    [optimum_inputs,fval_opti(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    x_opti(i)=optimum_inputs(1);
    y_opti(i)=optimum_inputs(2);
end
study_time(ct)=toc

figure(ct)
subplot(2,1,1);
hold on
surf(x,y,-f);
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
shading interp
plot3(x_opti,y_opti,-(fval_opti),'marker','o','markersize',5,'markerfacecolor','r');
title('Bounded inputs with increase in poulation size');
subplot(2,1,2);
plot(-fval_opti);
xlabel('No of Itrations');
ylabel('F maximum');
ct=ct+1
end

Results and plots:

Study 1: PLOT

here we have run the ga for 50 iterations and plot each and every optimal solution on the 3D surface of the stalagmite function, along with that the 2nd part of the subplot shows the Fmax/otpi V/s Number of iterations

here we can see that the values are all over the place since no bounds are specified.

Study 2: PLOT

since from the study 1, we can observe that the values of the function are all over the space and hence the solver has not been specified a given search space due to this it is taking random bounds, therefore in order to reduce the search we have provided some bounds so that the optimum solution can be re hed faster. we can see that in this case the optimum value comes close to 4 peaks

STUDY 3:PLOTS

In this study apart from specifying the bounds we have also increases the initial population size since the ga takes 50 as its initial size. here we have used taken the initial population size 150 to 250 with an increment of 50 with help of optimoption() by which we can change ga variables. this increases the chance of finding the optimum solution. for loop is used to increment the population size.

PLOT 3.1:

PLOT 3.2:

PLOT 3.3

PLOT 3.3 with label

OBSERVATIONS AND CONCLUSION:

1) We can observe from the above figures how the solver converges to an optimal solution. when we look at the subplot of study 1 we can see in the iterations plot that the so many numbers of optimum values are there this is due to the infinite search space and hence in to help in the convergence, we can observe in the study 2 plot that the number of optimum values decreases but still there are 4 maximum values obtained and hence to increase the convergence we have increased the initial population size as seen in study 3, in this the population size is varied from 150 to 250 with an increment of 50 and from this, we can observe that the optimum value obtained is when the population size of 200 and 250 is taken.

2) If we compare the study time of all 3 cases study 1 gives the shortest study time but most uncertain soliton values are obtained and the study 3 has taken the longest time but has given an optimum solution study 2 falls in between the study 1 and 3 in which it increases the convergence considerably but still fluctuation values are obtained in it.

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.