mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-05-18 07:06:46 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
de13593d4e
commit
cacc1d104c
@ -7,6 +7,7 @@ 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
|
||||||
@ -46,20 +47,22 @@ 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.
|
||||||
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.left_boundary or x >= self.right_boundary:
|
if x <= self.left_boundary or x >= self.right_boundary:
|
||||||
return 0
|
return 0
|
||||||
elif self.left_boundary < x <= self.peak:
|
elif self.left_boundary < x <= self.peak:
|
||||||
return (x - self.left_boundary) / (self.peak - self.left_boundary)
|
return (x - self.left_boundary) / (self.peak - self.left_boundary)
|
||||||
elif self.peak < x < self.right_boundary:
|
elif self.peak < x < self.right_boundary:
|
||||||
return (self.right_boundary - x) / (self.right_boundary - self.peak)
|
return (self.right_boundary - x) / (self.right_boundary - self.peak)
|
||||||
|
|
||||||
|
|
||||||
def union(self, other):
|
def union(self, other):
|
||||||
"""
|
"""
|
||||||
@ -82,7 +85,7 @@ def union(self, other):
|
|||||||
max(self.right_boundary, other.right_boundary),
|
max(self.right_boundary, other.right_boundary),
|
||||||
(self.peak + other.peak) / 2,
|
(self.peak + other.peak) / 2,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def intersection(self, other):
|
def intersection(self, other):
|
||||||
"""
|
"""
|
||||||
@ -103,6 +106,7 @@ def intersection(self, other):
|
|||||||
(self.peak + other.peak) / 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.
|
||||||
@ -111,7 +115,10 @@ def complement(self):
|
|||||||
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.right_boundary, 1 - self.left_boundary, 1 - self.peak)
|
return FuzzySet(
|
||||||
|
complement_name, 1 - self.right_boundary, 1 - self.left_boundary, 1 - self.peak
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def plot(self):
|
def plot(self):
|
||||||
"""
|
"""
|
||||||
@ -122,16 +129,18 @@ def plot(self):
|
|||||||
|
|
||||||
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.left_boundary}, {self.peak}, {self.right_boundary}]"
|
return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
A = FuzzySet("A", 0, 0.5, 1)
|
A = FuzzySet("A", 0, 0.5, 1)
|
||||||
B = FuzzySet("B", 0.2, 0.7, 1)
|
B = FuzzySet("B", 0.2, 0.7, 1)
|
||||||
|
|
||||||
A.plot()
|
A.plot()
|
||||||
B.plot()
|
B.plot()
|
||||||
|
|
||||||
plt.xlabel("x")
|
plt.xlabel("x")
|
||||||
plt.ylabel("Membership")
|
plt.ylabel("Membership")
|
||||||
plt.legend()
|
plt.legend()
|
||||||
@ -148,4 +157,4 @@ if __name__ == "__main__":
|
|||||||
plt.xlabel("x")
|
plt.xlabel("x")
|
||||||
plt.ylabel("Membership")
|
plt.ylabel("Membership")
|
||||||
plt.legend()
|
plt.legend()
|
||||||
plt.show()
|
plt.show()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user