mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-05-18 23:21:28 +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
5e1dfe63d1
commit
6d751f806d
@ -1,11 +1,13 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
""""
|
||||
FuzzySet class for triangular fuzzy sets
|
||||
Author: Shreya123714
|
||||
Source: https://en.wikipedia.org/wiki/Fuzzy_set
|
||||
"""
|
||||
|
||||
|
||||
class FuzzySet:
|
||||
"""
|
||||
A class for representing and manipulating triangular fuzzy sets.
|
||||
@ -39,7 +41,6 @@ class FuzzySet:
|
||||
self.b = b # Peak value
|
||||
self.c = c # Right boundary
|
||||
|
||||
|
||||
def membership(self, x):
|
||||
"""
|
||||
Calculate the membership value of an input 'x' in the fuzzy set.
|
||||
@ -58,7 +59,6 @@ class FuzzySet:
|
||||
elif self.b < x < self.c:
|
||||
return (self.c - x) / (self.c - self.b)
|
||||
|
||||
|
||||
def union(self, other):
|
||||
"""
|
||||
Calculate the union of this fuzzy set with another fuzzy set.
|
||||
@ -71,8 +71,12 @@ class FuzzySet:
|
||||
"""
|
||||
|
||||
union_name = f"{self.name} ∪ {other.name}"
|
||||
return FuzzySet(union_name, min(self.a, other.a), max(self.c, other.c), (self.b + other.b) / 2)
|
||||
|
||||
return FuzzySet(
|
||||
union_name,
|
||||
min(self.a, other.a),
|
||||
max(self.c, other.c),
|
||||
(self.b + other.b) / 2,
|
||||
)
|
||||
|
||||
def intersection(self, other):
|
||||
"""
|
||||
@ -85,8 +89,12 @@ class FuzzySet:
|
||||
FuzzySet: A new fuzzy set representing the intersection.
|
||||
"""
|
||||
intersection_name = f"{self.name} ∩ {other.name}"
|
||||
return FuzzySet(intersection_name, max(self.a, other.a), min(self.c, other.c), (self.b + other.b) / 2)
|
||||
|
||||
return FuzzySet(
|
||||
intersection_name,
|
||||
max(self.a, other.a),
|
||||
min(self.c, other.c),
|
||||
(self.b + other.b) / 2,
|
||||
)
|
||||
|
||||
def complement(self):
|
||||
"""
|
||||
@ -98,7 +106,6 @@ class FuzzySet:
|
||||
complement_name = f"¬{self.name}"
|
||||
return FuzzySet(complement_name, 1 - self.c, 1 - self.a, 1 - self.b)
|
||||
|
||||
|
||||
def plot(self):
|
||||
"""
|
||||
Plot the membership function of the fuzzy set.
|
||||
@ -108,8 +115,6 @@ class FuzzySet:
|
||||
|
||||
plt.plot(x, y, label=self.name)
|
||||
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name}: [{self.a}, {self.b}, {self.c}]"
|
||||
|
||||
@ -139,5 +144,3 @@ if __name__ == "__main__":
|
||||
plt.ylabel("Membership")
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user