GA LAB Manual-1
GA LAB Manual-1
GA LAB Manual-1
Engineering
LAB FILE
CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)
Table of Contents
UNIT-I
R=
0 0.8 1 0.6
0. 0.4 1 0.3
UNIT II
UNIT-III
Program-1
Write a MATLAB program to plot a few activation functions that are being
used in Genetic Algorithm
The MATLAB program is given below:
OUTPUT:
Program-2
STUDY AND ANALYSIS OF GENETIC ALGORITHM LIFE CYCLE.
Genetic Algorithm is a search heuristic (experience) that follows the process of natural
evolution.
This heuristic is used to generate useful solutions to optimization and search problems.
Genetic Algorithm belong to the larger class of evolutionary (EA) which generate
solutions to optimization problems and using inspired by natural evolution like –
inheritance, mutation, selection,
Genetic Algorithm need design space to be converted into genetic space. Genetic
Algorithm works with coding variables.
Genetic Algorithm uses population of point at one time in contrast to the single point
approach. It means that genetic algorithm processes a number of designs at the same
time.
The advantage of coding of variable is that coding discretizes the search space even
though the function may be continuous.
Traditional optimization methods use transition rules that are deterministic in nature.
While genetic algorithm uses randomize operators. Randomize operator improve the
search space in an adaptive manner.
1. It shows simplicity.
2. Ease of operation.
3. Minimal requirement.
4. Global perspective.
5. It does not guarantee to find global minimum solutions but acceptably good solutions
to “acceptably quickly
1. BEGIN
2. Create initial population ;
3. Compute fitness of each individuals ;
4. WHILE NOT finished DO Loop
5. BEGIN
6. Select individuals from old generation for mating ;
7. Create offspring by applying crossover or mutation to the selected individuals ;
8. Compute fitness of new individuals ;
9. Kill old individuals to make a room for new chromosomes and insert offspring in the
new
generation ;
10. If population has converged
11. Then fitness=TRUE ;
12. END
END
Program-3
Illustrate different types of generalized bell membership functions using
Matlab program.
Program:
x = (-10:0.4:10)’;
b = 2;c = 0;
a = 5;c = 0;
a = 5;b = 2;
c = 0;
OUTPUT:
Program-4
Using Matlab program find the crisp lambda cut set relations for λ = 0.2, the
fuzzy matrix is given by
R=
0 0.8 1 0.6
0. 0.4 1 0.3
PROGRAM:-
% Enter the matrix value
[m,n]=size(R);
for i=1:m
for j=1:n
if(R(i,j)<lambda)
b(i,j)=0;
else
b(i,j)=1;
end
end
end
% output value
display(b)
OUTPUT
Enter the matrix value
[0.2 0.7 0.8 1;1 0.9 0.5 0.1;0 0.8 1 0.6;0.2 0.4 1 0.3]
R=
lambda = 0.2000
b=
1111
1110
0111
1111
Program-5
Write a MATLAB program for Bit-wise Operators.
The following example demonstrates the perceptron learning law.
Program
p = 5; % dimensionality of the augmented input space
N = 50; % number of training patterns - size of the training epoch
% PART 1: Generation of the training and validation sets.
X = 2*rand(p-1, 2*N)-1;
nn = round((2*N-1)*rand(N,1))+1;
X(:,nn) = sin(X(:,nn));
X = [X; ones(1,2*N)];
wht = 3*rand(1,p)-1; wht = wht/norm(wht);
wht
D = (wht*X >= 0);
Xv = X(:, N+1:2*N) ;
Dv = D(:, N+1:2*N) ;
X = X(:, 1:N) ;
D = D(:, 1:N) ;
% [X; D]
pr = [1, 3];
Xp = X(pr, :);
wp = wht([pr p]); % projection of the weight vector
c0 = find(D==0); c1 = find(D==1);
% c0 and c1 are vectors of pointers to input patterns X
% belonging to the class 0 or 1, respectively.
figure(1), clf reset
plot(Xp(1,c0),Xp(2,c0),'o', Xp(1, c1), Xp(2, c1),'x')
% The input patterns are plotted on the selected projection
% plane. Patterns belonging to the class 0, or 1 are marked
% with 'o' , or 'x' , respectively
axis(axis), hold on
% The axes and the contents of the current plot are frozen
% Superimposition of the projection of the separation plane on the
% plot. The projection is a straight line. Four points lying on this
% line are found from the line equation wp . x = 0
L = [-1 1] ;
S = -diag([1 1]./wp(1:2))*(wp([2,1])'*L +wp(3)) ;
plot([S(1,:) L], [L S(2,:)]), grid, drawnow
% PART 2: Learning
eta = 0.5; % The training gain.
wh = 2*rand(1,p)-1;
% Randominitialisation of the weight vector with values
% from the range [-1, +1]. An example of an initial
% weight vector follows
% Projection of the initial decision plane which is orthogonal
% to wh is plotted as previously:
wp = wh([pr p]); % projection of the weight vector
S = -diag([1 1]./wp(1:2))*(wp([2,1])'*L +wp(3)) ;
plot([S(1,:) L], [L S(2,:)]), grid on, drawnow
C = 50; % Maximum number of training epochs
E = [C+1, zeros(1,C)]; % Initialization of the vector of the total sums of squared errors over an
epoch.
WW = zeros(C*N, p); % The matrix WW will store all weight
% vector whone weight vector per row of the matrix WW
c = 1; % c is an epoch counter
cw = 0 ; % cw total counter of weight updates
while (E(c)>1)|(c==1)
c = c+1;
plot([S(1,:) L], [L S(2,:)], 'w'), drawnow
for n = 1:N
eps = D(n) - ((wh*X(:,n)) >= 0); % eps(n) = d(n) - y(n)
wh =wh + eta*eps*X(:,n)'; % The Perceptron Learning Law
cw = cw + 1;
WW(cw, :) = wh/norm(wh); % The updated and normalised weight vector is stored in WW for
feature plotting
OUTPUT:
Program-6
Write a MATLAB program to show Multi objective optimization in Genetic
Algorithm
The MATLAB program is given as follows.
program
%Initialize weights and bias
v=[0.197 0.3191 -0.1448 0.3394;0.3099 0.1904 -0.0347 -0.4861];
v1=zeros(2,4);
b1=[-0.3378 0.2771 0.2859 -0.3329];
b2=-0.1401;
w=[0.4919;-0.2913;-0.3979;0.3581];
w1=zeros(4,1);
x=[1 1 0 0;1 0 1 0];
t=[0 1 1 0];
alpha=0.02;
mf=0.9;
con=1;
epoch=0;
while con
e=0;
for I=1:4
%Feed forward
for j=1:4
zin(j)=b1(j);
fori=1:2
zin(j)=zin(j)+x(i,I)*v(i,j);
end
z(j)=binsig(zin(j));
end
yin=b2+z*w;
y(I)=binsig(yin);
delk=(t(I)-y(I))*binsig1(yin);
delw=alpha*delk*z'+mf*(w-w1);
delb2=alpha*delk;
delinj=delk*w;
for j=1:4
delj(j,1)=delinj(j,1)*binsig1(zin(j));
end
for j=1:4
fori=1:2
delv(i,j)=alpha*delj(j,1)*x(i,I)+mf*(v(i,j)-v1(i,j));
end
end
delb1=alpha*delj;
w1=w;
v1=v;
%Weight updation
w=w+delw;
b2=b2+delb2;
v=v+delv;
b1=b1+delb1';
e=e+(t(I)-y(I))^2;
end
if e<0.005
con=0;
end
epoch=epoch+1;
end
disp('BPN for XORfuntion with Binary input and Output');
disp('Total Epoch Performed');
disp(epoch);
disp('Error');
disp(e);
disp('Final Weight matrix and bias');
v
b1
w
b2
OUTPUT:
Program-7
Write a MATLAB program to show Classifier system
The MATLAB program is given as follows.
function y=bipsig(x)
y=2/(1+exp(–x))–1;
function y=bipsig1(x)
y=1/2*(1-bipsig(x))*(1+bipsig(x));
Program
delk=(t(I)-y(I))*bipsig1(yin);
delw=alpha*delk*z'+mf*(w-w1);
delb2=alpha*delk;
delinj=delk*w;
for j=1:4
delj(j,1)=delinj(j,1)*bipsig1(zin(j));
end
for j=1:4
fori=1:2
delv(i,j)=alpha*delj(j,1)*x(i,I)+mf*(v(i,j)-v1(i,j));
end
end
delb1=alpha*delj;
w1=w;
v1=v;
%Weight updation
w=w+delw;
b2=b2+delb2;
v=v+delv;
b1=b1+delb1';
e=e+(t(I)-y(I))^2;
end
if e<0.005
con=0;
end
epoch=epoch+1;
end
disp(' XORfuntion with Bipolar Input and Output');
disp('Total Epoch Performed');
disp(epoch);
disp('Error');
disp(e);
disp('Final Weight matrix and bias');
v
b1
w
b2
OUTPUT:
Practical 8
Write a Matlab program (m.file) to calculate union, intersection, complement
of two fuzzy sets.
Program :-
% enter the two matrix
%option 1 Union
%option 2 intersection
%option 3 complement
if (option==1)
w=max(u,v)
end
if (option==2)
p=min(u,v)
end
if (option==3)
or second matrix’);
if (option1==1)
[m,n]=size(u);
q=ones(m)-u;
else
q=ones(m)-v;
end
end
OUTPUT:-
(1)
w=
(2)
p=
(3)
To find complement of A
enter the whether to find complement for first matrix or second matrix
q=
(4)
To find complement of B
enter the whether to find complement for first matrix or second matrix
q=
Program-9
Write a MATLAB program to recognize the number 0, 1, 2, 39. A 5 * 3 matrix
forms the numbers. For any valid point it is taken as 1 and invalid point it is
taken as 0. The net has to be trained to recognize all the numbers using The
Bucket Brigade.
The numbers are formed from the 5 * 3 matrix and the input is determined. The input data and the test
data are given. When the test data is given, if the pattern is recognized then it is + 1, and if the pattern is
not recognized, it is – 1.
Program
input=[1 0 1 1 1 1 1 1 1 1;
1 1 1 1 0 1 1 1 1 1;
1 0 1 1 1 1 1 1 1 1;
1 1 0 0 1 1 1 0 1 1;
0 1 0 0 0 0 0 0 0 0;
1 0 1 1 1 0 0 1 1 1;
1 0 1 1 1 1 1 0 1 1;
0 1 1 1 1 1 1 0 1 1;
1 0 1 1 1 1 1 1 1 1;
1 0 1 0 0 0 1 0 1 0;
0 1 0 0 0 0 0 0 0 0;
1 0 0 1 1 1 1 1 1 1;
1 1 1 1 0 1 1 0 1 1;
1 1 1 1 0 1 1 0 1 1;
1 1 1 1 1 1 1 1 1 1;]
fori=1:10
for j=1:10
ifi==j
output(i,j)=1;
else
output(i,j)=0;
end
end
end
fori=1:15
for j=1:2
if j==1
aw(i,j)=0;
else
aw(i,j)=1;
end
end
end
test=[1 0 1 1 1;
1 1 1 1 0;
1 1 1 1 1;
1 1 0 0 1;
0 1 0 0 1;
1 1 1 1 1;
1 0 1 1 1;
0 1 1 1 1;
1 0 1 1 1;
1 1 1 0 0;
0 1 0 1 0;
1 0 0 1 1;
1 1 1 1 1;
1 1 1 1 0;
1 1 1 1 1;]
net=newp(aw,10,'hardlim');
net.trainparam.epochs=1000;
net.trainparam.goal=0;
net=train(net,input,output);
y=sim(net,test);
x=y';
fori=1:5
k=0;
l=0;
for j=1:10
if x(i,j)==1
k=k+1;
l=j;
end
end
if k==1
s=sprintf('Test Pattern %d is Recognised as %d',i,l-1);
disp(s);
else
s=sprintf('Test Pattern %d is Not Recognised',i);
disp(s);
end
end
OUTPUT:
Program-10
OUTPUT: