0% found this document useful (0 votes)
53 views

Lab Manual - Soft Computing

The document contains details of experiments to be performed in the Soft Computing Lab course. The experiments cover topics like fuzzy logic, neural networks and genetic algorithms. Some key experiments include: 1. Implementing fuzzy logic operations like union, intersection using MATLAB. 2. Modeling a tip prediction system using fuzzy inference in scikit-fuzzy. 3. Generating logic functions like AND, OR, NOT using McCulloch-Pitts neural networks. 4. Solving problems like controlling a water tank using fuzzy inference systems built in MATLAB. The document provides aims, codes and outputs for each experiment to help students understand and implement the concepts covered in the course.

Uploaded by

Harsh official
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Lab Manual - Soft Computing

The document contains details of experiments to be performed in the Soft Computing Lab course. The experiments cover topics like fuzzy logic, neural networks and genetic algorithms. Some key experiments include: 1. Implementing fuzzy logic operations like union, intersection using MATLAB. 2. Modeling a tip prediction system using fuzzy inference in scikit-fuzzy. 3. Generating logic functions like AND, OR, NOT using McCulloch-Pitts neural networks. 4. Solving problems like controlling a water tank using fuzzy inference systems built in MATLAB. The document provides aims, codes and outputs for each experiment to help students understand and implement the concepts covered in the course.

Uploaded by

Harsh official
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

GALGOTIAS UNIVERSITY

SCHOOL OF COMPUTER SCIENCE & ENGINEERING

2022 - 2023
SOFT COMPUTING LAB
BTCS9303

Faculty Name:
Mr.K. Rajkannan
Table of Contents

Sr. Program Remarks


No.
1. To perform Union, Intersection and Complement operations.

2. To implement De-Morgan’s Law.

3. To plot various membership functions.

4. To implement FIS Editor. Use Fuzzy toolbox to model tip value that is
given after a dinner based on quality and service.

5. To implement FIS Editor.

6. Generate ANDNOT function using McCulloch-Pitts neural net.

7. Generate XOR function using McCulloch-Pitts neural net.

8. Hebb Net to classify two-dimensional input patterns in bipolar with


given targets.

9. Perceptron net for an AND function with bipolar inputs and targets.

10. To calculate the weights for given patterns using heteroassociative


neural net.
Experiment-01
Aim: - Write A Program in “MATLAB” to perform the following
operations: -
1. Union,
2. Intersection,
3. Set Difference & Complement Operations.

Program:
M = input ("Enter First Matrix");
A= input ("Enter First Matrix");

U = union (M, A);


I = intersection (M, A);
S0 = setdiff(M,A);
S1 = setdiff(A,M);
P0 =(1-M);
P1 =(1-A);

display ("Union of twosets->");


display(U);

display ("Interaction of two sets->");


display(I);

display ("Set Difference of M andA->");


display(S0);

display (“Set Difference of A andM->”);


display(S1);

display ("Complement of Two Sets M andA->");


display(P0);
display(P1);
OUTPUT: -
Experiment-02
AIM :- Write A Program in “MATLAB” to implement the following De-
Morgan’s Laws:
1.(A+B)’=A’.B’
2. (A.B)’=A’+B’

CODE :-

% Let M and A be column vectors

M = [0 1 0 1]'

A = [0 0 1 1]'

% (i) (A+B)’=A’.B’

M_or_A = not(M|A)

DeMorgLaw1 = (not(M)& not(A))

% (ii) (A.B)’=A’+B’

M_and_A = not(M&A)

DeMorgLaw2 = (not(M) | not(A))


OUTPUT :-
EXPERIMENT-03
AIM: Write A Program to plot various Membership
functions in Matlab.
i.Triangle-Shaped Membership function:
Code:

x = 0:0.1:10;
y = trimf (x, [3 6 8]);
plot (x, y)
xlabel ('trimf, P = [3 6 8]')
ylim ([-0.05 1.05])

Output:
ii.Trapezoid-Shaped Membership function:
Code:
x = 0:0.1:10;
y = trapmf (x, [1 5 7 8]);
plot (x, y)
xlabel ('trapmf, P = [1 5 7 8]')
ylim ([-0.05 1.05])

Output:

iii.Guassian Membership function:


Code:
X = 0:0.1:10;
y = gaussmf (x, [2 5]);
plot (x, y)
xlabel ('gaussmf, P = [2 5]')

Output:
iv. Sigmoid Membershipfunction:
Code:
X = 0:0.1:10;
y = sigmf (x, [2 4]);
plot (x, y)
xlabel ('sigmf, P = [2 4]')
ylim ([-0.05 1.05])

output:
Experiment-04
AIM: Write a Program in MATLAB to use Fuzzy toolbox to tip value
that is given after a dinner which can be-> not good, satisfying, good
and delightful and service which is poor, average or good and the tip
value will range from Rs. 10 to 100.

CODE:
import NumPy as np

import skfuzzy as fuzz

from skfuzzy import control as ctrl

quality = ctrl. Antecedent (np.arange (0, 11, 1), 'quality')

