Stalagmite function using MATLAB
Genetic Algorithm
“Select The Best, Discard The Rest”
The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation.
The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:
- Selection rules select the individuals, called parents, that contribute to the population at the next generation.
- Crossover rules combine two parents to form children for the next generation.
- Mutation rules apply random changes to individual parents to form children.
‘’Syntax for GA’’
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)
[x,fval,exitflag,output] = ga(___)
[x,fval,exitflag,output,population,scores] = ga(___)
|
fitnessfcn |
Fitness functions |
|
nvars |
Number of design variables |
|
Aineq |
A matrix for linear inequality constraints |
|
Bineq |
b vector for linear inequality constraints |
|
Aeq |
Aeq matrix for linear equality constraints |
|
Beq |
beq vector for linear equality constraints |
|
lb |
Lower bound on x |
|
ub |
Upper bound on x |
|
nonlcon |
Nonlinear constraint function |
|
rngstate |
Optional field to reset the state of the random number generator |
|
solver |
'ga' |
|
options |
Options created using optimoptions or exported from the Optimization app |
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) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').
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,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)
x = ga(fun,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 optimoptions.
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options) requires that the variables listed in IntCon take integer values.
x = ga(problem) finds the minimum for problem, where problem is a structure.
[x,fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.
[x,fval,exitflag,output] = ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.
[x,fval,exitflag,output,population,scores] = ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.
Script:
To find global maxima
%Defining our search space to be optimized.
%%one dimesional
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating 2dimiensional grid
[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);
f(i,j) = stalagmite_func(input_vector);
end
end
surf(xx, yy, f)
shading interp
xlabel('x value')
ylabel('y value')
[x_output ,fval] = ga(@stalagmite_func,2);
Inputs 'X and Y' are assigned by using linspace command and afterwards 2-dimensional array is generated by using meshgrid command. forloop command is used for repetitive vector data formation and executed by calling hyperlinked function code for stalagmite.
Function Code:
function [stalagmite] = stalagmite_func(input_vector)
x= input_vector (1)
y = input_vector (2)
f1_x = (sin(5.1*pi * x +0.5)).^6;
f2_y = (sin(5.1*pi * y +0.5)).^6;
f3_x = exp((-4*0.6931).*(x-0.0667)^2/(0.64));
f4_y = exp((-4*0.6931).*(y-0.0667)^2/(0.64));
stalagmite = (f1_x.*f2_y.*f3_x.*f4_y)
end
%Defining our search space to be optimized.
%%one dimensional
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%creating 2dimiensional grid
[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);
f(i,j) = stalagmite_func(input_vector);
end
end
%study_1 statistical behaviour
tic
for i = 1:num_cases
[inputs,Fopt(i)] = ga(@stalagmite_func,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time_1 = toc;
%plotting
figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,f);
shading interp;
ylabel('Y data');
xlabel('X data');
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','y')
title('Unbounded Inputs')
subplot(2,1,2)
plot(Fopt)
xlabel('Iterations')
ylabel('Maximum function')
%study_2 bounding the values
tic
for i = 1:num_cases
[inputs,Fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time_2 = toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('X data')
ylabel('Y data')
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('Bounded Inputs')
subplot(2,1,2)
plot(Fopt)
xlabel('Iterations')
ylabel(' Maximum Function')
%study_3 bounding the values and increasing population size
options = optimoptions('ga');
options = optimoptions(options,'populationsize',300);
tic
for i = 1:num_cases
[inputs,Fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time_3 = toc;
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('X data')
ylabel('Y data')
plot3(xopt, yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','g');
title('Bounded Inputs with Increased population size');
subplot(2,1,2)
plot(Fopt)
xlabel('Iterations');
ylabel('Maximum Function');
Case Study 1: Unbounded inputs
Case Study 2: Bounded Inputs
Case study 3: Bounded Inputs with increased population size.
Workspace data:
Presented result shows data from 3 experimented investigations to examine statistical behaviour which shows optimisation of code has been done.
References:
https://www.iitg.ac.in/rkbc/CE602/CE602/Genetic%20Algorithms.pdf
https://in.mathworks.com/videos/what-is-a-genetic-algorithm-100904.html