11
11
import numpy as np
12
12
import matplotlib .pyplot as plt
13
13
14
+
14
15
@dataclass
15
16
class GaussianFuzzySet :
16
17
"""
17
18
A class for representing and manipulating Gaussian fuzzy sets.
18
-
19
+
19
20
Attributes:
20
21
name: The name or label of the fuzzy set.
21
22
mean: The mean value (center) of the Gaussian fuzzy set.
@@ -26,7 +27,7 @@ class GaussianFuzzySet:
26
27
membership(x): Calculate the membership value of an input 'x' in the fuzzy set.
27
28
complement(): Create a new GaussianFuzzySet instance representing the complement.
28
29
plot(): Plot the membership function of the fuzzy set.
29
-
30
+
30
31
>>> fuzzy_set = GaussianFuzzySet("Medium Temperature", mean=25, std_dev=5)
31
32
>>> fuzzy_set.membership(25)
32
33
1.0
@@ -45,7 +46,7 @@ def membership(self, x: float) -> float:
45
46
"""
46
47
Calculate the membership value of an input 'x' in the Gaussian fuzzy set.
47
48
If it's a complement set, returns 1 - the Gaussian membership.
48
-
49
+
49
50
>>> GaussianFuzzySet("Medium", 0, 1).membership(0)
50
51
1.0
51
52
>>> GaussianFuzzySet("Medium", 0, 1).membership(1)
@@ -57,25 +58,34 @@ def membership(self, x: float) -> float:
57
58
def complement (self ) -> GaussianFuzzySet :
58
59
"""
59
60
Create a new GaussianFuzzySet instance representing the complement.
60
-
61
+
61
62
>>> GaussianFuzzySet("Medium", 0, 1).complement().membership(0)
62
63
0.0
63
64
"""
64
- return GaussianFuzzySet (f"¬{ self .name } " , self .mean , self .std_dev , is_complement = not self .is_complement )
65
+ return GaussianFuzzySet (
66
+ f"¬{ self .name } " ,
67
+ self .mean ,
68
+ self .std_dev ,
69
+ is_complement = not self .is_complement ,
70
+ )
65
71
66
72
def plot (self ):
67
73
"""
68
74
Plot the membership function of the Gaussian fuzzy set.
69
75
"""
70
- x = np .linspace (self .mean - 3 * self .std_dev , self .mean + 3 * self .std_dev , 1000 )
76
+ x = np .linspace (
77
+ self .mean - 3 * self .std_dev , self .mean + 3 * self .std_dev , 1000
78
+ )
71
79
y = [self .membership (xi ) for xi in x ]
72
80
plt .plot (x , y , label = self .name )
73
81
plt .xlabel ("x" )
74
82
plt .ylabel ("Membership" )
75
83
plt .legend ()
76
84
85
+
77
86
if __name__ == "__main__" :
78
87
from doctest import testmod
88
+
79
89
testmod ()
80
90
81
91
# Create an instance of GaussianFuzzySet
@@ -84,7 +94,9 @@ def plot(self):
84
94
# Display some membership values
85
95
print (f"Membership at mean (25): { fuzzy_set .membership (25 )} " )
86
96
print (f"Membership at 30: { fuzzy_set .membership (30 )} " )
87
- print (f"Complement Membership at mean (25): { fuzzy_set .complement ().membership (25 )} " )
97
+ print (
98
+ f"Complement Membership at mean (25): { fuzzy_set .complement ().membership (25 )} "
99
+ )
88
100
89
101
# Plot the Gaussian Fuzzy Set and its complement
90
102
fuzzy_set .plot ()
0 commit comments