Project

Using Genetic Algorithm to find the Global maxima of Stalagmite function

AIM

To optimise the stalagmite function and find the global maxima of the function.

OBJECTIVES

  • To optimise the stalagmite function and use the genetic algorithm in MATLAB to find the global maxima.
  • Plot graphs for all 3 studies and for maximum value of function vs no. of Iterations.

THEORY

The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, non-differentiable, stochastic, or highly nonlinear.

The following syntaxes have been used in the code for optimistaion of stalagmite function-

  1. 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.
  2. 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.)
  3. 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.

The Stalagmite function is defined as follows

where,

The three studies to be performed are-

  1. Using genetic algorithm for random sampling
  2. Using genetic algorithm with lower and upper bounds
  3. Using genetic algorithm with lower and upper bounds and increasing the population size.

CODE

Programming Language used- MATLAB

For finding the global maxima

clear all
close all
clc

%Input values
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

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

for i=1:length(xx)
  for k=1:length(yy)
    input_vector(1)=xx(i,k);
    input_vector(2)=yy(i,k);
    f(i,k)=stalagmite(input_vector);
  end
end

%Study 1(without bounds)
tic
for i=1:num_case
  [inputs,fopt(i)]=ga(@stalagmite,2);
  xopt(i)=inputs(1);
  yopt(i)=inputs(2);
end

Study_time_1=toc

figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,f);
shading interp;
ylabel('y data');
xlabel('x data');
plot3(xopt,yopt,fopt,'marker','o','markersize',10,'markerfacecolor','r');
title('Unbounded inputs');
subplot(2,1,2);
plot(fopt);
xlabel('Iterations');
ylabel('maximum value of function');

%Study 2 (with upper and lower bounds)
tic
for i=1:num_case
  [inputs,fopt(i)]=ga(@stalagmite,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;
ylabel('y data');
xlabel('x data');
plot3(xopt,yopt,fopt,'marker','o','markersize',10,'markerfacecolor','r');
title('Bounded inputs');
subplot(2,1,2);
plot(fopt);
xlabel('iterations');
ylabel('Maximum value of Function');

%Study 3( with upper and lower bounds and increased population size)
options=optimoptions('ga');
options=optimoptions(options,'populationsize',200);
tic
for i=1:num_case
  [inputs,fopt(i)]=ga(@stalagmite,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;
ylabel('y data');
xlabel('x data');
plot3(xopt,yopt,fopt,'marker','o','markersize',10,'markerfacecolor','r');
title('Bounded inputs with increase population size');
subplot(2,1,2);
plot(fopt);
xlabel('Iterations');
ylabel('Maximum value of function');

max_value=fopt

For modified function ‘stalagmite’

function f=stalagmite(input_vector)
  x=input_vector(1);
  y=input_vector(2);
  f_1x=(sin(5.1*pi*x+0.5))^6;
  f_1y=(sin(5.1*pi*y+0.5))^6;
  f_2x=e^(-4*log(2)*((x-0.0667)^2/0.064));
  f_2y=e^(-4*log(2)*((y-0.0667)^2/0.064));
  f=-(f_1x*f_1y*f_2x*f_2y);
end

OUTPUT

Plot

Study 1(without bounds)

Study 2 (with upper and lower bounds)

Study 3( with upper and lower bounds and increased population size)

The study times were

EXPLANATION

Code-

The modified Stalagmite function was defined with a negative sign, so that on using genetic algorithm, it wil give the maximum value of the function.

The three studies show how the function converges towards the global maxima as we provide upper and lower bounds and increase the initial population.

Output-

In Study 1, we can clearly see the random nature of genetic algorithm with function maximums occurring at random points all over the spectrum.

In Study 2, we see the global maxima region converge to five of the tallest peaks of the stalagmite function for the bounded region.

Study 3 was done by increasing the population size to increase the probability of converging to the global maxima as opposed to a local maximum.

ERRORS ENCOUNTERED

I had used ‘sind’ in stalagmite function instead of ‘sin’ so the following plot was obtained.

CONCLUSION

So, the stalagmite function was optimised and genetic algorithm was successfully used to find the global maxima.

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.