Challenge

Week 4.1 - Genetic Algorithm

Aim: Finding global maxima in stalagmite function using genetic algorithm function in matlab.

Procedure:  

  1. First we will write ‘close all’, ‘clear all’, ‘clc’ to reset command center and workspace.
  2. Now we will define our workspace by creating matrix for x and y axis
  3. X and Y axis will be 0.6 by 0.6 and each axis will be divided into 150 steps by using linspace command which will create matrix of 1 row and 150 columns.
  4. Then we will create different variable called xx and yy in which we will create 150 by 150 matrix using meshgrid command so that it can be used in ‘surfc’ command to create our stalagmite result.
  5. Now we will create stalagmite function.
  6. Given formulas:
    `f(x,y) = f_(1x)xxf_(2x)xxf_(1y)xxf_(2y)`

    `f_(1x) = [sin(5.1pix + 0.5]^6`
    `f_(1y)[sin(5.1piy+0.5)]^6`
    `f_(2x) = exp[-4ln(2)frac{(x-0.0667)^2}{0.64}]`
    `f_(2y) = exp[-4ln(2)frac{(y-0.0667)^2}{0.64}]`
  7. First we will create a for loop to loop through xx and yy matrix and we will store every data in single matrix called input vector for once (data will reset at every loop)
  8. Now we will create a stalagmite function using above formula and function input will be input vector which will be used for z axis in surfc command and will save this data in ‘F’ variable.
  9. Now we will understand how genetic algorithm function.
  10. A genetic algorithm is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution.
  11. The algorithm repeatedly modifies a population of individual solutions.
  12. 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.
  13. Over successive generations, the population ‘evolves toward an optimal solution.
  14. Ga syntax:
    1. ga(fun,nvars): finds a local unconstrained minimum, to the objective function, fun. nvars is the dimension (number of design variable) of fun.
    2. ga(fun,nvar,A,b): finds a local minima x to fun, subject to the linear inequalities A*x<=b. ga evaluates the matrix product A*x as if x is transposed (A*x’).
    3. ga(fun,nvars,A,b,Aeq,beq): find a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*X<=b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed(Aeq*x’).
    4. 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.)
    5. x = ga(fun,nvar,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 function such that C(x) <= 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exists.)
    6. x = (ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options): minimizes with the default optimization parameters replaced ny values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimotions.
  15. Now we will find the global maxima by using genetic algorithm function and optimizing it.
  • As ga gives global minima NOT maxima so we will invert our stalagmite graph (turn it upside-down) by multiply the final equation by ‘-‘ sign which will give us global maxima of the stalagmite.
  1. Test 1: Unbound inputs
    1. We will create a for loop to loop through 50 iterations.
    2. Now we will use ga function and use first two index which ih function and no. of output.(in our case it will be x and y axis)
    3. We will save the result in ‘inputs’ matrix which will be x and y axis and ‘fopt’ which will be z axis.
    4. And we will save x and y value in ‘xopt’ and ‘yopt’ respectively.
    5. These will be used in graph representation later.
  2. Test 2: Bound inputs
    1. Now we will add boundary limits by using ‘lb’ and ‘ub’ syntax.
    2. First use ‘[]’ for empty syntax as we don’t know their value.
    3. Now we will pu’0,0’ as ‘lb’ and ‘0.6,0.6’ as ‘ub’.
    4. Rest in for loop will be the same.
  3. Test 3: Optimization
    1. Now we will optimize the ga function by using optimotions command.
    2. We will create a variable ‘options’ and will save the output optimotion command.
    3. In optimotion command we will change the population size to 170. (Default is 50)
    4. Now we will put ‘options’ variable in ‘options’ syntax. (Use ‘[]’ for blank syntax.
  4. Now we will plot a graph showing stalagmite function and global maxima by following steps.\
    1. First we will use ‘subplot’ command to put two graphs in one window.
    2. In first graph we will put stalagmite graph and the global maxima plot by ga function.
    3. In second plot we will plot ‘fopt’ i.e. z-axis of maxima predicted by ga against iterations and label the plot.
    4. Now we will get to see are end result.

Conclusion:

  1. We can optimize ga to find out global maxima in stalagmite function and learn more about how ga works.
  2. Errors occurred while coding:
    1. Ga function was not receiving initial inputs due to error in stalagmite function where I was using wrong function inputs type.(error code and code screen shot are attached below)
clear all
close all
clc

% Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;

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

%Evaluating the stalagmite 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

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

study_time = toc;

figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,F)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded input')
subplot(2,1,2)
plot(fopt)
xlabel('Interations')
ylabel('Function minimun')


--------------------------stalagmite function code-----------------------------

function [f] = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);
f1x = (sin(5.1 * 3.14 * x + 0.5)).^6;
f1y = (sin(5.1 * 3.14 * 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);
f = -(f1x * f2x * f1y * f2y);

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