Dr B R Ambedkar National Institute of
Technology Jalandhar
Grand Trunk Road, Barnala - Amritsar
Bypass Road,Jalandhar, Punjab 144011
DEPARTMENT OF INFORMATION TECHNOLOGY
ITPC-322 SOFT COMPUTING CONCEPTS LAB
B. TECH IT – III YEAR (6th SEMESTER)
Submi ed by:
Name: Saloni Aggarwal
Roll No. 21124095
Group – A4
Submi ed to
Dr Neeraj Kumar
1
2
Index
[Link] Experiment name Page No. Date Remarks
3
4
Lab – 1 (Part -1)
Logic Gates in Python.
1. AND Gate:
The AND gate produces a high output (1) only if all its inputs are high (1).
Python Code:
def and_gate(input1, input2): return input1 and input2
2. OR Gate:
The OR gate produces a high output (1) if at least one of its inputs is high (1).
Python Code:
def or_gate(input1, input2): return input1 or input2
3. NOT Gate:
The NOT gate produces an output that is the opposite of its input.
Python Code:
def not_gate(input1): return not input1
4. XOR Gate:
The XOR (exclusive OR) gate produces a high output (1) only if the number of high inputs is odd.
Python Code:
def xor_gate(input1, input2): return (input1 or input2) and not (input1 and input2)
5. NAND Gate:
The NAND gate produces a low output (0) only if all its inputs are high (1).
Python Code:
def nand_gate(input1, input2): return not (input1 and input2)
6. NOR Gate:
The NOR gate produces a low output (0) if at least one of its inputs is high (1).
5
PYTHON CODE:
def and_gate(input1, input2):
return input1 and input2
def or_gate(input1, input2):
return input1 or input2
def not_gate(input1):
return not input1
def xor_gate(input1, input2):
return (input1 or input2) and not (input1 and input2)
def nand_gate(input1, input2):
return not (input1 and input2)
def nor_gate(input1, input2):
return not (input1 or input2)
def main():
# Example inputs
input_a = True
input_b = False
# AND gate
result_and = and_gate(input_a, input_b)
print(f"AND gate: {input_a} AND {input_b} = {result_and}")
# OR gate
result_or = or_gate(input_a, input_b)
print(f"OR gate: {input_a} OR {input_b} = {result_or}")
# NOT gate
result_not = not_gate(input_a)
print(f"NOT gate: NOT {input_a} = {result_not}")
# XOR gate
result_xor = xor_gate(input_a, input_b)
6
print(f"XOR gate: {input_a} XOR {input_b} = {result_xor}")
# NAND gate
result_nand = nand_gate(input_a, input_b)
print(f"NAND gate: {input_a} NAND {input_b} = {result_nand}")
# NOR gate
result_nor = nor_gate(input_a, input_b)
print(f"NOR gate: {input_a} NOR {input_b} = {result_nor}")
if __name__ == "__main__":
main()
OUTPUT :
7
Lab – 2
Implementa on of Fuzzy and Crisp in Python
THEORY:
Crisp Sets:
Defini on: Crisp sets, also known as classical or tradi onal sets, deal with well-defined,
dis nct elements.
Characteris cs: Each element either belongs to the set (membership value of 1) or does not
belong (membership value of 0).
Representa on: Typically represented using mathema cal nota on, where membership is
binary (0 or 1).
Example: The set of prime numbers less than 10 is a crisp set.
Fuzzy Sets:
Defini on: Fuzzy sets introduce the concept of par al membership, allowing elements to
belong to a set to varying degrees between 0 and 1.
Characteris cs: Membership values are not strictly binary but can be any real number
between 0 and 1, expressing the degree of belongingness.
Representa on: O en represented using linguis c terms (e.g., "very high," "medium,"
"low") to capture the uncertainty or vagueness.
Example: The set of tall people in a room, where height membership values can range from 0
to 1 based on a defined criterion.
Key Dis nc on:
Crisp sets have clear, well-defined boundaries for membership (either in or out).
Fuzzy sets accommodate uncertainty and vagueness, allowing for par al membership and
gradual transi ons between in and out.
PYTHON CODE:
import numpy as np
def Crisp(taste, behav):
if (taste and behav):
return 200
elif (taste and not behav):
return 100
elif (not taste and behav):
return 100
elif (not taste and not behav):
8
return 0
def sigmoid(x):
return float(1 / (1 + [Link](-x)))
def fuzzy():
tas = float(input("Enter the taste"))
beh = float(input("Enter the behav"))
print(sigmoid(tas)*10*sigmoid(beh)*10)
print()
def main():
tas = int(input("Enter the taste[0-1]"))
beh = int(input("Enter the behav[0-1]"))
print(Crisp(tas, beh))
fuzzy()
if __name__ == "__main__":
main()
OUTPUT:
9
LAB: 3 Implemen ng Fuzzy Logic in MATLAB
CODE:
% Define input variables
food = 5; % Scale of 1 to 10 (where 1 is very bad and 10 is very good)
behavior = 8; % Scale of 1 to 10 (where 1 is very bad and 10 is very good)
% Define membership func ons for food quality
food_bad = gaussmf(food, [1 1]);
food_good = gaussmf(food, [1 5]);
food_very_good = gaussmf(food, [1 10]);
% Define membership func ons for behavior
behavior_bad = gaussmf(behavior, [1 1]);
behavior_good = gaussmf(behavior, [1 5]);
behavior_very_good = gaussmf(behavior, [1 10]);
% Define output membership func ons
p_low = gaussmf(0, [1 0]);
p_medium = gaussmf(0.15, [1 0.5]);
p_high = gaussmf(0.3, [1 1]);
% Fuzzifica on - determine degree of membership for each input value
food_membership = [food_bad food_good food_very_good];
behavior_membership = [behavior_bad behavior_good behavior_very_good];
% Apply fuzzy rules
rule1 = min(food_bad, behavior_bad);
rule2 = min(food_good, behavior_good);
rule3 = min(food_very_good, behavior_very_good);
% Apply aggrega on (combining all rules)
aggregated = max(rule1, max(rule2, rule3));
% Defuzzifica on (calculate the crisp output value)
p = defuzz([0 0.15 0.3], aggregated, 'centroid');
disp(['The p amount is: ', num2str( p)]);
OUTPUT:
10
Fig 3.1 All cases with values of service and food.
Fig 3.2 All rules are shown with weight and name.
11
Fig 3.3 All input and output with func on and parameters.
Fig 3.4 Input and output are represented.
12
LAB 4: Converting coloured image to grayscale in MATLAB.
THEORY:
Converting a colored image to grayscale in MATLAB involves transforming each pixel's RGB (Red,
Green, Blue) color values into a single grayscale intensity value. This process can be done using
several methods, but a common one is luminance-based conversion, which mimics the human
perception of light.
RGB to Grayscale Conversion: In a colored image, each pixel is represented by three values
corresponding to its Red, Green, and Blue components. Grayscale images, on the other hand,
represent each pixel with a single intensity value representing its brightness.
CODE:
RGB = imread('[Link]');
imshow(RGB)
[h,s,v] = imsplit(HSV);
saturationFactor = 2;
s_sat = s*saturationFactor;
HSV_sat = cat(3,h,s_sat,v);
imshow(HSV_sat)
RGB_sat = hsv2rgb(HSV_sat);
I = rgb2gray(RGB);
Figure
imshow(I)
OUTPUT:
13
LAB 5: Creating a perceptron using MATLAB.
THEORY:
A perceptron is a basic unit of a neural network used for binary classification tasks. It takes
multiple input values, each with an associated weight, computes a weighted sum, and applies
an activation function (often a step function) to produce a binary output. The weights are
adjusted during training based on the error between the predicted output and the true output
using a learning algorithm (e.g., perceptron learning rule). Perceptrons are simple and
efficient for linearly separable problems but have limitations for complex tasks.
To create a perceptron using MATLAB:
1. Initialize Weights: Start with random weights.
2. Define Activation Function: Typically, use the step function for binary classification.
3. Training: Adjust weights iteratively using the perceptron learning rule.
4. Testing: Evaluate the perceptron's performance with new data.
CODE:
x = [80 30 10 10; 15 10 20 10];
t = [80 20 10 10];
% Create a perceptron
net = perceptron;
% Set the number of epochs (e.g., 992)
epochs = 992;
% Train the perceptron with the specified number of epochs
[Link] = epochs;
net = train(net, x, t);
% View the trained network
view(net);
14
OUTPUT:
Fig 5.1 Ini al values of perceptron’s variables and representa on of perceptron with ac va on func on step.
Fig 5.2 Performance of model over 992 epochs.
15