Challenge

Week 4.1 - Genetic Algorithm

AIM

To write a code in MATLAB to optimisze the stalagmite function and find the global maxima of the function

METHODS

OPTIMISATION

It is basically an area of study that deals with how to minimize or maximize a particular function which depends on different input parameters. Here we get optimum output depending upon the constraints or some criteria. It is a process which executed iteratively by comparing various solutions until an optimum or satisfactory solution is found.

STALAGMITE FUNCTION

Stalagmite function is a mathematical model which help to generate maxima and minima. It has local maxima and minima. Also, it has global maxima and that is the objective of the study.

GENETIC ALGORITHM

Genetic algorithm is based on random sampling, that means, we start with random input based on input we evaluate the function, then comes the concept of natural selection, where we take two-generation we basically mutate them, do cross over then we create a next-generation which is hopefully better.

Genetic algorithm function which is in MATLAB always minimize the function.

Syntax for GA:

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

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

X = ga(fun,nvars,A,B,[],[],lb,ub,nonlcon,Intcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

study 1 : unbounted statistical Behaviour

The range of the genetic algorithm is unbounded and hence the values of global maxima and the values of x and y can be out of the defined search space in inputs

Use Code [inputs fopt) ga(fun,nvars)

here, inputs represents the values of parameters x and y for which the function has the least value (fopt)

Fun - the function to be optimized, in this case Stalagmite

nvars - the number of design variables, in this case since only x and y are the variables, 2 is to be inputted

Study 2: Bounded Statistical Behaviour
The range of the genetic algorithm is Bounded by introducing values for lower and upper bounds and hence the values of global maxima and the values of x and y will be within the defined search space in input

Use Code : [inputs fopt] = ga(fur , nvars, A, Aeq, beq, lb, ub)

inpuut fopt, fun and mars are the same as Study 1

A Linear inequality constraints real matrox, if there are none, then

D-Linear inequality constraints real vector, if there are none then

Aeg - Linear equality constraints real matrix, if there are none, then

beq - Linear equality constraints real vector, if there are none, then

lb - Lower bounds, specified as a real vector or array

ub - Upper bounds, specified as a real vector or array

study 3 : Bounded Statistical Reha Your with Options Optimized
The range of the genetic algorithm is Bounded as in Study 2 but to improve the accuracy, the commands optimoptions is introduced to increase the Population Size

Use Code ga(fun nvars A b Aeq beq lb ub nonicon IntCon options)

inputs, fopt fun and nvars A b Aeq beq lb ub are the same as Study 1 and Study 2 respectively

nonicon-Nonilinear constraints, specified as a function handle or function name. If there are none, then

IntCon - integer variables, specified as a vector of positive integers, if there are none, then

Options Optimization options, specified as the output of optimoptions or a structure.

Determining the Maxima Using ga-function

The function ga in matlab determines only the global minima of the function.To determine the maxima, it is ideal to minimize the function to be optimized by introducing a (negative) sign infront of the function Therefore, when running the function go, it will determine the global minima of the function which is actually the global maxima

Plotting and Displaying the Maxima Value

While plotting the graphs using surfc (surface contour), plats (3 dimensional Plotting), plot and also while displaying result with fprintf, it is ideal to represent the value of the function with a (negative) sign, thereby making the plot or value to appear in positive direction as is the requirement. Also, since a surface contour plot and a regular plot are required to be shown in same figure window, Command subplot is to be used.

Program 1:Function for Stalagmite

function[f]=stalagmite(input_vector)

x=input_vector(1);
y=input_vector(2);

ax=(sin(5.1*pi*x+0.5))^6;
ay=(sin(5.1*pi*y+0.5))^6;
bx=exp(-4*log(2)*((x-0.0667)^2/0.64));
by=exp(-4*log(2)*((y-0.0667)^2/0.64));
f=-(ax*bx*ay*by);
end


Explanation of Program:

The function is declared with the name Stalagmite
Two inputs variables (x and y) are considered
Based on the above formula each individual polynomial is taken into the account name ax, ay, bx & by
Finally the -ve sign has been put optimize stability for the Ga function (taking minimum value in the account)

Main program to find the global maximum values:

clear all
close all
clc

% Defining the Search Space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases=50
% Creating a 2d dimensional mesh
[xx, yy] = meshgrid(x,y);
% Evaluating the stalagamite 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

tic

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

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',10,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Fuction Maximum')


%Study2 statistics
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
study2_time=toc

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',10,'markerfacecolor','r')
title('Bounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Fuction Maximum')

%Study3 statistics

options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',150);
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)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',10,'markerfacecolor','r')
title('Bounded Inputs')
subplot(2,1,2)
plot(-fopt)

xlabel('Iterations')

ylabel('Function Minimum')

code explanation

The general search space has been defined between 0 to 0.6.
The Red pointers (shown in the graph ) are node points confined within the search brackets
The stalagmite function is being called to find and optimize the given function.
The graph is set (surfc) command is used for the shaded and Mountain representations of the peak values.
The ga function is now used to find the global maximum value in the function.
Important: Here as shown in the program the f output is kept in -ve, this is because Ga peaks the minimum value which is difficult to peak in the already negative function thus by putting the -ve sign we are reversing the graph plotting so as to get the maximum value easily from the +ve output.
With three statistics involved the increasing number of iterations gives the output.

Conclusion:

Thus the program for finding the global maxima value for the given function is created and increasing the number of iterations is decreasing the search space for the right value also the ga function is utilized and made to understand by providing necessary examples.

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.