Week 4 - Genetic Algorithm
AIM:-
- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
- Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
THEORY:-
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 syntax for ga used in MATLAB is as follows:
- x = local minimum
- fun = objective function
- nvars = number of variables in objective function
- A,b = linear inequalities such that A*x<=b
- Aeq,beq = liner equality such that Aeq*x = beq
- lb,ub = lower and upper bounds for the solution
- nonlcon = non linear constraints
- options = optimization options
FUNCTION
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*f1y*f2x*f2y);
MAIN PROGRAM:-
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%creating a 2dimensional mesh
[xx,yy] = meshgrid(x,y);
%looping
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); % stalagmite function;
end
end
surfc(f)
xlabel('x value')
ylabel('y value')
shading interp
[inputs, fval] = ga(@stalagmite,2); % genetic algorithm function
tic
%study 1
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc;
figure(1)
subplot(2,1,1)%To make subplot inside the main plot;
hold on
surfc(xx,yy,f)
shading interp
%plot3 command plot x-value,y-value,foptimum-value
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2) %this plots the optimum value over the iteration numbers
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
%study 2
num_cases = 50;
options = gaoptimset('ga');
options = gaoptimset(options,'populationSize',200);
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(xx,yy,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')
%study 3
num_cases = 50;
options = gaoptimset('ga');
options = gaoptimset(options,'populationSize', 250);
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(xx,yy,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')
CALLING FUNCTION:-
function [f] = 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));
f = -(f1x*f1y*f2x*f2y);
end
RESULTS:-
STUDY 1
STUDY 2
STUDY 3
REFFERENCES