To maximize the stalagmite function by using genetic algorithm in Matlab code
AIM: Write a Matlab program to calculate global maxima of the stalagmite function.
Introduction:
Genetic algorithm (GA):
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.
- 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.
- 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,options) requires that the variables listed in IntCon take integer values.
In this study our objective to maximize the stalagmite function by using genetic algorithm method. The stalagmite function is explain below:
Stalagmite function:
Program of stalagmite function:
% stalagmite function program
function [Results ] = stalagmite( input_vector)
x = input_vector(1);
y = input_vector(2);
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));
Results = -(f1x.*f2x.*f1y.*f2y);
end
Main program:
clear all
close all
clc
% Define search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
% Meshgrid is used to generate 2D array
[xx yy] = meshgrid(x,y);
% To create 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
% stalagmite function plot in figure(1)
figure(1)
surfc(xx,yy,-f);
xlabel(\'x value\')
ylabel(\'y value\')
shading interp
% Case 1 Statistical behaviour without any bounding condition
tic
for a=1:num_cases
[inputs,fopt(a)]= ga(@stalagmite,2);
xopt(a) = inputs(1);
yopt(a) = inputs(2);
end
study1_time= toc
figure(2)
subplot(2,1,1);
surfc(x,y,-f);
xlabel(\'x value\')
ylabel(\'y value\')
shading interp
hold on
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\' Unbound output\')
subplot(2,1,2)
plot(-fopt);
xlabel(\'Iterations\');
ylabel(\'Functions maxima\');
% Case 2 Behaviour with bounded uper and lower boundary conditions
tic
for b=1:num_cases
[inputs,fopt(b)]= ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);
xopt(b) = inputs(1);
yopt(b) = inputs(2);
end
study2_time= toc
figure(3)
subplot(2,1,1);
surfc(x,y,-f);
xlabel(\'x value\')
ylabel(\'y value\')
shading interp
hold on
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\' Bounded output\')
subplot(2,1,2)
plot(-fopt);
xlabel(\'Iterations\');
ylabel(\'Functions maxima\');
% Case 3 Behaviour with bounded and increased populations
tic
options= gaoptimset(\'ga\');
options = gaoptimset( options, \'populationsize\',200);
for c = 1: num_cases
[inputs,fopt(c)]= ga(@stalagmite,2,[],[],[],[],[0:0],[1:1],[],options);
xopt(c)= inputs(1);
yopt(c)= inputs(2);
end
study3_time= toc
figure(4)
subplot(2,1,1);
surfc(x,y,-f);
xlabel(\'x value\')
ylabel(\'y value\')
shading interp
hold on
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bound output with increase populations\')
subplot(2,1,2)
plot(-fopt);
xlabel(\'Iterations\');
ylabel(\'Functions maxima\');
% ************************************END******************************************** %
Code explaination:
- Initially we start with defining the search space by taking x and y 1D array values.
- We start with 50 iterations to stalagmite function.
- Mesh grid command is used to define 2D array of search space.
- Shading interp is used for to smooth colour variations.
- Tic and toc word is used to calculate the time required for optimization.
- To increase the population size we used ‘gaoptimset’ function.
Output:
1. Stalagmite function
2. Case 1:
Study1_time = 18.585 sec
3. Case 2:
Study2_time = 37.644 sec
4. Case 3:
Study3_time = 108.0029 sec