Challenge

Week 4 - Genetic Algorithm

To calculate the global maxima of a stalagmite function

AIM

To write a program to optimize the stalagmite function and find the global maxima of the function

GENETIC ALGORITHM

Genetic algorithm is a method used to solve both constrained and unconstrained optimization problems based on natural selection. It repeatedly changes the population of individual solutions. At each step genetic algorithm uses the individuals at random value of the current populations as parents to produce the children of the next generation.

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

FUNCTION TO BE OPTIMIZED:

Explaing Main program:

1) GA is based on random sampling its because if we input random values and it come in the concept of random selection. Where we take two generation and we mutate them also do cross over.

2) Every time we run the code we get different ga results.

3) Search space: We want GA to take x and y as search space but it doesnt know the values of x and y can go beyond 0.6 or below 0.

4) The num_cases is set to 50 as a counter for iterations.we can get the value to converge with less iteration and if we incresae the the iteration the time taken to get the valuse will increase.

5) Subplots is used to get multiple plots in one figure.

6) Here in plot function is creating x,y and the optimum value the GA predicts over the 50 studies. It helps to visualise how the algorythm working.

7) Tic and Toc is introduced to measure how long it takes to study.

8) In study two we have to set upper bounds and lower bounds, since in case one we know that the x and y value goes below 0 and above 0.6. In study two we want it to be between 0 and 1 by using upper and lower bounds.

9) Search space depends upon how we run our optimization or what type of problem we are solving.

10) Search space: It is a set of domain through which an algorithm searches.

11) In study three we will specify the bounds and change the initial population size, We know that GA takes random samples.

12) I have add this code fprintf('n') to get my stutime results in rows.

13) Introducing Population size: The population size field in Population options determines the size of the population at each generation. Increasing the population size enables the genetic algorithm to search more points and thereby obtain a better result. However, the larger the population size, the longer the genetic algorithm takes to compute each generation.

Note:

  • If our initial population is too small it wont be able to find the global minima and it can find the local minima which happened in study two.
  • If we have more iteration ga will run gfor long time.
PROGRAM TO OPTIMISE THE STALAGMITE FUNCTION:
close all
clear all
clc

%setting up the search spcae 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);


%creating a 2d mesh 
[xx,yy] = meshgrid(x,y);

%for salagmite function 
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

[results,fval] =ga(@stalagmite,2);
maximum = -fval

%for study 1
iterations = 50;
tic
for i = 1:iterations
  [inputs,fopt(i)] = ga(@stalagmite,2);
  xopt(i) = inputs(1);
  yopt(i) = inputs(2);
end
 study_time1 = toc
 figure(1)
 subplot(2,1,1)
 hold on 
 surfc(x,y,-f)
 shading interp
 plot3 (xopt,yopt,-fopt,'marker','o','markersize',6,'markerfacecolor','b');
 title('Unbounded Input');
 subplot(2,1,2);
 plot (-fopt)
 xlabel ('Iterations');
 ylabel ('Function Maximum');

 
 %for study 2
 iterations = 50
 tic
 for i = 1:iterations
   [input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
   xopt(i) = input(1);
   yopt(i) = input(2);
 
end

study_time2 = toc
 figure(2)
 subplot(2,1,1)
 hold on 
 surfc(x,y,-f)
 shading interp
 plot3 (xopt,yopt,-fopt,'marker','o','markersize',6,'markerfacecolor','r');
 title('bounded Input');
 subplot(2,1,2);
 plot (-fopt);
 xlabel ('Iterations');
 ylabel ('Function Maximum');
 
 %for study 3
 options = optimoptions('ga');
 options = optimoptions(options,'Populationsize',250)
 
 tic
 for i=1:iterations
 [input fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
   xopt(i) = input(1);
   yopt(i) = input(2);
 
end

study_time3 = toc
 figure(3)
 subplot(2,1,1)
 hold on 
 surfc(x,y,-f)
 shading interp
 plot3 (xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','y');
 title('bounded Input');
 subplot(2,1,2);
 plot (-fopt)
 xlabel ('Iterations');
 ylabel ('Function Maximum');

FUNCTION OF THE STALAGMITE:

function r = stalagmite(input_vector)
  t1 = sin(5.1*pi*input_vector(1)+0.5);
  t2 = sin(5.1*pi*input_vector(2)+0.5);
  t3 = (-4*log(2))/0.64;
  t4 = input_vector(1) -0.0667;
  t5 = input_vector(2) -0.0667;
  
  f1x = t1^6;
  f1y = t2^6;
  f2x = exp(t3*t4^2);
  f2y = exp(t3*t5^2);
  
  f = (f1x*f1y*f2x*f2y);
r = -f;
  
  end

OUTPUT OF THE PROGRAM:

ERRORS FACED:

  • Error in the syntax of ga.

Study 1:

6.) Here we plot the graphs, from the values we got from ga function and make the subplot to put all plots in same plot. The above plot is showing the unbounded input between the iterations and maximum function.

7.) In next we run the iteration along with upper and lower bound values and run the same for 50 iterations.

8.) f_opt command is used to get the optimum function values,x_opt(i) and y_opt(i) are the inputs value of stalagmite function.

Study 2:

9.) Now we plot the graph from the values we got from the ga function and make the subplot to put all plots in same plot. The above plot is showing the unbounded input between the iterations and maximum function.

10.) In the third study we inrease the population and define the population size 250, now in this study we get the global maxima.

11.) Now we plot the graph from the values we got from the ga function and make the subplot to put all plots in same plot.

Study 3 :

12.) In the above plot we have bounded inputs along with higher population size and here we found the global maxima at points X,Y & Z.

Result:

Thus calculating the Global Maxima of a stalagmite function using Genetic Algorithm is explained with MATLAB.

Refernces :

1. https://in.mathworks.com/discovery/genetic-algorithm.html

2.https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.html

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.