MATLAB Project 1:- Programming Genetic Algorithm using MATLAB
Stalagmite Mathematical Model
Aim:- To solve the global optimization and determine the global maxima for the given function.
Principle:- Genetic Algorithm which is based on the Evolution theory of Charles Darwin.
Applications :-
-IC Engines
-Airfoil(lift of aeroplane) to have the minimum drag force for varying angle of lift.
(Generally we use Brut force method to determine the drag force by varying the various parameters corresponding to angle of lift which consumes more time)so we choose optimization.
Optimization in simple terms deals/defines with minimizing or maximizing a particular function depending on the input parameters.
'ROBUSTNESS' - a designed part has to be highly robust(strength,efficiency,life span and so on...)
Steps in Genetic Algorithm:-
-Generating Initial Random Population
-Evaluating the fitness function
-selection
-cross-over
-mutation
Genetic Algorithm operators
-Selection
-Cross-over
-Mutation
Initial Random Population & Evaluating the fitness function: -
x & y are individual parameters
f is the variable characterized by the individual parameter(x and y) obtained by defining the function.
fval-solution.
(As per program below)
Comparing with Darwin theory of Evolution:
x & y are the parents
f is the Genes from parents
fval is the chromosomes.
(chromosomes/genotypes are generally written in binary and later encoded to integer)
(x,y,f and fval As per program below)
Selection:-
- selecting 2 or more parents from random population for crossing.
- The results should have higher fitness(highly robust in nature)
- Methods: Roultte wheel selection,rank selection,tournament selection,boltzmann selection.
- Mostly preferred - Roultte wheel selection (depending on % contribution to total polulation, higher fitness spring is selected for mating to form next generation).
selection operator, p(i) = f(i)/summation(f(j))
(j ranging from 1 to n)
f(i)= fitness of the string
p(i) = probability in the string being selected
n = no. of individual in the population or chromosomes
n*p(i) = expected count
(The string with higher value is selected for mating)
2) Crossover:-
- can be one point or two point crossover.
- in one point - selected pairs of string at random position are swapped to form a new pair of string.
- In 2 point string there will be 2 break point.
(In Crossover the least value is replaced with the higher value of the string.
With correct selection the fitness value would have been increased)
3) Mutation:-
- Applied to each individual child after crossover.
- Binary are changed from 0 to 1 or 1 to 0 from randomly chosen strings or randomly chosen position.
- Higher values obtained by selection are not changed here.
(In Mutation, the fitness value would have been increased further)
Note:- If fitness value is lesser in mutation or crossover, we need to choose a different random population to get higher desired results.
Syntax of Genetic Algorithm and explanation:-
[local_minimum]=ga(function,no of parameters)
[local minimum]=ga(function,no of design parameters,non-linear equalities,upper_bound,lower_bound)
[local Minimum] = ga(function,no of paramaters,non-linear equalities,upper_bound,lower_bound,no of iterations defined in options)
Matlab Program:-
clear all
close all
clc
%defining search space
x=linspace(0,0.6,100);
y=linspace(0,0.6,100);
%creating 2D mesh
[xx yy] = meshgrid(x,y);
% Initial population
tic
%Evaluating 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
% Statistival behaviour where considering no of iteraitions to be 50
% (i=1:50)
tic
for i=1:50;
% [local_minimum]=ga(function,no of parameters)
[Input_vector,fval(i)]=ga(@stalagmite,2);
x1(i)=Input_vector(1);
y1(i)=Input_vector(2);
end
time_study1=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(x1,y1,fval,'Marker','o','Markersize',5,'Markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(fval)
xlabel('Iteration')
ylabel('Function Maximum')
% Study 2 - Statistical Behaviour with upper & lower bounds(upper & lower limits)
num_case=50;
tic
for i=1:num_case;
%[local minimum]=ga(function,no of design parameters,non-linear equalities,upper_bound,lower_bound)
[Inputs,fval(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
x1(i)=Inputs(1);
y1(i)=Inputs(2);
end
time_study2=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(x1,y1,fval,'Marker','o','Markersize',5,'Markerfacecolor','r')
title('Bounded Inputs')
subplot(2,1,2)
plot(fval)
xlabel('Iterations')
ylabel('Function Maximum')
% Study 3: Increasing Genetic algorithm iteration
tic
options = optimoptions('ga');
options = optimoptions(options,'populationsize',200);
for i = 1:num_case
% [local Minimum] = ga(function,no of paramaters,non-linear equalities,upper_bound,lower_bound,no of iterations defined in options)
[Inputs,fval(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
x1(i)=Inputs(1);
y1(i)=Inputs(2);
end
time_study3=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(x1,y1,fval,'Marker','o','Markersize',5,'Markerfacecolor','r')
title('Bounded Inputs')
subplot(2,1,2)
plot(fval)
xlabel('Iterations')
ylabel('Function Maximum')
Stalagmite Function:-
function [F]= stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1 = (sin(5.1*pi*x+0.5))^6;
f2 = (sin(5.1*pi*y+0.5))^6;
f3 = exp(-4*log(2)*(((x-0.0667)^2)/(0.64)));
f4 = exp(-4*log(2)*(((y-0.0667)^2)/(0.64)));
F= -(f1*f2*f3*f4);
end
Outputs:-
Unbounded Inputs
no. of iterations = 50;


Population size = 200,

population size = 400,
Errors:-
Error in the stalagmite function:-

wrong use of brackets and unbalanced brackets lead to this error in stalagmite function

Not defined the indexing of the initial vector corrsponding to the values of x and y in the function.
calling the function should be without indexing, and throughput from the function is 1 value.

Due to error in coding:- Markerfacecolor

used a dot operator instead of comma.