Challenge

Week 4 - Genetic Algorithm

Aim:

To understand the concept of genetic algorithm and Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

Solution:

Genetic Algorithm:

A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents 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 problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.

some of the GA syntax are shown below and those are used in the coding for optimization,they are as follow,

1. x = ga(function,nvariables) finds a local unconstrained minimum, x, to the objective function, nvariables is the dimension (number of design variables) of function.

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

3. 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.

then moving on to make a matlab code for global maxima is shown below

close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_c = 50;
[xx ,yy] = meshgrid(x,y);

% initial for condition
for i = 1:length(xx)
    for j = 1:length(yy)
        input_v(1) = xx(i,j);
        input_v(2) = yy(i,j);
        f(i,j) = stalagmite(input_v);
    end
end

%1st study condition

tic
for i = 1:num_c
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
time_1 = toc

figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'Marker','o','markersize',3,'MarkerFaceColor','r')
xlabel 'x-axis'
ylabel 'y-axis'
subplot(2,1,2)
plot(fopt)
xlabel('iterations');
ylabel('Function maximum');
%study 2
tic
for i=1:num_c
  [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);
  xopt(i)=inputs(1);
  yopt(i)=inputs(2);
end

time_2=toc

figure(2)
subplot(2,1,1);
hold on
surfc(xx,yy,f);
shading interp;
plot3(xopt,yopt,fopt,'marker','o','markersize',3,'markerfacecolor','r');
xlabel 'x-axis'
ylabel 'y-axis'
subplot(2,1,2);
plot(fopt);
xlabel('iterations');
ylabel('Function maximum');

%study 3
options= optimoptions('ga');
options= optimoptions(options,'PopulationSize',500);

tic
for i=1:num_c
  [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1],[],[],options);
  xopt(i)=inputs(1);
  yopt(i)=inputs(2);
end

time_3 = toc

figure(3)
subplot(2,1,1);
hold on
surfc(xx,yy,f);
shading interp;
plot3(xopt,yopt,fopt,'marker','o','markersize',3,'markerfacecolor','r');
xlabel 'x-axis'
ylabel 'y-axis'
subplot(2,1,2);
plot(fopt);
xlabel('iterations');
ylabel('Function maximum');

First step in the code is defining the X and Y values with an interval of 0.6,then a limit is aet upto 150 values for both X and Y values.

then fixing a limit for optmization as 50 and then using the for loop the stalagmite function is used stalagmite function is shown below,

function [stalag] = stalagmite(input_v)
    x = input_v(1);
    y = input_v(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));
    stalag = -(f1x * f2x * f1y * f2y);
end

this function is compraises the following equation they are as follow

then , study 1 starts with a comman command called tic which is going to count the time for the iteration and then a for loop which was given by the ga function after at the end of first loop then move on to the results by using a common command called plot, plot3 and surfc so that we can getboth the surface plot and normal plot

plot3:

plot3(X,Y,Z) plots coordinates in 3-D space.

  • To plot a set of coordinates connected by line segments, specify X, Y, and Z as vectors of the same length.

  • To plot multiple sets of coordinates on the same set of axes, specify at least one of X, Y, or Z as a matrix and the others as vectors.(reference : MATHLAB Documentation.

the final graph of the study 1 is shown below;

here the stalagmite function used is [inputs,fopt(i)] = ga(@stalagmite,2); and it is explained above.

then ,study 2 starts with a comman command called tic which is going to count the time for the iteration and then a for loop which was given by the ga function after at the end of first loop also a tac command is used to stop counting and it is display in a pre-defined variable called time_2, then move on to the results by using a common command called plot, plot3 and surfc so that we can getboth the surface plot and normal plot.

and the obtained plot is shown below,

then ,study 3 starts with a comman command called tic which is going to count the time for the iteration and then a for loop which was given by the ga function after at the end of first loop also a tac command is used to stop counting and it is display in a pre-defined variable called time_3, then move on to the results by using a common command called plot, plot3 and surfc so that we can getboth the surface plot and normal plot,

Result:

Finally based on the understanding of ga expanded as Genetic Algorithm and made a code for optimization of ga and found the global maxima from the graph.

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.