Skip to content

Commit 4af9b1f

Browse files
authored
Update fuzzy_operations.py
1 parent a1180d8 commit 4af9b1f

File tree

1 file changed

+15
-134
lines changed

1 file changed

+15
-134
lines changed

fuzzy_logic/fuzzy_operations.py

Lines changed: 15 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,30 @@
1-
"""
2-
By @Shreya123714
3-
4-
https://en.wikipedia.org/wiki/Fuzzy_set
5-
"""
6-
71
from __future__ import annotations
8-
92
from dataclasses import dataclass
10-
113
import matplotlib.pyplot as plt
124
import numpy as np
135

14-
156
@dataclass
167
class FuzzySet:
178
"""
189
A class for representing and manipulating triangular fuzzy sets.
19-
Attributes:
20-
name: The name or label of the fuzzy set.
21-
left_boundary: The left boundary of the fuzzy set.
22-
peak: The peak (central) value of the fuzzy set.
23-
right_boundary: The right boundary of the fuzzy set.
24-
Methods:
25-
membership(x): Calculate the membership value of an input 'x' in the fuzzy set.
26-
union(other): Calculate the union of this fuzzy set with another fuzzy set.
27-
intersection(other): Calculate the intersection of this fuzzy set with another.
28-
complement(): Calculate the complement (negation) of this fuzzy set.
29-
plot(): Plot the membership function of the fuzzy set.
30-
31-
>>> sheru = FuzzySet("Sheru", 0.4, 1, 0.6)
32-
>>> sheru
33-
FuzzySet(name='Sheru', left_boundary=0.4, peak=1, right_boundary=0.6)
34-
>>> str(sheru)
35-
'Sheru: [0.4, 1, 0.6]'
36-
37-
>>> siya = FuzzySet("Siya", 0.5, 1, 0.7)
38-
>>> siya
39-
FuzzySet(name='Siya', left_boundary=0.5, peak=1, right_boundary=0.7)
40-
41-
# Complement Operation
42-
>>> sheru.complement()
43-
FuzzySet(name='¬Sheru', left_boundary=0.4, peak=0.6, right_boundary=0)
44-
>>> siya.complement() # doctest: +NORMALIZE_WHITESPACE
45-
FuzzySet(name='¬Siya', left_boundary=0.30000000000000004, peak=0.5,
46-
right_boundary=0)
47-
48-
# Intersection Operation
49-
>>> siya.intersection(sheru)
50-
FuzzySet(name='Siya ∩ Sheru', left_boundary=0.5, peak=0.6, right_boundary=1.0)
51-
52-
# Membership Operation
53-
>>> sheru.membership(0.5)
54-
0.16666666666666663
55-
>>> sheru.membership(0.6)
56-
0.0
57-
58-
# Union Operations
59-
>>> siya.union(sheru)
60-
FuzzySet(name='Siya U Sheru', left_boundary=0.4, peak=0.7, right_boundary=1.0)
6110
"""
62-
6311
name: str
6412
left_boundary: float
6513
peak: float
6614
right_boundary: float
6715

6816
def __str__(self) -> str:
69-
"""
70-
>>> FuzzySet("fuzzy_set", 0.1, 0.2, 0.3)
71-
FuzzySet(name='fuzzy_set', left_boundary=0.1, peak=0.2, right_boundary=0.3)
72-
"""
73-
return (
74-
f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]"
75-
)
17+
return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]"
7618

7719
def complement(self) -> FuzzySet:
78-
"""
79-
Calculate the complement (negation) of this fuzzy set.
80-
Returns:
81-
FuzzySet: A new fuzzy set representing the complement.
82-
83-
>>> FuzzySet("fuzzy_set", 0.1, 0.2, 0.3).complement()
84-
FuzzySet(name='¬fuzzy_set', left_boundary=0.7, peak=0.9, right_boundary=0.8)
85-
"""
8620
return FuzzySet(
8721
f"¬{self.name}",
8822
1 - self.right_boundary,
8923
1 - self.left_boundary,
9024
1 - self.peak,
9125
)
9226

