Challenge

Week 4 - Genetic Algorithm using MATLAB

AIM: Write code in MATLAB to optimize the stalagmite function and find the global maxima of the function.

OBJECTIVE:

  • Clearly explain the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
  • Plot graphs for all 3 studies and for F maximum vs no. of iterations.

GENETIC ALGORITHM:

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, nondifferentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed-integer programming, where some components are restricted to be integer-valued.

The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:

  1. Selection rules select the individuals, called parents, that contribute to the population at the next generation.

  2. Crossover rules combine two parents to form children for the next generation.

  3. Mutation rules apply random changes to individual parents to form children.

  • SYNTAX:

General syntax:

  • x = ga(fitnessfcn,nvars) ;finds a local unconstrained minimum, x, to the objective function, fitnessfcn. nvars is the dimension (number of design variables) of fitnessfcn.
  • x = ga(fitnessfcn,nvars,A,b) ;finds a local minimum x to fitnessfcn, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
  • x = ga(fitnessfcn,nvars,A,b,Aeq,beq) ;finds a local minimum x to fitnessfcn, subject to the linear equalities Aeq*x = beq as well as 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').
  • x = ga(fitnessfcn,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.)
  • x = ga(fitnessfcn,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 fitnessfcn such that C(x) ≤ 0 and Ceq(x) = 0. (Set LB=[] and UB=[] if no bounds exist.)
  • x = ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon,options) ;minimizes with the default optimization parameters replaced by values in the structure options, which can be created using the gaoptimset function. (Set nonlcon=[] if no nonlinear constraints exist.)
  • x = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon) ;requires that the variables listed in IntCon take integer values.

Note: When there are integer constraints, ga does not accept linear or nonlinear equality constraints, only inequality constraints.

  • x = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon,options) ;minimizes with integer constraints and with the default optimization parameters replaced by values in the options structure.
  • x = ga(problem) ;finds the minimum for problem, where problem is a structure.
  • [x,fval] = ga(fitnessfcn,nvars,...) ;returns fval, the value of the fitness function at x.
  • [x,fval,exitflag] = ga(fitnessfcn,nvars,...) ;returns exitflag, an integer identifying the reason the algorithm terminated.
  • [x,fval,exitflag,output] = ga(fitnessfcn,nvars,...) ;returns output, a structure that contains output from each generation and other information about the performance of the algorithm.
  • [x,fval,exitflag,output,population] = ga(fitnessfcn,nvars,...) ;returns the matrix, population, whose rows are the final population.
  • [x,fval,exitflag,output,population,scores] = ga(fitnessfcn,nvars,...) ;returns scores the scores of the final population.

Syntax used in code:

  • [inputs, fopt(i)] = ga(@stalagmite,2) ; Unbounded inputs syntax where, 2 - indicate no. of variables & @stalagmite - is objective function.
  • [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0 0],[0.6 0.6]) ; Bounded inputs syntax where, [0 0] - lower bound & [0.6 0.6] - upper bound.
  • [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0 0],[0.6 0.6],options) ; Bounded inputs with increased population size for iteration where, options - set optimization options

GOVERNING EQUATION:

  1. `f_(1,x )= [sin⁡(5.1πx+0.5)]^6`
  2. `f_(1,y )= [sin⁡(5.1πy+0.5)]^6`
  3. `f_(2,x) = exp(-4log(2)(x-0.0667)^2/0.64)`
  4. `f_(2,y) = exp(-4log(2)(y-0.0667)^2/0.64)`

Here, x & y are input coordinates.

CODE:

  • MATLAB software is used for programming.
  • Main Code
%Write a program to maximize stalagmite function using genetic algorithm
clear all
close all
clc

%Input x and y values for stalagmite function 
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

% Creating mesh of provided x and y values to form a rectangular 2D grid
[xx yy] = meshgrid(x,y);

% Providing input values to fuction 
% 1st for-loop 
for i = 1:length(xx)
    % 2nd for-loop containing input-vectors 
    for j = 1:length(yy)
        input_vector(1) = xx(i,j);
        input_vector(2) = yy(i,j);
        f(i,j) = stalagmite(input_vector); % function values array
    end
end

% 
interval = 50;    
% Study 1 - Statistical behaviour
tic % to calculate study time
for i = 1:interval
    [inputs, fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study1_time = toc

figure(1)
subplot(2,1,1)   %subplot containing the function Vs iteration plot
hold on
surfc(x,y,-f)    % to plot surface of the fuction with global and local maximas 
shading interp   % to turn of grid over surface and show smooth surface plot
% plot3 command to plot function optimum values with respect to x and y
% optimum values(coordinates)
plot3(xopt,yopt,-fopt,'-o','MarkerSize',5,'markerfacecolor','m')
title('Unbounded Inputs')
subplot(2,1,2)
plot(-fopt)     % to plot function optimum values 
xlabel('Iterations')
ylabel('Function maximum')

%Study 2- statistical behaviour with upper and lower bounds
tic
for i = 1:interval
    % lower bound provided as [0:0] and upper bound as [0.6;0.6]g
    % Bound provide limit within which ga done minimization of function
    [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end  
study2_time = toc

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)  % -f because to plot in +ve z- direction
shading interp
% to find maximum fopt '-fopt' is used
plot3(xopt,yopt,-fopt,'-o','MarkerSize',5,'markerfacecolor','r')
title('Bounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')

%Study 3- statistical behavior with upper and lower bounds
%%Increasing GA iterations
% ga is not assigned as a solver in optimization options
% Using gaoptimset to create genetic algorithm options structure 
options = gaoptimset(@ga)  % Assing Genetic algorithm as solver
% Increasing iterations through 'PopulationSize' command
options = gaoptimset(options,'PopulationSize',200,'MutationFcn',{@mutationadaptfeasible})

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

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'-o','MarkerSize',5,'markerfacecolor','g')
title('Bounded Inputs with increased GA iterations')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')
axis([0 60 0 1.5])
  • Stalagmite function code:
%Stalagmite function
function [F] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);

f1_x = [sin(5.1*pi*x + 0.5)].^6;
f1_y = [sin(5.1*pi*y + 0.5)].^6;

f2_x = exp(-4*log(2)*(x - 0.0667).^2/0.64);
f2_y = exp(-4*log(2)*(y - 0.0667).^2/0.64);
f = (f1_x.*f2_x.*f1_y.*f2_y); % main function
% to find maxima to function f, minima of -f that is F will be calculated
% because ga can only find minima of a function
F = -f;
end  
  • PLOTS:
  • PLOT-1: Unbounded inputs
  • PLOT-2: Bounded input with lower and upper bounds
  • Lower and upper bound will give limit to genetic algorithm to optimize the function within bounded inputs and find maxima within limit.
  • PLOT-3: Bounded inputs with increased iteration values
  • Increased iteration size to find global maxima.




REFERENCEs:

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.