You are on page 1of 33

Department of Computer Science &

Engineering

SUBJECT: Genetic Algorithm


(CSA-466)

B.E. IV Year – VIII Semester


(Branch: CSE)

LAB FILE
CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Table of Contents

Sr. No. List of Programs

UNIT-I

Write a MATLAB program to plot a few activation functions that


1.
are being used in Genetic Algorithm
STUDY AND ANALYSIS OF GENETIC ALGORITHM LIFE
2. CYCLE.

Illustrate different types of generalized bell membership functions


3.
using Matlab program.
Using Matlab program find the crisp lambda cut set relations for λ =
0.2, the fuzzy matrix is given by

R=

0.2 0.7 0.8 1


4.
1 0.9 0.5 0.1

0 0.8 1 0.6

0. 0.4 1 0.3

UNIT II

Write a MATLAB program for Bit-wise Operators.


5.

Write a MATLAB program to show Multi objective optimization in


6. Genetic Algorithm

Write a MATLAB program to show Classifier system


7.

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

UNIT-III

Write a Matlab program (m.file) to calculate union, intersection,


8. complement of two fuzzy sets.

Write a MATLAB program to recognize the number 0, 1, 2, 39.


A5*3 matrix forms the numbers. For any valid point it is taken as 1
8. and invalid point it is taken as 0. The net has to be trained to
recognize all the numbers using The Bucket Brigade.

Write a MATLAB program to illustrate Smitch’s Poker player


9. application

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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:

>> % Illustration of various activation functions used in GA's


x = -10:0.1:10;
tmp = exp(-x);
y1 = 1./(1+tmp);
y2 = (1-tmp)./(1+tmp);
y3 = x;
subplot(231); plot(x, y1); grid on;
axis([min(x) max(x) -2 2]);
title('Logistic Function');
xlabel('(a)');
axis('square');
subplot(232); plot(x, y2); grid on;
axis([min(x) max(x) -2 2]);
title('Hyperbolic Tangent Function');
xlabel('(b)');
axis('square');
subplot(233); plot(x, y3); grid on;
axis([min(x) max(x) min(x) max(x)]);
title('Identity Function');
xlabel('(c)');
axis('square');
>>

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Program-2
STUDY AND ANALYSIS OF GENETIC ALGORITHM LIFE CYCLE.

Genetic Algorithm (GA) :-

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.

There are three important aspects of Genetic Algorithm are-

1. Definition of objective function.

2. Definition and implementation of genetic representation.

3. Definition and implementation of Genetic operators.

Advantages of Genetic Algorithm (GA) :-

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

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Flowchart of Genetic Algorithm (GA) :-

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Genetic Algorithm Steps :-

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

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Program-3
Illustrate different types of generalized bell membership functions using
Matlab program.

Program:

% Illustration of different generalized bell MFs

x = (-10:0.4:10)’;

b = 2;c = 0;

mf1 = gbell mf(x, [2, b, c]);

mf2 = gbell mf(x, [4, b, c]);

mf3 = gbell mf(x, [6, b, c]);

mf = [mf1 mf2 mf3];

subplot(221); plot(x, mf); title(‘(a) Changing “a”’);

axis([-inf inf 0 1.2]);

a = 5;c = 0;

mf1 = gbell mf(x, [a, 1, c]);

mf2 = gbell mf(x, [a, 2, c]);

mf3 = gbell mf(x, [a, 4, c]);

mf = [mf1 mf2 mf3];

subplot(222); plot(x, mf); title(‘(b) Changing “b”’);

axis([-inf inf 0 1.2]);

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

a = 5;b = 2;

mf1 = gbell mf(x, [a, b, -5]);

mf2 = gbell mf(x, [a, b, 0]);

mf3 = gbell mf(x, [a, b, 5]);

mf = [mf1 mf2 mf3];

subplot(223); plot(x, mf); title(‘(c) Changing “c”’);

axis([-inf inf 0 1.2]);

c = 0;

mf1 = gbell mf(x, [4, 4, c]);

mf2 = gbell mf(x, [6, 6, c]);

mf3 = gbell mf(x, [8, 8, c]);

mf = [mf1 mf2 mf3];

subplot(224); plot(x, mf); title(‘(d) Changing “a” and “b”’);

axis([-inf inf 0 1.2]);

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Program-4
Using Matlab program find the crisp lambda cut set relations for λ = 0.2, the
fuzzy matrix is given by

R=

