Skip to content

Commit 67b8af8

Browse files
authored
Update 2 fuzzy_operations.py
1 parent a1180d8 commit 67b8af8

File tree

1 file changed

+6
-107
lines changed

1 file changed

+6
-107
lines changed

fuzzy_logic/fuzzy_operations.py

Lines changed: 6 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
"""
2-
By @Shreya123714
3-
4-
https://en.wikipedia.org/wiki/Fuzzy_set
5-
"""
6-
71
from __future__ import annotations
82

93
from dataclasses import dataclass
@@ -14,75 +8,19 @@
148

159
@dataclass
1610
class FuzzySet:
17-
"""
18-
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)
61-
"""
62-
11+
""" A class for representing and manipulating triangular fuzzy sets. """
6312
name: str
6413
left_boundary: float
6514
peak: float
6615
right_boundary: float
6716

6817
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-
"""
7318
return (
7419
f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]"
7520
)
7621

7722
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-
"""
23+
""" Calculate the complement (negation) of this fuzzy set. """
8624
return FuzzySet(
8725
f"¬{self.name}",
8826
1 - self.right_boundary,
@@ -91,17 +29,7 @@ def complement(self) -> FuzzySet:
9129
)
9230

9331
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-
"""
32+
""" Calculate the intersection of this fuzzy set with another fuzzy set. """
10533
return FuzzySet(
10634
f"{self.name}{other.name}",
10735
max(self.left_boundary, other.left_boundary),
@@ -110,25 +38,7 @@ def intersection(self, other) -> FuzzySet:
11038
)
11139

11240
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-
"""
41+
""" Calculate the membership value of an input 'x' in the fuzzy set. """
13242
if x <= self.left_boundary or x >= self.right_boundary:
13343
return 0.0
13444
elif self.left_boundary < x <= self.peak:
@@ -139,16 +49,7 @@ def membership(self, x: float) -> float:
13949
raise ValueError(msg)
14050

14151
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.
148-
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-
"""
52+
""" Calculate the union of this fuzzy set with another fuzzy set. """
15253
return FuzzySet(
15354
f"{self.name} U {other.name}",
15455
min(self.left_boundary, other.left_boundary),
@@ -157,9 +58,7 @@ def union(self, other) -> FuzzySet:
15758
)
15859

15960
def plot(self):
160-
"""
161-
Plot the membership function of the fuzzy set.
162-
"""
61+
""" Plot the membership function of the fuzzy set. """
16362
x = np.linspace(0, 1, 1000)
16463
y = [self.membership(xi) for xi in x]
16564

0 commit comments

Comments
 (0)