Maximize Stalgamite function using Genetic Algorithm in Matlab
Objective:
Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Introduction:
The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. 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. Over successive generations, the population \"evolves\" toward an optimal solution. You can apply the genetic algorithm 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. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.
Matlab Syntax:
Note: ga function only give minimum value (However we want to calculate maxima, hence refer my fitness function for maxima)
[x,fval] = ga(function,no_of_variables,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
1) x=optimum solution/variables
2) no_of_variables=Number of variables, specified as a positive integer
3) A,b= Linear inequality constraints
For example, to specify
2x1 + 2x2 ≤ 13
x1 - x2 ≤ 20
x1 + 3x2 ≤ 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
2x1 + 2x2 = 13
x1 - x2 = 20
x1 + 3x2 = 30
give these constraints:
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 (We will see this in another program)
7) IntCon : to get solution in integer.
8) options: Minimize with non default options (Refer below program)
options = optimoptions(\'function\',\'Constraint\',limit,);
Procedure:
Step 1: Clearing workspace, command window & closing all plots
Step 2: Define our search space by using linspace command
Step 3: Calculating a 2 Dimentional mesh using meshgrid command
Step 4: Evaluating the stalagmite function for each value of x & y
Step 5: using GA command with unbounded inputs (Study_1) means optimum value may be come from outside of search space, Refer attached graph.
Step 6: using GA command with bounded inputs means optimum value will be come from inside of search space, Refer attached graph. However there will be huge variation in optimum value.
Step 7: using GA command with bounded inputs means optimum value will be come from inside of search space, Refer attached graph. However this time to avoid huge variation in optimum value, we have increased initial population size to 200. ( If we do not specify initial population in program, MATLAB take 50 as initial population)
Matlab Code:
A) Fitness Function:
We have added negative sign in stalagmite function to get maxima from this function. We also add negative sign in main function to get correct value.
function [F] = stalagmite(input_vector)
x=input_vector(1) ;
y=input_vector(2) ;
f1_x = (sin(5.1*pi*x+0.5)).^6;
f1_y = (sin(5.1*pi*y+0.5)).^6;
f2_x = exp(-4*log(2)*((x-0.0667).^2)/0.64);
f2_y = exp(-4*log(2)*((y-0.0667).^2)/0.64);
[F] = -(f1_x.*f1_y.*f2_x.*f2_y);
end
B)
close all
clear all
clc
% Define our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Calculating a 2Dimentionl mesh
[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(input_vector);
end
end
tic
% Study1- Statical behaviour
num_cases = 50;
for i=1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,30);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Unbounded inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')
% Study2- Statical buhaviour with upper and lover bounds
options = optimoptions(\'ga\');
options = optimoptions(options,\'populationSize\',50);
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
Study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')
% Study3- Increasing GA iteration
options = optimoptions(\'ga\');
options = optimoptions(options,\'populationSize\',200);
tic
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],...
options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs with modified population size\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minimum\'
Results:
1. Study-1 : Unbounded Inputs
2. Study-2 : Bounded Inputs (Population size=50
3. Study-3 : Bounded Inputs (Population size=200)