0.2 0.7 0.8 1

1 0.9 0.5 0.1

0 0.8 1 0.6

0. 0.4 1 0.3

PROGRAM:-
% Enter the matrix value

R=input(‘Enter the matrix value’)

% Enter the lambda value

lambda=input(‘enter the lambda value’)

[m,n]=size(R);

for i=1:m

for j=1:n

if(R(i,j)<lambda)

b(i,j)=0;

else

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

b(i,j)=1;

end

end

end

% output value

display(‘the crisp value is’)

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=

0.2000 0.7000 0.8000 1.0000

1.0000 0.9000 0.5000 0.1000

0 0.8000 1.0000 0.6000

0.2000 0.4000 1.0000 0.3000

Enter the lambda value 0.2

lambda = 0.2000

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

The crisp value is

b=

1111

1110

0111

1111

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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.

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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

E(c) = E(c) + abs(eps) ; % |eps| = eps^2


end;
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,:)], 'g'), drawnow
end;
% After every pass through the set of training patterns the projection of the current decision
plane which is determined by the current weight vector is plotted after the previous projection
has been erased.
WW = WW(1:cw, pr);
E = E(2:c+1)

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Program-6
Write a MATLAB program to show Multi objective optimization in Genetic
Algorithm
The MATLAB program is given as follows.

contents of binsig.m file


function y=binsig(x)
y=1/(1+exp(-x));

contents of bnsig1.m file


function y=binsig1(x)
y=binsig(x)*(1-binsig(x));

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);

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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:

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Program-7
Write a MATLAB program to show Classifier system
The MATLAB program is given as follows.

Contents of bipsig.m file:

function y=bipsig(x)
y=2/(1+exp(–x))–1;

Contents of bipsig1.m file:

function y=bipsig1(x)
y=1/2*(1-bipsig(x))*(1+bipsig(x));

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 -1 -1;1 -1 1 -1];
t=[-1 1 1 -1];
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)=bipsig(zin(j));
end
yin=b2+z*w;
y(I)=bipsig(yin);

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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:

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Practical 8
Write a Matlab program (m.file) to calculate union, intersection, complement
of two fuzzy sets.

Program :-
% enter the two matrix

u=input(‘enter the first matrix’);

v=input(‘enter the second matrix’);

option=input(‘enter the option’);

%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)

option1=input(‘enter whether to find complement for first matrix

or second matrix’);

if (option1==1)

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

[m,n]=size(u);

q=ones(m)-u;

else

q=ones(m)-v;

end

end

OUTPUT:-

(1)

To find union of A and B

enter the first matrix[1 0.5 0.2 0.3]

enter the second matrix[0.5 0.7 0.2 0.4]

enter the option1

w=

1.0000 0.7000 0.2000 0.4000

(2)

To find Intersection of A and B is

enter the first matrix[1 0.5 0.2 0.3]

enter the second matrix[0.5 0.7 0.2 0.4]

enter the option2

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

p=

0.5000 0.5000 0.2000 0.3000

(3)

To find complement of A

enter the first matrix[1 0.5 0.2 0.3]

enter the second matrix[0.5 .7 .2 .4]

enter the option3

enter the whether to find complement for first matrix or second matrix

q=

0 0.5000 0.8000 0.7000

(4)

To find complement of B

enter the first matrix[1 0.5 0.2 0.3]

enter the second matrix[0.5 .7 .2 .4]

enter the option3

enter the whether to find complement for first matrix or second matrix

q=

0.5000 0.3000 0.8000 0.6000

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Program-10

Write a MATLAB program to illustrate Smitch’s Poker player application


The MATLAB program for the above given problem is
Program
b=[0.57 0.0 0.3;0.0 0.0 0.3;0.0 0.57 0.3;0.0 0.47 0.3];
t=[1 1 0 0;1 0 0 1;1 1 1 1];
vp=0.4;
L=2;
x=[1 0 1 1];
s=x;
ns=sum(s);
y=x*b;
con=1;
while con
fori=1:3
if y(i)==max(y)
J=i;
end
end
x=s.*t(J,:);
nx=sum(x);
ifnx/ns >= vp
b(:,J)=L*x(:)/(L-1+nx);
t(J,:)=x(1,:);
con=0;
else
y(J)=-1;
con=1;
end
if y+1==0
con=0;
end
end
disp('Top Down Weights');
disp(t);
disp('Bottom up Weights');
disp(b);

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

You might also like