Aim = Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
objective = Plot graphs for all 3 studies and for F maximum vs no. of iterations.
calculation =
concept of genentic algorithm.
Genetic algorithms are a method that work based on Darwin's theory of natural selection
'The fittest survives,and has a greater chance of breeding and passing forward its genetic information'.
IN Matlab,
The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection.
working of Genetic Algorithm
1. The algorithm begins by creating a random initial population.
2. The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population. To create the new population, the algorithm performs the following steps:
a. Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.
b. Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.
c. Selects members, called parents, based on their expectation.
d. 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.
e. Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.
f. Replaces the current population with the children to form the next generation.
3. The algorithm stops when one of the stopping criteria is met.
stopping criteria for algorithm are as follows:
A. MaxGenerations — The algorithm stops when the number of generations reaches MaxGenerations.
B. MaxTime — The algorithm stops after running for an amount of time in seconds equal to MaxTime.
C. FitnessLimit — The algorithm stops when the value of the fitness function for the best point in the current population is less than or equal to FitnessLimit.
D. MaxStallTime — The algorithm stops if there is no improvement in the objective function during an interval of time in seconds equal to MaxStallTime.
syntax =
( ga function in matlab only give minimum value.)
[x,fval] = ga(function,no_of_variables,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
1. x = optimum solution
2. no_of_variables = Number of variables specified as a positive integer.
3. A,b = linear inequality constraints
for example, to specify
2*1 + 2*2 < 13
x1 - x2 < 20
x1 + 3*2 < 30
give these constraints:
A = [2,2;1,-1;1,3]
b = [13;20;30]
4. Aeq,beq = linear equality constraints
for example,to specify
2*1 + 2*2 = 13
x1 - x2 = 20
x1 + 3*2 = 30
give these contraints:
Aeq = [2,2;1,-1;1,3]
beq = [13;20;30]
5. lb,ub
lb = lower bounds
ub = upper bounds
for example,to specify
1<=x(1)<=6
-3<=x(2)<=8
give these constraints:
lb = [1 -3]
ub = [6 8]
6. nonlcon : use for nonlinear constraints
7. IntCon : to get solution in integer.
8. options : minimize with non default options
(options = optimoptions('function','Constraint',limit)
significance of running multiple iteration.
1.when working with multiple iterations, it can quickly become tedious to switch between them to check if everything worked as you'd expect.
function [f] = stalagmite(input_vector);
%defining input condition
x = input_vector(1);
y = input_vector(2);
%solving
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*f2x*f1y*f2y);
end
%Genetic Algorithm case 1(Unbounded Points)
clear all
close all
clc
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%defining 2Dimensional array
[xx yy] = meshgrid(x,y);
%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
tic
for i = 1:50
[inputs, fval(i)] = ga(@stalagmite,2)
x_out(i) = inputs(1);
y_out(i) = inputs(2);
end
case_time_1 = toc
f = -f;
figure(1)
subplot(2,1,1)
surfc(x,y,f);
xlabel('x value')
ylabel('y value')
zlabel('z value')
shading interp
hold on
plot3(x_out,y_out,-fval,'marker','o','markersize',5,'markerfacecolor','b');
title('unbounded points')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function maxima')
case_time_1 =
5.2381
STEPS
1. start with clear all close all and clc
2. define the input variables x and y with the help of linspace command
3. create 2dimensional array by using meshgrid command.
4. evalaute satalagmite funcion with help of function and end command
5.open the for loop for no of iterations(i.e 1:50)then specifiy x_out,y_out and in ga command specifiy stalagmite function with variables 2.
6. use surfc command to plot the stalagmite function
7. use plot3 command to plot unbounded inputs x_out,y_out,fval in same plot
8. for iteration and function maxima use plot command.
%Genetic Algorithm case 2(bounded inputs)
clear all
close all
clc
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%defining 2Dimensional array
[xx yy] = meshgrid(x,y);
%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
tic
for i = 1:50
[inputs, fval(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])
x_out(i) = inputs(1);
y_out(i) = inputs(2);
end
case_time_2 = toc
f = -f;
figure(2)
subplot(2,1,1)
surfc(x,y,f);
xlabel('x value')
ylabel('y value')
zlabel('z value')
shading interp
hold on
plot3(x_out,y_out,-fval,'marker','o','markersize',5,'markerfacecolor','b');
title('bounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function maxima')
case_time_2 =
7.0359
STEPS
1. start with clear all close all and clc
2. define the input variables x and y with the help of linspace command
3. create 2dimensional array by using meshgrid command.
4. evalaute satalagmite funcion with help of function and end command
5.open the for loop for no of iterations(i.e 1:50)then specifiy x_out,y_out and in ga command specifiy stalagmite function with variables 2,lower bounds and upper bounds.
6. use surfc command to plot the stalagmite function
7. use plot3 command to plot unbounded inputs x_out,y_out,fval in same plot
8. for iteration and function maxima use plot command
%Genetic Algorithm case 3(bounded inputs with modified size)
clear all
close all
clc
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%defining 2Dimensional array
[xx yy] = meshgrid(x,y);
%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
options = optimoptions('ga')
options = optimoptions(options,'populationsize',200)
tic
for i = 1:50
[inputs, fval(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
x_out(i) = inputs(1);
y_out(i) = inputs(2);
end
case_time_3 = toc
f = -f;
figure(3)
subplot(2,1,1)
surfc(x,y,f);
xlabel('x value')
ylabel('y value')
zlabel('z value')
shading interp
hold on
plot3(x_out,y_out,-fval,'marker','o','markersize',5,'markerfacecolor','b');
title('bounded points with modified size')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function maxima')
case_time_3 =
17.2266
STEPS
1. start with clear all close all and clc.
2. define the input variables x and y with the help of linspace command.
3. create 2dimensional array by using meshgrid command.
4. evalaute satalagmite funcion with help of function and end command.
5.open the for loop for no of iterations(i.e 1:50)then specifiy x_out,y_out and in ga command specifiy stalagmite function with variables 2,lower bounds and upper bounds,modified the population size by using function 'options'.
6. use surfc command to plot the stalagmite function.
7. use plot3 command to plot unbounded inputs x_out,y_out,fval in same plot
8. for iteration and function maxima use plot command.
%values of x and y for maximun value of function.
inputs =
0.0668 0.0668
Error =
1. comma is missing in between two square bracket. that's why it showing error.
steps to overcome error
1. put comma between two square brackets.
Reference
https://in.mathworks.com/discovery/genetic-algorithm.html
conclusion = by using matlab function like ga we able to find out global maxima and also using surfc command we plot stalagmite function.