Week 4.1 - Genetic Algorithm
clear all
close all
clc
% defind our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
input_vector = [0.6 0.6];
%creating 2Danimation mesh
[xx yy] = meshgrid(x,y);
num_cases = 50
%evaluating the staglmin fuctiction
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_fun(input_vector);
end
end
surfc(xx,yy,f)
%shading interp
xlabel('x value')
ylabel('y value')
title('stalagmite fuction')
%study one
tic
for i= 1:num_cases
[input,fopt(i)] = ga(@stalagmite_fun, 2);
xopt(i) = input(1);
yopt(i) = input(2);
end
study_1 = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
title('unbounded_input')
xlabel('x value')
ylabel('y value')
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximalum')
%case 2nd with upar and lower bounds
tic;
for i = 1:num_cases
[inputs,fopt(i)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.6;0.6])
xopt(i)=input(1);
yopt(i)=input(2);
end
study_2_time = toc
figure(3)
subplot(2,1,1)
surf(x,y,f)
shading interp
title('bounded input')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt,yopt,fopt,'marker','o' ,'markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('x value')
ylabel('y value')
%study 3 incresing the pouplwwstion size
%by doing so we are increesing the probility of fuction
%to cover at global maximuma
option = optimoptions(@ga)
option = optimoptions(@ga,'populationsize',500)
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.6;0.6],[],[],option);
xopt(i) = input(1);
yopt(i) = input(2);
end
study_3_time=toc
figure(4)
subplot(2,1,1)
surf(x,y,f)
shading interp
title('bound input with incresing population size')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('intertion')
ylabel('fuction maximum')
A genetic algorithm is a search heuristic that is inspired by Charles Darwin's theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.
genetic algorithm(GA) is the method of solving both constrain and uncostrain Optimation problems
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | A1 (Gene) |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 |
A2(chromoso me) |
| 1 | 0 | 0 | 1 | 0 | 1 | 1 | A3 |
| 1 | 0 | 1 | 1 | 1 | 0 | 1 | A4(population) |
syntax for GA
x = ga( finds a local minimum fun,nvars,A,b)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,uvars), find the local uncostrain minimum x,to the objective fuction ,nvrs is the dimension (number of desing variables ) of the function
x= ga (fun,nvars,A,b,Aeq,beq) find the local minimum of x to function object to linear equlalities Aeq*X = beq and A*x<=b.[set of A = [] and b = [] if no linear equlalities exiest ga evalution the matrix product Aeq*X if X is the transpose
the prosses of natural selection
the process of natural selection start with the selection of fittest individuals from a population they produce offspring
which inherit the characteristics of the perents and will be added to next generation. if perents have better fittnes there offspring wil be better than perents and have a better chances to surviving this process keep on itersting and at the end a generation wilth the fittest indivisible will be found.




some error are facing in runing time '

over come on the erroe
i can find the the right path in matlab fille
use who commond and selected the right path