service = ctrl. Antecedent (np.arange(0, 11, 1), 'service')

tip = ctrl. Consequent (np.arange(0, 26, 1), 'tip')

quality.automf (3)

service.automf(3)

tip['low'] = fuzz.trimf (tip. universe, [0, 0, 13])

tip['medium'] = fuzz.trimf (tip. universe, [0, 13, 25])

tip['high'] = fuzz.trimf (tip. universe, [13, 25, 25])

quality['average']. view()

service.view ()

tip.view ()

rule1 = ctrl.Rule (quality['poor'] | service['poor'], tip['low'])


rule2 = ctrl.Rule (service['average'], tip['medium'])

rule3 = ctrl.Rule (service['good'] | quality['good'], tip['high'])

rule.view()

tipping_ctrl = ctrl. ControlSystem ([rule1, rule2, rule3])

tipping = ctrl. ControlSystemSimulation (tipping_ctrl)

tipping. Input ['quality'] = 6.5

tipping. Input ['service'] = 9.8

tipping. compute()

print (tipping. output['TIP'])

tip.view (sim = tipping)

OUTPUT:
Experiment-04
AIM: Write a Program in MATLAB to use Fuzzy toolbox to tip value
that is given after a dinner which can be-> not good, satisfying, good
and delightful and service which is poor, average or good and the tip
value will range from Rs. 10 to 100.

CODE:
import NumPy as np

import skfuzzy as fuzz

from skfuzzy import control as ctrl

quality = ctrl. Antecedent (np.arange (0, 11, 1), 'quality')

service = ctrl. Antecedent (np.arange(0, 11, 1), 'service')

tip = ctrl. Consequent (np.arange(0, 26, 1), 'tip')

quality.automf (3)

service.automf(3)

tip['low'] = fuzz.trimf (tip. universe, [0, 0, 13])

tip['medium'] = fuzz.trimf (tip. universe, [0, 13, 25])

tip['high'] = fuzz.trimf (tip. universe, [13, 25, 25])

quality['average']. view()

service.view ()

tip.view ()

rule1 = ctrl.Rule (quality['poor'] | service['poor'], tip['low'])


rule2 = ctrl.Rule (service['average'], tip['medium'])

rule3 = ctrl.Rule (service['good'] | quality['good'], tip['high'])

rule.view()

tipping_ctrl = ctrl. ControlSystem ([rule1, rule2, rule3])

tipping = ctrl. ControlSystemSimulation (tipping_ctrl)

tipping. Input ['quality'] = 6.5

tipping. Input ['service'] = 9.8

tipping. compute()

print (tipping. output['TIP'])

tip.view (sim = tipping)

OUTPUT:
EXPERIMENT - 5

Aim: To implement FIS Editor.


Introduction:
FIS stands for Fuzzy Inference System. In FIS fuzzy rules are used for approximate
reasoning. It is the logical framework that allows us to design reasoning systems based on
fuzzy set theory.
Title: Water Tank Problem: -
FIS editor consists of following units: -
i) Input: The Water Level is considered as the Inputvariable
ii) Inference System
iii) Output: The Valve status is taken as OutputVariable.

The Input-Output Variable’s Membership functions should be plotted along with their ranges:
The fuzzy Rules defined for water tank are: -
IF level is ok, THEN there is no change in valve.
IF level is low, THEN valve is open in fast mode.
IF level is high, THEN valve is closed in fast mode.

The result is displayed as plots of input-output membership functions:

Water Level is indicated by:


ok, low, high
Valve Status is indicated by:
no change, open fast, closed fast
View-rule Viewer:
The output in accordance with the input and rules provided by user is shown as:
OUTPUT:
Experiment06
AIM: Generate ANDNOT function using McCulloch-Pitts neural net by MATLAB program.

CODE:

#ANDNOT function using McCulloch-Pitts neuron:


clear ;
clc;
#Getting weights and threshold value:
disp('Enter the weights');
w1=input('Weight w1=');
w2=input('Weight w2=');

disp('Enter threshold value');


theta=input('theta=');
y=[0 0 0 0];
x1=[0 0 1 1];
x2=[0 1 0 1];
z=[0 0 1 0];

con=1;
while con

zin = x1*w1+x2*w2;
for i=1:4
if zin(i)>=theta

y(i)=1;
else y(i)=0;
end
end

disp('Output of net=');
disp(y);
if y==z

con=0;
else

disp('Net is not learning Enter another set of weights and threshold value');
w1=input('Weight w1=');
w2=input('Weight w2=');
theta=input('theta=');
end

end

disp('McCulloch Pitts Net for ANDNOT function');


disp('Weights of neuron');
disp(w1);

disp(w2);
disp('Threshold value=');
disp(theta);

OUTPUT:-
Enter the weights
Weight w1=1
Weight w2=1

Enter threshold value theta=1


Output of net= 0 1 1 1

Net is not learning


Enter another set of weights and threshold value
Weight w1=1
Weight w2=-1
theta=1
Output of net=0 0 1 0
McCulloch Pitts Net for ANDNOT function
Weights of neuron
1
-1
Threshold value= 1
Experiment-07
AIM: Generate XOR function using McCulloch-Pitts neural net by MATLAB program.
#XOR function using McCulloch-Pitts neuron:
clear;
clc;
#Getting weights and threshold value:
disp('Enter the weights');
w11=input('Weight w11=');
w12=input('Weight w12=');
w21=input('Weight w21=');
w22=input('Weight w22=');
v1=input('Weight v1=');
v2=input('Weight v2=');
disp('Enter threshold value');
theta=input('theta=');
x1=[0 0 1 1];
x2=[0 1 0 1];
z=[0 1 1 0];
con=1;
while con
zin1 = x1*w11+x2*w21;
zin2 = x1*w21+x2*w22;
for i=1:4

if zin1(i)>=theta
y1(i)=1;
else
y1(i)=0;
end

if zin2(i)>=theta
y2(i)=1;
else
y2(i)=0;

end
end

yin=y1*v1+y2*v2;
for i=1:4
if yin(i)>=theta;

y(i)=1;
else y(i)=0;
end

end
disp('Output of net=');
disp(y);
if y==z
con=0;
else
disp('Net is not learning Enter another set of weights and threshold value');
w11=input('Weight w11=');
w12=input('Weight w12=');
w21=input('Weight w21=');
w22=input('Weight w22=');
v1=input('Weight v1=');
v2=input('Weight v2=');
theta=input('theta=');

end
end

disp('McCulloch Pitts Net for XOR function');


disp('Weights of neuron Z1');

disp(w11);
disp(w21);

disp('Weights of neuron Z2');


disp(w12);
disp(w22);

disp('Weights of neuron Y');


disp(v1);
disp(v2);
disp('Threshold value=');
disp(theta);

OUTPUT:-
Enter the weights
Weight w11=1
Weight w12=-1
Weight w21=-1
Weight w22=1
Weight v1=1
Weight v2=1
Enter threshold value
theta=1
Output of net= 0 1 1 0

McCulloch Pitts Net for XOR function


Weights of neuron z1

1
-1
Weights of neuron z2
-1
1

Weights of neuron y
1
1

Threshold value= 1
EXPERIMENT - 8
AIM: Write a MATLAB program for Hebb Net to classify two dimensional input patterns in
bipolar with their targets given below:
‘*’ indicates a‘+’ And ‘.’ Indicates ‘-’
***** *****

*…. *….
***** *****
*…. *….
***** *
#Hebb Net to classify Two -Dimensional input patterns.
clear;
clc;
#Input Pattern:
E=[1 1 1 1 1 -1-1 -1 1 1 11 1 -1 -1 -1 1 1 1 1];
F=[1 1 1 1 1 -1-1 -1 1 1 11 1 -1 -1 -1 1 -1 -1 -1];
X(1,1:20)=E;
X(2,1:20)=F;
w(1:20)=0;
t=[1 -1];
b=0;
for i=1:2

w=w+X(i,1:20)*t(i);
b=b+t(i);
end

disp('Weight Matrix');
disp(w);
disp('Bias');
disp(b);
OUTPUT: -

Weight Matrix

00000000000000000222

Bias 0
EXPERIMENT - 09
AIM: Write a MATLAB program for Perceptron net for an AND function with bipolar inputs
and targets.
#Perceptron for AND Function:
clear;
clc;
x=[1 1 -1 -1;1 -1 1 -1];
t=[1 -1 -1 -1];
w=[0 0];
b=0;

alpha=input('Enter Learning rate=');


theta=input('Enter Threshold Value=');
con=1;
epoch=0;
while con
con=0;
for i=1:4
yin=b+x(1,i)*w(1)+x(2,i)*w(2);
if yin>theta
y=1;

end
if yin<=theta & yin>=-theta
y=0;
end

if yin<-theta
y=-1;
end

if y-t(i)
con=1;
for j=1:2
w(j)=w(j)+alpha*t(i)*x(j,i);

end
b=b+alpha*t(i);

end
end

epoch=epoch+1;
end
disp('Perceptron for AND Function');
disp('Final Weight Matrix');
disp(w);
disp('Final Bias');
disp(b);

OUTPUT: -

Enter Learning rate=1

Enter Threshold Value=0.5

Perceptron for AND Function

Final Weight Matrix

1 1

Final Bias

-1
EXPERIMENT - 10
AIM: Write a M-file to calculate the weights for the following patterns using hetero-associative
neural net for mapping four input vectors to two output vectors:

S1 S2 S3 S4 t1 t2
1 1 0 0 1 0

1 0 1 0 1 0

1 1 1 0 0 1
0 1 1 0 0 1

CODE: -

#Hetero-associative neural net for mapping input vectors to output vectors.

clear;

clc;

x= [1 1 0 0;1 0 1 0;1 1 1 0;0 1 1 0];

t= [1 0;1 0;0 1;0 1];

w=zeros (4,2);

for i=1:4

w= w+x (i,1:4)'*t(i,1:2);

end

disp('Weight Matrix');

disp(w);

OUTPUT: -

Weight Matrix

2 1
1 2

1 2

0 0

You might also like