Project

Stalagmite Function Maximization using GA

Introduction:

Genetic Algorithm is derived from Charles Darwin's theory of evolution which states that ' It is not the strongest of the species that survives, nor the most intelligent , but the one most responsive to change.' The entire concept of Genetic Algorithm is based on the above famous statement.

Algorithm is a process of natural selection that belongs to a larger class of evolutionary algorithms. It is a method used to generate high-quality solutions to optimization and search problems by relying on bio-inspired operators such as mutation, crossover and selection.

So to formalize a definition of a genetic algorithm, we can say that it is an optimization technique, which tries to find out such values of input so that we get the best output values or results.

The working of a genetic algorithm is also derived from biology, which is as follows,

1) Initialization.

2) Fitness Assignment.

3) Selection.

4) Cross Over.

5) Mutuation.

6) Stopping Criteria.

Some advantages of genetic algorithms this method are the following:

1) They usually perform better than traditional feature selection techniques.

2) Genetic algorithms can manage data sets with many features.

3) They don't need specific knowledge about the problem under study.

4) These algorithms can be easily paralelized in computer clusters.

Working of Genetic Algorithm:

The following outline summarizes how the genetic algorithm works:

1) The algorithm begins by creating a random initial population.The algorithm then creates a sequence of new populations.

2) At each step, the algorithm uses the individuals in the current generation to create the next population. To create the new population, the algorithm performs the following steps:

a) Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.

b) Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.

c) Selects members, called parents, based on their expectation.

d) Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.

e) Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.

e) Replaces the current population with the children to form the next generation.The algorithm stops when one of the stopping criteria is met.

Note,

Genetic Algorithm is only used to generate minimum solution. So the value of stalagmite function is taken negative to maximise the function.

Syntax For ga :

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)

where,

1) x=optimum solution/variables.

2) no_of_variables=Number of variables, specified as a positive integer.

3) A,b= Linear inequality constraints.

4) Aeq,beq=Linear equality constraints.

5) lb,ub

lb=Lower bounds ub=Upper bounds

6) nonlcon : Use for nonlinear constraints.

7) IntCon : to get solution in integer.

8) options: Minimize with non default options.

Matlab Code:

Stalagmite Function:

function [F] = stalagmite(input_vector)
         x=input_vector(1) ;
         y=input_vector(2) ;
         
         f1_x = (sin(5.1*pi*x+0.5)).^6;
         f1_y = (sin(5.1*pi*y+0.5)).^6;
         f2_x = exp(-4*log(2)*((x-0.0667).^2)/0.64);
         f2_y = exp(-4*log(2)*((y-0.0667).^2)/0.64);
         [F] = -(f1_x.*f1_y.*f2_x.*f2_y);
         end
%Creating x and y array
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;

%Creating two dimensional mesh
[xx,yy]=meshgrid(x,y);

%Calculating the stalagmite function
for i=1:length(xx)
   for j=1:length(yy)
       a(1)=xx(i,j);
       a(2)=yy(i,j);
      f(i,j)=stalagmite(a);
       
   end
end


%Study-1
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study1_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
title('Unbounded inputs')
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')

%Study-2
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
title('Bounded inputs')
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')

%Study-3
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',170)
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
fopt=-fopt;
f=-f;
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')
max_value=fopt
Stalagmite function code:
function [F]=stalagmite(a)
      x = a(1);
      y = a(2);

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

Output:

Study 1

Study 2

Study 3

Report:

1) Initially create a x and y array and number of iterations you want.

2) Then create xx and yy array by using meshgrid command.

3) By using nested for loop, calculate the stalagmite function and store the value in f. Here we use -ve sign in stalagmite function, because we calculate the global maxima.

4) Then you find out all the three studies.

5) Study-1, it calulate the global maxima by taking random samples from the function. Here we use tic-toc command for calculating the time taken for study-1.

6) Syntax for ga in study-1 is [inputs,fopt(i)]=ga(@stalagmite,2);

7) Study-2, it calculate the global maxima by giving boundary limits for the inputs.

8) Syntax for ga in study-2 is [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);. In that you see the boundary limits 0 to 1.

9) Study-3, it calculate the global maxima by giving population size by which we can find the specific optimum point.

10) Syntax for ga in study-3 is [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);. Here, options=optimoptions('ga') options=optimoptions(options,'PopulationSize',170) In that the population size given is 170. The maximum value of stalagmite function is +1 obtained.

THANK YOU!!!

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.