Week 4.1 - Genetic Algorithm
clear all
close all
clc
num_case= 100;
%difining the search space
x = linspace(0,0.5,100)
y = linspace(0,0.5,100)
%creating the 2 dimentional array
[xx yy] = meshgrid(x,y)
%evaluvating the stalagmate 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
surfc(xx,yy,f)
shading interp
tic
%study1 - statistical behaviour
for i = 1:num_case
[input,fopt(i)] = ga(@stalagmite , 2);
xopt = input(1);
yopt = input(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',3,'MarkerFaceColor','b')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('itteration')
ylabel('function minimum')
tic
%study2 - statistical behavior - with upper and lower bounds
for i = 1:num_case
[input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt = input(1);
yopt = input(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',3,'MarkerFaceColor','b')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('itteration')
ylabel('function minimum')
%study3 - increasing GA iteration
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',500);
tic
for i = 1:num_case
[input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt = input(1);
yopt = input(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',3,'MarkerFaceColor','b')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('itteration')
ylabel('function minimum')
Explaination of genetic algorithm :-
genetic algorithm is search based optimization technicque based on tha principal of natural and genetic seletion and it is frequently used an find optimal and near opitimal seletion
ones we choose optimal algoritham to perform optimazation prosess genetic algotham is one of tha optimasation processes
* it is frequently used to solve in research and in machine learning
* genetic algoritham is start with population initialization
Explaination on stalagmite code :-
Aim :-
To implemant genetic algorithm technicque and make tha stalagmite code to achive tha population increasing and used of stalagmite function
Governing tha equation :-
- To. assume tha one dimentional array and to creating two dimentional array using meshgrid command
- For using for loop. command and implement tha i ang j and to complete tha f(i,j)
- f(i,j)= stalagmite(input_vector)
- To creat the stady 1 and 2 and 3
- study 1 is nothing but statistical behaviour and study 2 is statistical behaviour with upper and lower bound and study 3 is increasing tha genetic algoritham iteration
Objective of the project :-
- The main objective of the project is to achive bounded input and unbounded input
- plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',3,'MarkerFaceColor','b')
- The plot3 is used as the to creat upper nad lower case of population
Body of contant :-
* In case of programming project :-
- First case to evaluvate the stalagmite function using for loop and surfc and sanding interp
- In first study is called as tic command and assume num_case 100
- Using for loop and govering at [input,fopt(i)] = ga(@stalagmite , 2);
- And we can assume figure(1) and let we take tha subplot using smooth curve
- plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',3,'MarkerFaceColor','b') make sure can put the title is unbounded inputs
- And than plot (-fopt) and mension xlabel and ylabel
- At the same condition of study 2 first using tic and toc command and using for loop
- [input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
- It is nothig but tha upper and lower bound plot comes smootly
* In case of simulation project :-
- In 3rd study was increasing genetic algotitham iteration
- options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',500); - in that option was indicate the population increase in upper and lower case
- Hear the useing the tic toc command and make a for loop in different input vector
- [input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
- And same the study 1 and 2 consist make a subplot and creat tha plot3 command
- plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',3,'MarkerFaceColor','b')
- Finally make the title and label we got bounded and unbounded inputs
Conclution :-
We are creating the stalagmite function and use genetic algoritham make a unbounded and bounded output and its run to matlab succesfully