Fix the bug in fuzzy_operations.py

This commit is contained in:
Shreya123714 2023-10-28 14:09:18 +05:30
parent 231b33ea1a
commit a7b7a9169a

View File

@ -7,18 +7,15 @@ import numpy as np
Source: https://en.wikipedia.org/wiki/Fuzzy_set Source: https://en.wikipedia.org/wiki/Fuzzy_set
""" """
class FuzzySet: class FuzzySet:
""" """
A class for representing and A class for representing and
manipulating triangular fuzzy sets. manipulating triangular fuzzy sets.
Attributes: Attributes:
name (str): The name or label of the fuzzy set. name (str): The name or label of the fuzzy set.
a (float): The left boundary of the fuzzy set. a (float): The left boundary of the fuzzy set.
b (float): The peak (central) value of the fuzzy set. b (float): The peak (central) value of the fuzzy set.
c (float): The right boundary of the fuzzy set. c (float): The right boundary of the fuzzy set.
Methods: Methods:
membership(x): Calculate the membership value membership(x): Calculate the membership value
of an input 'x' in the fuzzy set. of an input 'x' in the fuzzy set.
@ -37,7 +34,6 @@ class FuzzySet:
""" """
Initializes a triangular fuzzy set Initializes a triangular fuzzy set
with the given parameters. with the given parameters.
Args: Args:
name (str): The name or label of the fuzzy set. name (str): The name or label of the fuzzy set.
a (float): The left boundary of the fuzzy set. a (float): The left boundary of the fuzzy set.
@ -50,113 +46,86 @@ class FuzzySet:
self.peak = peak # Peak value self.peak = peak # Peak value
self.right_boundary = right_boundary # Right boundary self.right_boundary = right_boundary # Right boundary
def membership(self, x): def membership(self, x):
""" """
Calculate the membership value of Calculate the membership value of
an input 'x' in the fuzzy set. an input 'x' in the fuzzy set.
Args: Args:
x (float): The input value for x (float): The input value for
which the membership is calculated. which the membership is calculated.
Returns: Returns:
float: The membership value of 'x' in float: The membership value of 'x' in
the fuzzy set. the fuzzy set.
""" """
if x <= self.a or x >= self.c: if x <= self.left_boundary or x >= self.right_boundary:
return 0 return 0
elif self.a < x <= self.b: elif self.left_boundary < x <= self.peak:
return (x - self.a) / (self.b - self.a) return (x - self.left_boundary) / (self.peak - self.left_boundary)
elif self.b < x < self.c: elif self.peak < x < self.right_boundary:
return (self.c - x) / (self.c - self.b) return (self.right_boundary - x) / (self.right_boundary - self.peak)
def union(self, other): def union(self, other):
""" """
Calculate the union of this fuzzy set Calculate the union of this fuzzy set
with another fuzzy set. with another fuzzy set.
Args: Args:
other (FuzzySet): Another fuzzy set other (FuzzySet): Another fuzzy set
to union with. to union with.
Returns: Returns:
FuzzySet: A new fuzzy FuzzySet: A new fuzzy
set representing the union. set representing the union.
""" """
union_name = f"{self.name} {other.name}" union_name = f"{self.name} {other.name}"
return FuzzySet( return FuzzySet(
union_name, union_name,
min(self.a, other.a), min(self.left_boundary, other.left_boundary),
max(self.c, other.c), max(self.right_boundary, other.right_boundary),
(self.b + other.b) / 2, (self.peak + other.peak) / 2,
) )
def intersection(self, other): def intersection(self, other):
""" """
Calculate the intersection of this Calculate the intersection of this
fuzzy set with another fuzzy set. fuzzy set with another fuzzy set.
Args: Args:
other (FuzzySet): Another fuzzy set to intersect with. other (FuzzySet): Another fuzzy set to intersect with.
Returns: Returns:
FuzzySet: A new fuzzy set representing the intersection. FuzzySet: A new fuzzy set representing the intersection.
""" """
intersection_name = f"{self.name}{other.name}" intersection_name = f"{self.name}{other.name}"
return FuzzySet( return FuzzySet(
intersection_name, intersection_name,
max(self.a, other.a), max(self.left_boundary, other.left_boundary),
min(self.c, other.c), min(self.right_boundary, other.right_boundary),
(self.b + other.b) / 2, (self.peak + other.peak) / 2,
) )
def complement(self): def complement(self):
""" """
Calculate the complement (negation) of this fuzzy set. Calculate the complement (negation) of this fuzzy set.
Returns: Returns:
FuzzySet: A new fuzzy set representing the complement. FuzzySet: A new fuzzy set representing the complement.
""" """
complement_name = f"¬{self.name}" complement_name = f"¬{self.name}"
return FuzzySet(complement_name, 1 - self.c, 1 - self.a, 1 - self.b) return FuzzySet(complement_name, 1 - self.right_boundary, 1 - self.left_boundary, 1 - self.peak)
def plot(self): def plot(self):
""" """
Plot the membership function of the fuzzy set. Plot the membership function of the fuzzy set.
""" """
x = np.linspace(0, 1, 1000) x = np.linspace(0, 1, 1000)
y = [self.membership(xi) for xi in x] y = [self.membership(xi) for xi in x]
plt.plot(x, y, label=self.name) plt.plot(x, y, label=self.name)
def __str__(self): def __str__(self):
return f"{self.name}: [{self.a}, {self.b}, {self.c}]" return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]"
# Example usage:
if __name__ == "__main__":
A = FuzzySet("A", 0, 0.5, 1)
B = FuzzySet("B", 0.2, 0.7, 1)
A.plot()
B.plot()
plt.xlabel("x")
plt.ylabel("Membership")
plt.legend()
plt.show()
union_ab = A.union(B)
intersection_ab = A.intersection(B)
complement_a = A.complement()
union_ab.plot()
intersection_ab.plot()
complement_a.plot()
plt.xlabel("x")
plt.ylabel("Membership")
plt.legend()
plt.show()