Challenge

Week 4 - Genetic Algorithm

Aim : Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

concept of 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.

Stopping criteria:

We usually want a termination condition such that our solution is close to the optimal, at the end of the run. When there has been no improvement in the population for X iterations. When we reach an absolute number of generations. When the objective function value has reached a certain pre-defined value.

syntax for ga :

f_min = ga(@function,nvars,A,B,Aeq,Beq,LB,UB,nonlcon,intcon,options)

Explaination of terms involved are following :

  1. f_min is the local minima point.
  2. @function is the function to be called.
  3. nvars indicates the no of variables used.
  4. A and B are used to indicate any kind inequalities that exist in  the function.
  5. Same goes with Aeq and Beq.
  6. LB = lower bound.
  7. UB = upper bound.
  8. nonlcon gives a two vectors representing inequalities and equalities.
  9. options is used to perform additional operations like increasing population size. This is done with the help of optimooptions.

Code for the stalagmite Function :

function [F] = stalagmite(input_vector)
 x = input_vector(1);
 y = input_vector(2);

 f1=(sin(5.1*pi*x + 0.5)).^6;
 f2=(sin(5.1*pi*y + 0.5)).^6;
 f3= exp((-4*log(2)*(x-0.0667).^2)/(0.64));
 f4= exp((-4*log(2)*(y-0.0667).^2)/(0.64));
 F = -f1*f2*f3*f4;
end

Code Explanation :

This stalagmite function will be use in main program.

and required input values are assigned to x and y

and minus sign in F is for maximize the stalagmite function.

Code for finding Global maxima using ga function. 

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 the 2D mesh
[xx, yy] =meshgrid(x,y);

%evaluating 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

%study1 : statistical behaviour

tic 
for i=1:num_cases
[inputs , f_opt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end

study_1_time = toc

%plotting

figure (1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,f_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(f_opt)
xlabel('Iterations')
ylabel('Function maximum')

%Study 2 for statistical behaviour- upper and lower limits

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

study_2_time = toc

%plotting
figure (2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,f_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(f_opt)
xlabel('Iterations')
ylabel('Function maximum')

%study 3  increasing the size of population or GA iterations
 
options = optimoptions('ga');
options = optimoptions(options,'populationsize',200)

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

study_3_time = toc

%plot
figure (3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,f_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with modified population size')
subplot(2,1,2)
plot(f_opt)
xlabel('Iterations')
ylabel('Function maximum')

Code Explanation :

first , we are defining the search space using linspace function. but remember that the ga command not consider the search space.

then using meshgrid function , creating the 2D mesh.

then evaluating stalagmite function using for loops.

here we are doing 3 studies for getting Global maxima point.

study1 : observing statistical behaviour 

here we are using ga function which gives inputs(x and y optimum) for the Function optimum .

then assigning x and y optimum values.

for plotting we are using subplot , surfc , plot3 functions and getting Unbounded inputs plot and Fuction optimum (Iterations vs Function maximum) plot.

results :

Study 2 for statistical behaviour- by defining upper and lower limits

results :

study 3 increasing the size of population or GA iterations

Results: 

Conclusion :

For best solution we have to define the limits and increase the population size

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.