Challenge

Week 4.1 - Genetic Algorithm

Genetic Algorithm

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

THEORY:

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:

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

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

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

PROCEDURE:

1.Initially stalagmite function is defined in a separate script.

2.A negative sign is used in front of the function to obtain maxima of the function rather minima.

3.A nested for loop is utilized to iterate each of the elements.

4.GA is used to determine the optimal of the function.

5.Plot is generated for different cases.

The three considered are:

1.Unbounded inputs

2.Bounded Inputs-restricted with upper and lower bounds

3.Bounded inputs with options-increasing no of iterations by giving population size.

STALAGMITE FUNCTION:

MATLAB CODE:

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*f1_y*f2_x*f2_y);

end

clear all
close all 
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

[xx yy] = meshgrid(x,y);
rep = 50;

tic

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_function(input_vector);
    end
end

study_time = toc

surfc(xx,yy,-f)
shading interp

[inputs,fval] = ga(@stalagmite_function,2)
xlabel('X axis')
ylabel('Y axis')
title('Stalagmite Function')


%Study-1 Statistical Behaviour

tic

for i = 1:rep
    
    [inputs,fopt(i)] = ga(@stalagmite_function,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

study_time1 = toc

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')


%Study-2 Statistical Behaviour with Upper and Lower Bounds

tic

for i = 1:rep
    
    [inputs,fopt(i)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

study_time2 = toc

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('optimum solution')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('function maximum')


%Study-3 Increasing GA Iterations
options = optimoptions(@ga)
options = optimoptions(options,'populationsize',200);

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

study_time3 = toc

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('optimum solution')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('function maximum')

global_maxima = -fopt

study time:

RESULTS:

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.