To study STALAGMITE function for optimization purpose
AIM
To optimize the stalagmite function and to find out the global maxima of the function
GOVERNING EQUATIONS
There are several equation are used in this optimization problem which are discussed below
`1. ``f (x,y)=f1 . f2 . f3 . f4`
2. `f1 = [sin(5.1πx + 0.5)]^6`
3. `f2 = [sin(5.1πy + 0.5)]^6`
4. `f3 = exp [-4 ln(2) .(x-0.0667)^6/0.64 ]`
5. `f4 = exp [-4 ln(2) .(y-0.0667)^6/0.64 ]`
BODY OF THE CONTENT
Definition of genetic algorithm
A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution.
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.
You can apply the genetic algorithm to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.
The genetic algorithm differs from a classical, derivative-based, optimization algorithm in two main ways, as summarized in the following table.
|
Classical Algorithm |
Genetic Algorithm |
|
Generates a single point at each iteration. The sequence of points approaches an optimal solution. |
Generates a population of points at each iteration. The best point in the population approaches an optimal solution. |
|
Selects the next point in the sequence by a deterministic computation. |
Selects the next population by computation which uses random number generators. |
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.
GA CODE IN MATLAB
GA always calculate the minimum value for the optimization.
Syntax and description
To finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
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').
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').
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.
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.)
minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
requires that the variables listed in IntCon take integer values.
for any previous input arguments, also returns fval, the value of the fitness function at x.
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.
also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population
where,
A is Linear inequality constraints (real matrix)
Linear inequality constraints, specified as a real matrix. A is an M-by-nvars matrix, where M is the number of inequalities.
A encodes the M linear inequalities
A*x <= b,
where x is the column vector of nvars variables x(:), and b is a column vector with M elements.
For example, to specify
x1 + 2x2 ≤ 10
3x1 + 4x2 ≤ 20
5x1 + 6x2 ≤ 30,
give these constraints:
A = [1,2;3,4;5,6];
b = [10;20;30];
b — Linear inequality constraints (real vector)
Linear inequality constraints, specified as a real vector. b is an M-element vector related to the A matrix. If you pass b as a row vector, solvers internally convert b to the column vector b(:).
b encodes the M linear inequalities
A*x <= b,
where x is the column vector of N variables x(:), and A is a matrix of size M-by-N.
For example, to specify
x1 + 2x2 ≤ 10
3x1 + 4x2 ≤ 20
5x1 + 6x2 ≤ 30,
give these constraints:
A = [1,2;3,4;5,6];b = [10;20;30];
Aeq — Linear equality constraints (real matrix)
Linear equality constraints, specified as a real matrix. Aeq is an Me-by-nvars matrix, where Me is the number of equalities.
Aeq encodes the Me linear equalities
Aeq*x = beq,
where x is the column vector of N variables x(:), and beq is a column vector with Me elements.
For example, to specify
x1 + 2x2 + 3x3 = 10
2x1 + 4x2 + x3 = 20,
give these constraints:
Aeq = [1,2,3;2,4,1];beq = [10;20];
beq — Linear equality constraints (real vector)
Linear equality constraints, specified as a real vector. beq is an Me-element vector related to the Aeq matrix. If you pass beq as a row vector, solvers internally convert beq to the column vector beq(:).
beq encodes the Me linear equalities
Aeq*x = beq,
where x is the column vector of N variables x(:), and Aeq is a matrix of size Meq-by-N.
For example, to specify
x1 + 2x2 + 3x3 = 10
2x1 + 4x2 + x3 = 20,
give these constraints:
Aeq = [1,2,3;2,4,1];beq = [10;20];
lb — Lower bounds [] (default) | real vector or array
Lower bounds, specified as a real vector or array of doubles. lb represents the lower bounds element-wise in lb ≤ x ≤ ub.
Internally, ga converts an array lb to the vector lb(:)
ub — Upper bounds [] (default) | real vector or array
Upper bounds, specified as a real vector or array of doubles. ub represents the upper bounds element-wise in lb ≤ x ≤ ub.
Internally, ga converts an array ub to the vector ub(:)
nonlcon — Nonlinear constraints function handle | function name
Nonlinear constraints, specified as a function handle or function name. nonlcon is a function that accepts a vector or array x and returns two arrays, c(x) and ceq(x).
- c(x)is the array of nonlinear inequality constraints at x. ga attempts to satisfy
c(x) <= 0
for all entries of c.
- ceq(x)is the array of nonlinear equality constraints at x. ga attempts to satisfy
ceq(x) = 0
for all entries of ceq.
For example,
x = ga(@myfun,4,A,b,Aeq,beq,lb,ub,@mycon)
where mycon is a MATLAB® function such as
function [c,ceq] = mycon(x)c = ... % Compute nonlinear inequalities at x.ceq = ... % Compute nonlinear equalities at x. options — Optimization options output of optimoptions | structure
Optimization options, specified as the output of optimoptions or a structure.
Create options by using optimoptions (recommended) or by exporting options from the Optimization app
Problem description
|
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 |
MATLAB CODING
Coding for general study of stalagmite function
clear all
close all
clc
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating 2d mesh
[xx yy]= meshgrid(x,y);
%evaluating the stalagmite function
%for loop p starts from 1 to length of x
for i=1:length(xx)
%for loop q starts from 1 to length of y
for j=1:length(yy)
%taking input from xx
input_vector(1)= xx(i,j);
%taking input from yy
input_vector(2)= yy(i,j);
%calling of stlagmite and and taking input
f(i,j)=stalagmite(input_vector);
end
end
%plotting of figure 1
figure(1)
%hold on is used to hold the plot
hold on
%creates a three-dimensional surface plot, which is a three-dimensional surface that has solid edge colors and solid face colors. The function plots the values in matrix Z as heights above a grid in the x-y plane defined by X and Y. The color of the surface varies according to the heights specified by Z
surfc(xx,yy,f)
%labelling of the plot by giving xlabel and ylabel to define the axis
xlabel('x value')
ylabel('y value')
%removing grid and giving color variation
shading interp
%finds a local unconstrained minimum for the objective function ie stalagmite.m and number of variables
%Solution, returned as a real vector. input1 is the best point that ga located during its iterations
%Objective function value at the solution, returned as a real number. Generally, fval = fun(x).
[inputs, fval]= ga(@stalagmite,2);
%hold off the figure
hold off
Coding for different case of study
clear all
close all
clc
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases=50;
%creating 2d mesh of x and y
[xx yy]= meshgrid(x,y);
%evaluating the stalagmite function
%for loop p starts from 1 to length of x
for p=1:length(xx)
%for loop q starts from 1 to length of y
for q=1:length(yy)
%taking input from xx
input_vector(1)= xx(p,q);
%taking input from yy
input_vector(2)= yy(p,q);
%calling of stlagmite and and taking input
f(p,q)=stalagmite(input_vector);
%end of for loop
end
%end of for loop
end
tic %tic is used to calculate the time from strating
%study 1- statiscal behaviour
%for loop i starts from 1 to num case ie 50
for i=1:num_cases
%finds a local unconstrained minimum for the objective function ie stalagmite.m and number of variables
%Solution, returned as a real vector. input1 is the best point that ga located during its iterations
%Objective function value at the solution, returned as a real number. Generally, fval = fun(x).
[inputs1,fopt1(i)]= ga(@stalagmite,2)
%storing the best point of fuction from input1 that ga located during its iteration
xopt1(i) = inputs1(1)
yopt1(i) = inputs1(2)
end
study1_time=toc %toc is used to calculate the time upto end
%plotting of figure 2
figure(2)
%creating 2 plot in the same figure 2
%subplot(m,n,p)
%divides the figure into an m-by-n grid and creates axes in the position specified by p.
subplot(2,1,1)
%hold on is used to hold the plot
hold on
%creates a three-dimensional surface plot, which is a three-dimensional surface that has solid edge colors and solid face colors. The function plots the values in matrix Z as heights above a grid in the x-y plane defined by X and Y. The color of the surface varies according to the heights specified by Z
surfc(x,y,-f)
%removing grid and giving color variation
shading interp
%plotting by taking 3 value
%marker shape is 'o'
%markersize is 5
%markerfacecolor is red
plot3(xopt1,yopt1,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
%giving title name as unbounded input
title('unbounded input')
%divides the figure into an m-by-n grid and creates axes in the position specified by p
subplot(2,1,2)
%plot of the figure
plot(-fopt1)
%labelling of the plot by giving xlabel and ylabel to define the axis
xlabel('iteration')
ylabel('function')
%hold off the figure
hold off
tic %tic is used to calculate the time from strating
% study 2 statistical behaviour with upper and lower bound %
%for loop j starts from 1 to num case ie 50
for j=1:num_cases
%finds a local minimum of function stalagmite.m and number of variables
%Solution returned as a real vector input2 is the best point that ga located during its iterations
%Objective function value at the solution, returned as a real number. Generally, fval = fun(x).
%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.)
[inputs2,fopt2(j)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
%storing the best point of fuction from input1 that ga located during its iteration
xopt2(j)= inputs2(1);
yopt2(j)= inputs2(2);
end
study2_time = toc %toc is used to calculate the time upto end
%plotting of figure 3
figure(3)
%creating 2 plot in the same figure 2
%subplot(m,n,p)
%divides the figure into an m-by-n grid and creates axes in the position specified by p.
subplot(2,1,1)
%hold on is used to hold the plot
hold on
%creates a three-dimensional surface plot, which is a three-dimensional surface that has solid edge colors and solid face colors. The function plots the values in matrix Z as heights above a grid in the x-y plane defined by X and Y. The color of the surface varies according to the heights specified by Z
surfc(x,y,-f)
%removing grid and giving color variation
shading interp
%plotting by taking 3 value
%marker shape is 'o'
%markersize is 5
%markerfacecolor is red
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
%giving title name as bounded input
title('bounded input')
%divides the figure into an m-by-n grid and creates axes in the position specified by p
subplot(2,1,2)
%plot of the figure
plot(-fopt2)
%labelling of the plot by giving xlabel and ylabel to define the axis
xlabel('iteration')
ylabel('function')
%hold off the figure
hold off
%study 3 G A iteration %
%options = optimoptions(SolverName) returns a set of default options for
%the SolverName solver here solver is 'ga'
option= optimoptions('ga');
%options = optimoptions(options,Name,Value) returns a copy of options with the named parameters altered with the specified values.
option= optimoptions(option,'populationsize',200);
tic %tic is used to calculate the time from strating
%for loop k starts from 1 to num case ie 50
for k=1:num_cases
%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.)
[inputs3,fopt3(k)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],option);
xopt3(k)= inputs3(1);
yopt3(k)= inputs3(2);
end
study3_time = toc %toc is used to calculate the time upto end
%plotting of figure 4
figure(4)
%creating 2 plot in the same figure 2
%subplot(m,n,p)
%divides the figure into an m-by-n grid and creates axes in the position specified by p.
subplot(2,1,1)
%hold on is used to hold the plot
hold on
%creates a three-dimensional surface plot, which is a three-dimensional surface that has solid edge colors and solid face colors. The function plots the values in matrix Z as heights above a grid in the x-y plane defined by X and Y. The color of the surface varies according to the heights specified by Z
surfc(x,y,-f)
%removing grid and giving color variation
shading interp
%plotting by taking 3 value
%marker shape is 'o'
%markersize is 5
%markerfacecolor is red
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
%giving title name as bounded input WITH MODIEFIED
title('bounded input with modified')
%divides the figure into an m-by-n grid and creates axes in the position specified by p
subplot(2,1,2)
%plot of the figure
plot(-fopt3)
%labelling of the plot by giving xlabel and ylabel to define the axis
xlabel('iteration')
ylabel('function')
%hold off the figure
hold off
Stalagmite Function used in this coding is given below
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
ERROR OBTAIN WHILE PROGRAMMING
1. By defining wrong input in the function the code was not work properly after several attempt and practice now it’s working
RESULTS
After completing three studies, the results are discussed below
Result for studying stalagmite function
Result for different case of study
1. Figure for unbounded input
2. Figure for bounded input
3. Figure for bounded input with modified inputs
Reference
- video lecture of skill lync
- Mathworks website