Challenge

Week 4.1 - Genetic Algorithm

AIM :

to write the code to calculate the global maxima of the stalagmite function

GENETIC ALGORITHM (GA):

this is the method for solving both constrained and unconstrained optimisation problem that is based on natural selction the process that drives biological evolution

STEP USED IN THE GENETIC ALOGRITHM(GA)

  1. initialize population
  2. seletion according to fitness
  3. crossover between the selected chromosomes
  4. perform the mutation

THE STALAGMITE FUNCTION CODE :

function[f] = stalagmite(input_vector); 
a = input_vector(1)
b = input_vector(2)

f1_x = [sin(5.1*pi*a+0.5)]^6
f1_y = [sin(5.1*pi*b+0.5)]^6
f2_x = exp(-4*log(2)*(((a-0.0667)^2/0.64)));
f2_y = exp(-4*log(2)*(((b-0.0667)^2/0.64)));

f = -(f1_x*f1_y*f2_x*f2_y)


end

main code

clear all
close all
clc

x= linspace(0,0.6,150);
Y = linspace(0,0.6,150);
  num_cases = 50;
 [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
 figure(1)
 surfc(xx,yy,-f)
 shading interp
 xlabel('x value')
 ylabel('y value')
 title('stalagmite function')
 

 %study 1
  for i= 1 : num_cases
      [input,fopt(i)] = ga(@stalagmite,2);
      xopt(i) = input(1);
      yopt(i) = input(2);
      
  end
 
 figure(2)
 subplot(2,1,1)
 hold on 
 surfc(x,y,-f)
 title('unbounded_input')
 xlabel('x value')
 ylabel('y value')
 
 
 shading interp
 hold on 
 
 plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
 title('unbounded_input')
 subplot(2,1,2)
 plot(fopt)
 x_label = ('intrations');
 y_label = ('function minimum');
 
 
 tic;
 %case 2nd with upper and lower bounds
 
 for i = num_cases
     [input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
     xopt(i) = input(1);
     yopt(i) = input(2);
 end
 
 study_2_time = toc;
 figure(3)
 subplot(2,1,1)
 surfc(x,Y,-f)
 shading interp
 
 title('bounded inputs')
 xlabel('x value')
 ylabel('y value')
 hold on
 plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('interations')
 ylabel('function maximum')
 %study 3 icresing the population size
 
 options = optimoptions(@ga);
 options = optimoptions(@ga,'populationsize',500);
 
 tic; 
 
 for i = 1 : num_cases 
     [input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
     xopt(i) = input(1);
     yopt(i) = input(2);
 end
 study_3_time = toc;
 figure(4)
 subplot(2,1,1)
 surfc(x,y,-f)
 shading interp
 title('bounded inputs with increased population size')
 
 xlabel('x value')
 ylabel('y value')
 hold on
 plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('interations')
 ylabel('function maximum')
  1. clear all - remove data from the workspace and freeing up system memory
  2. close all -delete all the current figure or the specified memory
  3. clc - clear all the input and output variables
  4. the linspace set the range of data between the two number
  5. num_case is used number for interation in the algorithm
  6. the meshgrid command is used to create an array of the m*n martrix
  7. the for command is used for loop operation. the input vector stored the value of matrix xx and yy the output stored in the f martix after evaluted from the stalagamite operation surfc(xx,yy,-f)
  8. surfc(x,y,z) creates a three dimensional surface plot with a contour plot underneath a surface plot is a three dimesional surface that has solid face colors .the function plots the values in martix z as heights above a grid in the x - y plane defined by x and y the color of the surface varies according to the heights specified by z
  9. the label is used to name the axis in the figure shading interp
  10. the shading interp is used to hide the grid plot and smooth the [input fval] = ga(@stalagmite,2);
  11. tic toc command is used to calculate the time of running command

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.