Week 4.1 - Genetic Algorithm
AIM: TO CALCULATE THE GLOBAL MAXIMA OF A STALAGMITE FUNCTION USING MATLAB.
THEORY: The global maxima of a 'stalagmite function' can be obtained using 'gentic algorithm' in MATLAB.
genetic algorithms are a class of algorithms based on the abstraction of darwin's evolution of biological systems, pioneered by holland. He was probably the first to use genetic operation such as the crossover and recombination, mutation, and selection in the study of the adaptive and artifical systems. Three main components or genetic operators in genetic algorithm are
- crossover
- mutation
- selection of the fittest.
- The genetic algorithm can be applied to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable,stochastic, or highly nonlinear.
SYNTAX FOR ga-
- X=GA(fitnessfcn,nvars) finds a local unconstrained minimum,X, to the objective function, fitnessfcn.'nvars' is the dimension (number of design variables) of fitnessfcn.
- x=ga(fitnessfcn,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 eaqualities exist)
- x=ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon,options) minimizes with the default optimization parameters replaced by values in options.(set nonlcon=[] if no nonlinear constraints exist) create options using optimooptions (recommended) or gaoptimset.
THE FOLLOWING OUTLINE SUMMARIZES HOW THE GENETIC ALGORITHM WORKS:
- The algorithm then begins by creating a random intial population.
- The slgorithm then creates a sequences of the new populations. At each step, the algorithm uses the individuals in the curret generation to create the next population. To crate the new populations, the algorithm performs the following steps
- sources each member of the current population by computing its fitness values. these values are called the raw fitness scores.
- scales the raw fitness scores to convert them into a more usable range of values are called expectation values.
- selects members, called parents, based on their expectation.
- some of the individuals in the current population that have lower fitness are chosen as elite. these elite individuals are passed to the next population.
- produces children from the parents. children are produced either by makinng random changes to a single parents -mutation-or by combining the vector entries of the pair of plarents -crossover.
- Replaces the current population with the children to from the next generation.
3. The algorithm stops when one of the stopping criteria is met.
OBJECTIVES:
- To optimize the stalagmite function.
- To determine the global maxima of the function.
- STALAGMITE FUNCTION PROGRAM:
function [F] = stalagmite_func(input_vector)
x = input_vector(1);
y = input_vector(2);
f1 = (sin(5.1*3.14*x+0.5))^6;
f2 = (sin(5.1*3.14*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
MAIN PROGRAM:
clear all
close all
clc
%defining our space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
iterations = 50;
%creating a 2Dimentional mesh
[xx, yy] = meshgrid(x,y);
%evaluating the stalagmite function
for i = 1:length(xx)%incrementing i to next value until it stops
for j = 1:length(yy) %incrementing j to next value until it stops
input_vector(1) = xx(i,j);
input_vector(2) = yy(i,j);
f(i,j) = stalagmite_func(input_vector);
end
end
figure(1)
surfc(xx,yy,f) %surface plot 3D
shading interp %To make plot smoother without lines
xlabel('x values')
ylabel('y values')
title('stalagmite_function')
%X = ga(FITNESSFCN,NVARS,A,b,[],[],lb,ub,NONLCON,INTCON,options) minimizes
%with integer constraints and the default optimization parameters replaced
%by values in options. options can be created with the optimptions
%function.
%fitnessfcn:
% nvars:
% Aineq:
% bineq:
% Aeq:
% beq:
% lb:
% ub:
% nonclon:
% intcon:
% case1 - unbounded values using ga
tic%Start a stopwatch timer
for i=1:iterations
[inputs,fop1(i)] = ga(@stalagmite_func,2);
xop1(i)= inputs(1);
yop1(i)= inputs(2);
end
case1_t=toc;%Read the stopwatch timer
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel(' X Values')
ylabel(' Y Values')
title (' case1 - unbounded values using ga ')
plot3(xop1,yop1,-fop1,'marker','o','markersize',3,'markerfacecolour','r')
subplot(2,1,2)
plot(-fop1)
xlabel(' Iteration ')
ylabel(' Function Maximum')
global_max1 = -fop1;
% case2 - lower and upper bonded values using ga
tic
for i=1:iterations
[inputs,fop2(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1,1]);
xop2(i)= inputs(1);
yop2(i)= inputs(2);
end
case2_t=toc;
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel(' X Values')
ylabel(' Y Values')
title (' case2 - lower and upper bounded values using ga ')
plot3(xop2,yop2,-fop2,'marker','o','markersize',3,'markerfacecolour','r')
subplot(2,1,2)
plot(-fop2)
xlabel(' Iteration ')
ylabel(' Function Maximum')
global_max2 = -fop2;
% case 3 - increased iterations with bounded values using ga
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);
tic
for i=1:iterations
[inputs,fop3(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1,1],[],[],options);
xop3(i)= inputs(1);
yop3(i)= inputs(2);
end
case3_t=toc;
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel(' X Values')
ylabel(' Y Values')
title (' case1 - increased iterations with bounded values using ga ')
plot3(xop3,yop3,-fop3,'marker','o','markersize',3,'markerfacecolour','r')
subplot(2,1,2)
plot(-fop3)
xlabel(' Iteration ')
ylabel(' Function Maximum')
global_max3 = -fop3;
EXPLANATION OF PROGRAM:
- Initially, the stalagmite function was mentioned with approprite argument vectors.
- The program has been subdivided into three studies based on the limitations of the previous study.
- first study deals with unbounded inputs,thereby expanding the boundary wherein the results can be obtained.
- second study brings in the aspect of bounded inputs providing a better solution for the drawback faced.
- third study lays a appreciable platform to dfetermine the exact solution.
- plots of all the three studies have been displayed ,thus giving a clear insight of the results.