Challenge

Week 4.1 - Genetic Algorithm

Aim

To write a code in MATLAB to optimize stalagmite function and find the global Maxima of the same.

Objective

Writing Matlab code the concept of genetic algorithm & optimize the stalagmite function and find the

GLOBAL MAXIMA of the function.

Genetic Algorithm

The genetic algorithm is a method for solving both constrained and unconstrained optimization problems

that is based on natural seletion, the process that drives biological evolution.The genetic algoritham

repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects

individuals at random from the currect population to be parents and uses them produce the children for

the next generation. Over successive genetrations, the population "evolves" towards an optional solution.

You can apply the genetic algorithm to sove a variety of optimization problems that are not wll suited for

standard optimization algoriths,including problem in which the objective function is discontinuous non

differentiable,stochastic or highly non linear.The genetic alogorithm 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 genration from the

current population.

1. Selection rules select the individual , called parents,thet contribute to population at the next

generation.

2. crossover rule combine two parents to form children for the next generation.

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

Optimization

Optimization is the process of making something better.In any process we have a set of inputs and

set of outputs

Optimization refers to finding the values of input such as a way that we get the "best" output values.

The definition of the "best" varies from problem to problem , but in the mathematical terms, it refers to

maximizing or minimizing one or more objective functions by varing the input parameters

The set off all possible input values which the input can take make up the search space. In such a space

lies a point or set of points which gives the optimal solution. The aim of optimization to find the point or

set of point in the search space

Global Maxima

A global maxima, also known as an adsolute maximum the largest over all value of a set,function

etc. Over its entire range. It is impossible to construct an algorithm that will find global maximum for

an arbitary function.SSE also Global Minimum,Local Maxima,maximum.

Main Program

% Define search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50
% calculating a 2Dimensional mesh

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

% Evaluating the stalagmite function

for i = 1:length(xx)
    for j = 1:length(yy)
        input_vector(1) = xx(i,j);
        input_vector(2) = yy(i,j);
        GA(i,j) = stalagmite(input_vector);
        
    end
end

% case 1
% study1-statical behaviour
tic


for i = 1:num_cases
 [input, GA_opt(i)] = ga(@stalagmite,2);
 xopt(i) = input(1);
 yopt(i) = input(2);
end

study1_time = toc

%Plotting the graph

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-GA)
shading interp
plot3(xopt,yopt,-GA_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(-GA_opt)
xlabel('Iteration')
ylabel('Function Minium')

%% statistical behaviour - with upper & lower bounds
% tic_toc command for calculating the itration time
tic

for i = 1:num_cases
    [input, GA_opt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1]);
    xopt(i) = input(1);
    yopt(i) = input(2);
end
study2_time = toc

%plotting the Graph
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-GA)
shading interp
plot3(xopt,yopt,-GA_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-GA_opt)
xlabel('Iteration')
ylabel('Function Minium')

%%study-3
%calculating the optimum solution for 'ga' and save in options(variable)

options = optimoptions('ga');

%increasing the size of population

options = optimoptions(options,'PopulationSize',240);

%tic_toc command for calculating the itration time

tic

for i = 1:num_cases
    [input, GA_opt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = input(1);
    yopt(i) = input(2);
end
study3_time = toc

%plotting the Graph
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-GA)
shading interp
plot3(xopt,yopt,-GA_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-GA_opt)
xlabel('Iteration')
ylabel('Function Minium')

Procedure

step1:- Assigning the input values range of x & y is 0,0.6,150 values with the command of linspace for

both x & y

step2:- Mesh grid command used to create the grid size of these value 150*150 dimension.

xx=150*150

yy=150*150

step3:-Four loop are used to define the value in a range of [xx],[yy] according to that its run the defined

value.

step4:- Command with tic & toc used to calculate thee simulation time

step5:- Using shading interp so that we do not ge lines over the graph and we get a smooth plot on it.

step6:- Seperate function should be used to calculate the stalagmite and it call to the main function.

formula of using in stalagmite function

Stalagmite Function Code

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

term1 = pi*5.1*x;
term2 = pi*5.1*y;
term3 = ((x-0.0667)^2/0.64);
term4 = ((y-0.0667)^2/0.64);
term5 = (-4)*log(2);

xf1 = (sin(term1+0.5)).^6;
yf1 = (sin(term2+0.5)).^6;
xf2 = exp(term5*term3);
yf2 = exp(term5*term4);
GA = -(xf1*yf1*xf2*yf2);

end

Study1:- OUTPUT

STUDY1 time = 3.7019

From the above fig,it shows that its not clearly define the maximum values due to lot of minimum value

it has been defined

Study2: OUTPUT

STUDY2 time=2.8684

In study 2 shows that with the same procedure are followed to improve the results to find the global

maxima with lower bonds variable are provided with the help of tic and toc command used to calculate

the simulation time .The empty brackets are used to read the linear equality and inequality.

Study3: OUTPUT

STUDY time= 8.6424

In study3 we have another constraint function as options as it helps us to increase the population to

increase the size,Getting the local minima and maxima

With th help of tic and toc command used to calculate the simulation time

Errors

Conclution

From the above results we clearly see that optimizing the global maxima with various studis in thet we

need to get accurate value means we need to increase through the population options to get required

output according to the value

Reference

http://skill-lync.com/knowledgebase/genetic-algorithm-explained

http://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_introduction.htm

Google Drive link

https://drive.google.com/drive/folders/1w3TLyksg9zXtD8WlhcRi3vunKyn6_T89?usp=sharing

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.