Week 4.1 - Genetic Algorithm
Aim:
To optimize stalagmite function using algorithm
Observation:
-Find the global maxima
-Plotting the graph for all 3 studies for fn maximum vs number of iterations
Theory:
-Genetic Algorithm are the random serch provider with historical data to better performance in solution space.
-It mainly refers the tearm the term "Survial of the fittest" need to adapt according to the environment.
- It has a structure of
---Initial population- Evalute - Selection - Crossover - Mutation - termination (yes / no) - yes - solution---
Syatax of ga:
x=ga(fun, nvars) finds a local unconstrain minimum, x, to the objective fn, fun_nvars is the dimensions (number of design variables) od fun.
x=ga(fun,nvars,a,b,aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variable,x, so that a solution in the range lb≤x≤ub.
x=ga(fun,nvars,a,b,aeq,beq,lb,ub,nonlcon,option) minimize the default optimization parameter replaced by value in option.
Where
fun= objective fn
nvars= number of variables
a,b,aeq & beq= linear inequality consrtaints
lb= lower bound
ub=upper bound
nonlcon=nonlinear constrains
option= optimization options
Complete code:
clear all
close all
clc
%defining our search space
x= linspace(0, 0.6, 150);
y= linspace(0, 0.6, 150);
%creating a 2Dimensional mesh
[xx,yy]= meshgrid(x, y);
% Evalucating 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
% no of iterations
num_case=50
tic
%study 1 - Unbound input
for i= 1: num_case
%ga syntex
[input, fopt(i)]= ga(@stalagmite, 2)
xopt(i) = input(1);
yopt(i) = input(2);
end
%how much time it perform?
study1_time= toc;
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
%to hide grid lines
shading interp
plot3(xopt, yopt, fopt,"MarkerSize",8,"MarkerFaceColor","auto")
xlabel("x")
ylabel("y")
title("unbounded inputs")
%to make algo more clear
subplot(2,1,2)
plot(fopt);
xlabel("iterations")
ylabel("maximum value of function");
% study 2 - Bounded inputs, Upper and lower are bounded
for i= 1: num_case
%genetic algorithm syntex
[inputs, fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i)= inputs(1)
yopt(i)= inputs(2)
end
study2_time= toc
figure (2)
subplot(2,1,1)
hold on
surfc(x,y,f)
%to hide grid lines
shading interp
plot3(xopt, yopt, fopt,"MarkerSize",8,"MarkerFaceColor","auto")
xlabel("x")
ylabel("y")
title("bounded inputs")
%to make algo more clear
subplot(2,1,2)
plot(fopt)
xlabel("iterations")
ylabel("maximum value of function");
%study 3 - Bounded inputs with modified population
options= optimoptions("ga")
options= optimoptions(options, "PopulationSize",300);
for i= 1num_case
%genetic algorithm syntex
[inputs, fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],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)
%to hide grid lines
shading interp
plot3(xopt, yopt,fopt,"Marker","o","MarkerSize",9,"MarkerFaceColor","auto")
xlabel("x")
ylabel("y")
title("bounded inputs with modified population size")
%to make algo more clear
subplot(2,1,2)
plot(fopt)
xlabel("iterations")
ylabel("maximum value of function");
Function Definition:
function [f]=stalagmite(input);
x= input(1);
y= input(2);
term1= (sin((5.1*pi*x)+0.5))^6;
term2= (sin((5.1*pi*y)+0.5))^6;
term3= exp((-4)*log(2)*((x- 0.0667)^2)/0.64);
term4= exp((-4)*log(2)*((y- 0.0667)^2)/0.64);
f= -(term1*term2*term3*term4);
end
Code Explanation
-first write command clear all, close all, clc
-setting up the search space for giving problem, considering the variables x and y with line space command as x,y both are in one dimensional array. with the help of meshgrid it converted to two dimensional array as xx,yy
-we have created a function file to call up inside code, there we used formula considering it as tearm 1,2,3,4. then multiplying all.
-here we have specified the input vectors as xx and yy and are used to give the function call as (stalagmite)
-here the number of iterations are consider as 50(num_case=50)
-here tic and toc are the commands used to mesure the time taken by particular study.
Study 1(ubounded inputs):

Study 2 (bounded input)

Study 3 (bounded input with modified population size)

Errors:
- missed the negative sign in the function call up. its because ga is used to find the minima, for that we have to find maxima.

-missed end command to close for loop
