Challenge

Week 4 - Genetic Algorithm

Genetic Algorithm:

A genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutations, crossover and selection.

Objective: Find out the maximum value of the stalagmite function by using the Genetic algorithm.

Stalagmite function is built as follows

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));

f = (f1x*f1y*f2x*f2y);

The code for stalagmite in MATLAB is given below:

function [f] = stalagmite(input_vector) %inbuilt function
x = input_vector(1);
y = input_vector(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);

f = -(f1x.*f1y.*f2x.*f2y)
end

In this case, 3 studies are taken into account to find out the global maximum by using different population size for each study.

Initial Study

clear all
close all
clc

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

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


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
surfc(f);
xlabel('x value')
ylabel('y value')
shading interp

%Genetic algorithm function
[inputs, fval] = ga(@stalagmite,2);  

Study 1: Population size of 150 with 50 iterations.

%Study1 - Statistical behavior
tic

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

study1_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('unbound inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
hold off

Study 2: Population size of 200 with 100 iterations

%Study2 - Statistical behavior with upper and lower bounds

num_cases = 100;
options = gaoptimset('ga');
options = gaoptimset(options,'populationSize',200);
tic

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

study2_time = toc;

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

Study 3: Population size of 250 with 100 iterations

%Study3 - Increasing CA iterations

num_cases = 100;
options = gaoptimset('ga');
options = gaoptimset(options,'populationsize', 250);
tic

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

study3_time = toc;

figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with increased population size')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
hold off

Program Explanation:

1. The objective is to find the global maxima. We need to call the stalagmite function which is inbuilt in MATLAB.

2. x and y are variables.

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

3. Number of iterations: 50 for study 1, 100 for study 2.

4. surfc command to view graph in 3d by giving 3 inputs.

5. subplot is used to show two graphs in one figure.

Subplot syntax used to plot 2 graphs in a single frame, (2,1,2)- means 2 rows,1 column, and position is 2

6. tic and toc command to show the run time.

7.

Study 1:

Syntax:-

x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function,

fun. nvars is the dimension of fun.

Study 2:

Syntax:-

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.

Study 3:

Syntax:-

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options.

Result:

Global maximum, x=0.06846,y=0.0604

Errors:

1.

stalagmite function was not defined

2.

spelling mistake.

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.