Challenge

Week 4.1 - Genetic Algorithm

Evaluating the Stalagmite function:

clear all
close all
clc

% independent variables with proper line spaces
x=linspace(0,0.6,150);  % (Lower_bond,upper_bond,150)
y=linspace(0,0.6,150);

% Creating a 2D mesh/3D matrix
[xx,yy]=meshgrid(x,y);


num_cases=50; % Giving the Count/Number of iterations for the optimal generation
tic           % Tic starts the clock time for the function
% Evaluating a 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

% Creating a three dimensional surface
study_time=toc; % Toc stops the timer and shows the end time.
figure(1)
surfc(xx,yy,-f)
shading interp  % makes the surface looks more smoother
xlabel('X Values')
ylabel('Y Vaues')
legend('X values','Y Values')
title('Stalagmite Fuction')
saveas(1,'stalagmite.png')
% [x fval]=ga(@stalagmite,2)

% Study 1 Unbounded/random sampling inputs
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
studytime_1=toc;

figure(2)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Unbounded Inputs')
xlabel('X Values')
ylabel('Y Values')

hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','y');
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')
saveas(2,'Unbounded Inputs.png')

% Study 2 -Statistical Behaviour with upper and lower bound sampling
%(Bounded Inputs)
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end 
studytime_2=toc;

figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Bounded Inputs')
xlabel('X Values')
ylabel('Y Values')

hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','m')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')
saveas(3,'Bounded Input.png')

% Study 3-Code for upper and lower bound sampling and increase the
% population size

options=optimoptions(@ga);
options=optimoptions(options,'populationsize',500);

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
studytime_3=toc;
figure(4)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Bounded Inputs with modified population size')
xlabel('X values')
ylabel('Y values')

hold on

plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','c')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maxima')
saveas(4,'Bounded Inputs with modified population Size.png')

global_maxima=-fopt;     % Finding the global maxima

Writing a stalagmite function Code:

function A=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5))^6;
f2=exp((-4*log(2)*((x-0.0667).^2))/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2))*((y-0.0667).^2)/0.64);

A=-(f1*f2*f3*f4);
end
% We create another function file for the stalagmite function.here we take
% the output as -f since ga only minimise a given function .so if -f is
% considered it automatically gives us the maxima.

Genetic algorithms (GAs):

gentic algorithms are stochastic search methods based on the principles of natural genetic systems.They perform a search in providing an optimal solution for the evaluation (fitness) function of an optimization problem.GAs deal simultaneously with multiple solutions and use only the fitness function values.while solving an optimization problem using GAs,each solution is coded as astring of finite lenght over a finite alphabet A. each string or chromosome is considered as an individual.A collection of M such individuals is called a population.

The genetic algorithm starts with a randomly generated population of size M. During the process of iteration, a new population of the same size is generated from the current population using three basic operations on the individuals of the population.

Five phases are considered in a genetic algorithm.

  1. Initial population
  2. Fitness function
  3. Selection
  4. Crossover
  5. Mutation..

'ga' function is used to find the minimum of the function using a genetic algorithm

syntax:

  • for Random Inputs:

x=ga(fun,nvars) finds a local unconstrained minimum,x to the objective function,fun.N=nvars is the dimension of the fun.

  • for Bounded Inputs:

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

  • for Population Size:

x=ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,intCon,options) requires that the variables listed in Intcon take integervalues.

  • fun-function
  • nvars-number of variables
  • A-Linear inequality constaints
  • b-linear inequality constraints
  • Aeq-linear equality constraints
  • beq-linear equality constraints
  • lb-lower bounds
  • ub-upper bounds
  • nonlcon-linear constraints
  • IntCon-integer constraints

Code Explanation:

Intially we define the workspace and set default popuaton size to 50. A loop for the study of statistica behaviour with no bounds is created for default population size.

we use the solver 'ga' to find the minimum of the function

'ga'

ga function is used to find the minimum of the function using a genetic algorithm

Surfc

surfc function contours the plot under the surface plot.surfc(x,y,z) creates a three dimensional surface plot with contour plot undernearth.

Shading

shading function controls the color shading of surface and patch graphics objects.

shading interp varies the color in each line segment and face by interpolating the colormap index or true color values across the line/face.

PLot3

plot3(x,y,z)plots coordinates in 3-D space.

TO plot a set of coordinates connected by line segments, specify X,Y and Z as vectors of the same length.

optimoptions:

options=optimoptions(solvername) returns a set of default options for the solvername solver.

options=optimoptions(oldoptions,name,value) returns a copy of oldoptions with the named parameters altered with the specified values.

tic-toc

tic works with the toc function to measure elapsed time.The 'tic' function records the current time,and the 'toc' function uses the recorded value to calculate the elapsed time.

COMMAND:

Global Maxima:

RESULTS/PLOTS:

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.