93-
def intersection(self, other) -> FuzzySet:
94-
"""
95-
Calculate the intersection of this fuzzy set
96-
with another fuzzy set.
97-
Args:
98-
other: Another fuzzy set to intersect with.
99-
Returns:
100-
A new fuzzy set representing the intersection.
101-
102-
>>> FuzzySet("a", 0.1, 0.2, 0.3).intersection(FuzzySet("b", 0.4, 0.5, 0.6))
103-
FuzzySet(name='a ∩ b', left_boundary=0.4, peak=0.3, right_boundary=0.35)
104-
"""
27+
def intersection(self, other: FuzzySet) -> FuzzySet:
10528
return FuzzySet(
10629
f"{self.name}{other.name}",
10730
max(self.left_boundary, other.left_boundary),
@@ -110,45 +33,15 @@ def intersection(self, other) -> FuzzySet:
11033
)
11134

11235
def membership(self, x: float) -> float:
113-
"""
114-
Calculate the membership value of an input 'x' in the fuzzy set.
115-
Returns:
116-
The membership value of 'x' in the fuzzy set.
117-
118-
>>> a = FuzzySet("a", 0.1, 0.2, 0.3)
119-
>>> a.membership(0.09)
120-
0.0
121-
>>> a.membership(0.1)
122-
0.0
123-
>>> a.membership(0.11)
124-
0.09999999999999995
125-
>>> a.membership(0.4)
126-
0.0
127-
>>> FuzzySet("A", 0, 0.5, 1).membership(0.1)
128-
0.2
129-
>>> FuzzySet("B", 0.2, 0.7, 1).membership(0.6)
130-
0.8
131-
"""
13236
if x <= self.left_boundary or x >= self.right_boundary:
13337
return 0.0
13438
elif self.left_boundary < x <= self.peak:
13539
return (x - self.left_boundary) / (self.peak - self.left_boundary)
13640
elif self.peak < x < self.right_boundary:
13741
return (self.right_boundary - x) / (self.right_boundary - self.peak)
138-
msg = f"Invalid value {x} for fuzzy set {self}"
139-
raise ValueError(msg)
140-
141-
def union(self, other) -> FuzzySet:
142-
"""
143-
Calculate the union of this fuzzy set with another fuzzy set.
144-
Args:
145-
other (FuzzySet): Another fuzzy set to union with.
146-
Returns:
147-
FuzzySet: A new fuzzy set representing the union.
42+
raise ValueError(f"Invalid value {x} for fuzzy set {self}")
14843

149-
>>> FuzzySet("a", 0.1, 0.2, 0.3).union(FuzzySet("b", 0.4, 0.5, 0.6))
150-
FuzzySet(name='a U b', left_boundary=0.1, peak=0.6, right_boundary=0.35)
151-
"""
44+
def union(self, other: FuzzySet) -> FuzzySet:
15245
return FuzzySet(
15346
f"{self.name} U {other.name}",
15447
min(self.left_boundary, other.left_boundary),
@@ -157,39 +50,27 @@ def union(self, other) -> FuzzySet:
15750
)
15851

15952
def plot(self):
160-
"""
161-
Plot the membership function of the fuzzy set.
162-
"""
16353
x = np.linspace(0, 1, 1000)
16454
y = [self.membership(xi) for xi in x]
165-
16655
plt.plot(x, y, label=self.name)
167-
56+
plt.xlabel("x")
57+
plt.ylabel("Membership")
58+
plt.legend()
59+
plt.show()
16860

16961
if __name__ == "__main__":
17062
from doctest import testmod
171-
17263
testmod()
64+
65+
# Sample Fuzzy Sets
17366
a = FuzzySet("A", 0, 0.5, 1)
17467
b = FuzzySet("B", 0.2, 0.7, 1)
17568

69+
# Plot the fuzzy sets
17670
a.plot()
17771
b.plot()
17872

179-
plt.xlabel("x")
180-
plt.ylabel("Membership")
181-
plt.legend()
182-
plt.show()
183-
184-
union_ab = a.union(b)
185-
intersection_ab = a.intersection(b)
186-
complement_a = a.complement()
187-
188-
union_ab.plot()
189-
intersection_ab.plot()
190-
complement_a.plot()
191-
192-
plt.xlabel("x")
193-
plt.ylabel("Membership")
194-
plt.legend()
195-
plt.show()
73+
# Plot union, intersection, and complement of sets
74+
a.union(b).plot()
75+
a.intersection(b).plot()
76+
a.complement().plot()

0 commit comments

Comments
 (0)