Project

MATLAB Program to optimize the given stalagmite function using Genetic algorithm

OBEJCTIVE:- Write a MATLAB Program to optimize the given stalagmite function using Genetic algorithm.

INTRODUCTION OF GENETIC ALGORITHM(GA).

- genetic algorithm (GA) is a process of natural selection that belongs to the larger class of evolutionary algorithms(EA).

- It is based on the concept of Darwin\'s theory of evolution.

- This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.

- The process of natural selection starts with the selection of fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found.

- This notion can be applied for a search problem and to generate high-quality solutions to optimization. We consider a set of solutions for a problem and select the set of best ones out of them.

- Five phases are considered in a genetic algorithm.

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

Initial Population:-

The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.

An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).

In a genetic algorithm, the set of genes of an individual is represented using a string, in terms of an alphabet. Usually, binary values are used (string of 1s and 0s). We say that we encode the genes in a chromosome.

Fitness function:-

The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score

selection:-

The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.

Crossover:-

Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.

Mutation:-

In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability.

FLOW CHART OF GENETIC ALGORITHM

\"\"

STALAGMITE FUNCTION:-

\"\"

SYNTAX OF GENETIC ALGORITHM

x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

where:-

fun - function name
nvars - Number of variables
A - Linear inequality constraints as real matrix
B - Linear inequality constraints as real vector
Aeq - Linear equality constraints as real matrix
Beq - Linear equality constraints as real vector
lb - Lower Boundary
ub - upper Boundary
nonlcon - Non linear constraints
option - Creating optimisation option using \'optimoptions\'

MATLAN CODE

Step1:- Defining the stalagmite function

- Function named stalagmite was defined with input_vector as argument that accepts the vector passed from function call
- input_vector conatin the x and y values respectively.
- f1x,f1y,f2x,f2y and f were define as per stalagmite formula
- ve sign is used to obtain the global maxima, If we not put -ve sign value obtain is global minima.

function [f] = stalagmite(input_vector)

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

Step2:- Function call

- x and y are the array containing 0 to 0.6 in between there are 150 value by using linspace cmd
- Creat 2D array by using meshgrid cmd
- Evaluating the stalagmite function by defining the input_vector at each instant of xx and yy

clear all
close all
clc

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

%Creat 2D Array
[xx yy]=meshgrid(x,y)
    
%Evaluating the stalagmite fuction
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(xx,yy,f)
shading interp

%Genetic algoritm
[optimum_value,fvalue]=ga(@stalagmite,2)

OUTPUT

\"\"

CASE_STUDY_1:-STATISTICAL BEHAVIOUR WITH UNBOUNDED

- num_case = 50, that will run 50 iteration
- tic-toc cmd is used to find the time taken to run the case_study
- for loop run the function as 50 time and output value is stored in xopt(k) and yopt(k).
- the ga syntax used
x=ga(fun,nvars)

%Case1-statiistical behaviour
num_cases=50; %iteration=50
tic
for k=1:num_cases
    [Optimum_values,fopt(k)]=ga(@stalagmite,2);
    xopt(k)=Optimum_values(1);
    yopt(k)=Optimum_values(2);
end
study_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(\'statiistical behaviour of unbounded inputs\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'iteration\')
ylabel(\'maximum function value\')

OUTPUT

\"\"

CASE_STUDY_2:-STATISTICAL BEHAVIOUR WITH BOUNDED

- num_case = 50, that will run 50 iteration
- tic-toc cmd is used to find the time taken to run the case_study
- for loop run the function as 50 time and output value is stored in xopt(k) and yopt(k).
- the ga syntax used
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)

%Case2-statiistical behaviour - with upper and lower bound
tic
for m=1:num_cases
    [Optimum_values,fopt(m)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(m)=Optimum_values(1);
    yopt(m)=Optimum_values(2);
end
study2_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(\'statiistical behaviour of bounded inputs\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'iteration\')
ylabel(\'maximum function value\')

OUTPUT

\"\"

CASE_STUDY_3:-STATISTICAL BEHAVIOUR WITH BOUNDED

- num_case = 50, that will run 50 iteration
- tic-toc cmd is used to find the time taken to run the case_study
- for loop run the function as 50 time and output value is stored in xopt(k) and yopt(k).
- the ga syntax used
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)

- we have taken population size 250 to obtain global maxima.

%Case3-Increasing GA iterations
options = optimoptions(\'ga\');
options = optimoptions(options,\'populationsize\',250);

tic
for n=1:num_cases
    [Optimum_values,fopt(n)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(n)=Optimum_values(1);
    yopt(n)=Optimum_values(2);
end
study3_time=toc
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'statiistical behaviour of bounded inputs by increasing ga iteration\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'iteration\')
ylabel(\'maximum function value\')

OUTPUT

\"\"

CONCLUSION

-Time taken for Case_study

Study1 - 45 sec

study2 - 70 sec

study3 - 162 sec

- Global Maxima of the stalagmite function is 0.9999998

- When the population size is increase the time for study is also increase but the result will be precise.

REFERENCE

https://en.wikipedia.org/wiki/Genetic_algorithm

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.