22SCSE1180094_Shyam Lab File (SCA)!!!!
22SCSE1180094_Shyam Lab File (SCA)!!!!
LAB FILE
SUBJECT- soft computing and applications
SUBMITTED BY : SUBMITTED TO :
Date : 12 / 01 / 2025
Practical:-1. To Implement AND function using Perceptron
The AND function is a logical operation where the output is 1 only if both inputs
are 1; otherwise, the output is 0.
w1, w2, b = 0.5, 0.5, -1
if x >= 0 else 0
in range(len(inputs)): A, B = inputs[i]
target_output = desired_outputs[i]
output = activate(w1 * A + w2 * B + b)
learning_rate * error * A w2 +=
learning_rate * error * B
b += learning_rate * error
== 0:
break
inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]
desired_outputs = [0, 0, 0, 1]
output = activate(w1 * A + w2 * B + b)
• A NOR Gate is a universal logic gate that outputs 1 only when all inputs are 0.
import numpy as np
# Define a simple linearly separable dataset (X: inputs, Y: labels)
weights =
np.zeros(X.shape[1]) bias = 0
learning_rate = 1 epochs =
100
for i in range(len(X)):
if predicted != Y[i]:
bias += update
weight_updates += 1
if weight_updates == 0:
break
# Output the final weights, bias, and number of epochs until convergence
The XOR (Exclusive OR) Gate outputs 1 only when the inputs are
different.
import numpy as np
def bipolar_activation(net):
np.array_equal(output, T[i]):
return weights
X = np.array([[-1, -1, 1], [-1, 1, 1], [1, -1, 1], [1, 1, 1]])
learning_rate = 0.1
max_epochs = 1000 final_weights = madaline_train(X, T, weights,
Import numpy as np
weights =
np.zeros(X.shape[1]) bias = 0
learning_rate = 1 epochs =
100
epoch in range(epochs):
weight_updates = 0
for I in range(len(X)):
# Calculate the perceptron output
if predicted != Y[i]:
bias += update
weight_updates += 1
if weight_updates == 0:
break
# Output the final weights, bias, and number of epochs until convergence
Key Concepts:
1. Forward Propagation:
4. f(x) = 1 / 1+e^−x
import numpy as np class NeuralNetwork: def
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
# Initialize weights
return 1 / (1 + np.exp(-x))
# Input to hidden
self.hidden_output = self.sigmoid(self.hidden_activation)
self.weights_hidden_output) +
self.bias_output
self.predicted_output = self.sigmoid(self.output_activation)
return self.predicted_output
output_error = y - self.predicted_output
np.dot(self.hidden_output.T, output_delta) *
learning_rate
self.bias_output += np.sum(output_delta, axis=0, keepdims=True) * learning_rate
self.weights_input_hidden += np.dot(X.T, hidden_delta) * learning_rate
self.feedforward(X)
self.backward(X, y, learning_rate)
if epoch % 4000 == 0:
np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]])
nn = NeuralNetwork(input_size=2, hidden_size=4,
nn.feedforward(X) print("Predictions
1. Union:
μA B(x)=max(μA(x),μB(x))
2. Intersection:
3. Complement:
Membership function:
μComplement of A(x)=1−μA(x)
import numpy as np
return np.maximum(A, B)
return np.minimum(A, B)
def fuzzy_complement(A):
return 1 - A
union = fuzzy_union(A, B)
intersection = fuzzy_intersection(A, B)
complement_A = fuzzy_complement(A)
complement_B = fuzzy_complement(B)
print(f"Union: {union}")
print(f"Intersection: {intersection}")
print(f"Complement of A: {complement_A}")
print(f"Complement of B: {complement_B}")
3. Union: [0.2 0.6 0.7 0.9] Intersection: [0.1 0.5 0.4 0.8]
Complement of B: [0.9 0.4 0.6 0.2] Complement of A: [0.8 0.5 0.3 0.1]
PRACTICAL 10 : Create two matrices of the dimensions
3x3 and 3x4, respectively, which contain random
numbers as their elements.
Compute The composition of this two fuzzy relation
using both
max-min and max-product composition
import numpy as np
A = np.random.rand(3, 3)
for i in range(A.shape[0]):
for j in range(B.shape[1]):
result[i, j] = np.max(np.minimum(A[i, :], B[:, j]))
return result def
max_product_composition(A, B):
for i in range(A.shape[0]):
for j in range(B.shape[1]):
result[i, j] = np.max(A[i, :] * B[:, j])
return result
max_min_result = max_min_composition(A, B) max_product_result
= max_product_composition(A, B) print("Matrix A (3x3):")
print(A) print("Matrix B
(3x4):") print(B)
(3x4):
Composition:
Composition:
Fuzzy sets extend classical sets by assigning a membership degree (between 0 and 1) to each
element. When dealing with multiple fuzzy sets, their Cartesian product forms a fuzzy relation that
defines the relationship between elements of the two sets.
import random
def generate_fuzzy_set(size):
cartesian_product(set1, set2):
return {(i, j): set1[i] * set2[j] for i in set1 for j in set2} def
main():
generate_fuzzy_set(m) fuzzy_relation =
print(f"{key}: {value}")
if __name__ == "__main__":
main()
8. Write a program in to implement De-Morgan’s Law.
De Morgan’s Laws:
A B=A∩B
This means the complement of the union of two fuzzy sets is equal to
the intersection of their complements.
A∩B=A B
This means the complement of the intersection of two fuzzy sets is
equal to the union of their complements.
import numpy as np
def fuzzy_complement(A):
return 1 - A
return np.maximum(A, B)
return np.minimum(A, B)
print(f"Set B: {B}")
print(f"LHS: {left_hand_side_1}")
print(f"RHS: {right_hand_side_1}")
print(f"LHS: {left_hand_side_2}")
print(f"RHS: {right_hand_side_2}")
ANDNOT Logic:
ANDNOT(A,B)=A B
print(and_not(1, 1))
11. Study of a research paper based on fuzzy logic. Choose any one of the following (a)
Integration of Fuzzy and Deep Learning in Three-Way Decisions.
(b) Aspect based Fuzzy Logic Sentiment Analysis on Social Media Big Data.
(c) Short-Term Forecasting of Convective Weather Affecting Civil Aviation Operations Using
Deep Learning.