-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Add FuzzySet Class for Triangular Fuzzy Sets #11036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
bb8f7f7
48f246d
5e1dfe6
6d751f8
e778cb3
abe9361
c51022b
cb77a2a
5486668
231b33e
798e78f
a7b7a91
c6022c2
1183dd9
ee0a7c0
de13593
cacc1d1
472e739
5565063
d2cdcb8
d091358
0db5402
70c3787
81340cd
26cf8e7
54a3a42
f0f581a
8a01993
14410df
10bc341
3ff4ec9
06db239
4e9ded8
8ccf12a
a357f85
d961a50
050debb
ae179fa
f3ae7ed
d9a8c65
faed044
2b1cef8
2dd8521
c343a09
16a712a
005f852
dc849bd
8c8b200
cec6c1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,162 @@ | ||||||||||
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. | ||||||||||
|
||||||||||
Attributes: | ||||||||||
name (str): The name or label of the fuzzy set. | ||||||||||
a (float): The left boundary of the fuzzy set. | ||||||||||
b (float): The peak (central) value of the fuzzy set. | ||||||||||
c (float): The right boundary of the fuzzy set. | ||||||||||
|
||||||||||
Methods: | ||||||||||
membership(x): Calculate the membership value | ||||||||||
of an input 'x' in the fuzzy set. | ||||||||||
union(other): Calculate the union of this fuzzy set | ||||||||||
with another fuzzy set. | ||||||||||
intersection(other): Calculate the intersection of this fuzzy set | ||||||||||
with another fuzzy set. | ||||||||||
complement(): Calculate the complement (negation) | ||||||||||
of this fuzzy set. | ||||||||||
plot(): Plot the membership function of the fuzzy set. | ||||||||||
""" | ||||||||||
|
||||||||||
def __init__( | ||||||||||
self, name: str, left_boundary: float, peak: float, right_boundary: float | ||||||||||
) -> None: | ||||||||||
""" | ||||||||||
Initializes a triangular fuzzy set | ||||||||||
with the given parameters. | ||||||||||
|
||||||||||
Args: | ||||||||||
name (str): The name or label of the fuzzy set. | ||||||||||
a (float): The left boundary of the fuzzy set. | ||||||||||
b (float): The peak (central) value of | ||||||||||
the fuzzy set. | ||||||||||
c (float): The right boundary of the fuzzy set. | ||||||||||
""" | ||||||||||
self.name = name # Fuzzy set name | ||||||||||
self.left_boundary = left_boundary # Left boundary | ||||||||||
self.peak = peak # Peak value | ||||||||||
self.right_boundary = right_boundary # Right boundary | ||||||||||
|
||||||||||
def membership(self, x): | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide descriptive name for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: Please provide descriptive name for the parameter: Please provide type hint for the parameter: |
||||||||||
""" | ||||||||||
Calculate the membership value of | ||||||||||
an input 'x' in the fuzzy set. | ||||||||||
|
||||||||||
Args: | ||||||||||
x (float): The input value for | ||||||||||
which the membership is calculated. | ||||||||||
|
||||||||||
Returns: | ||||||||||
float: The membership value of 'x' in | ||||||||||
the fuzzy set. | ||||||||||
""" | ||||||||||
|
||||||||||
if x <= self.a or x >= self.c: | ||||||||||
return 0 | ||||||||||
elif self.a < x <= self.b: | ||||||||||
return (x - self.a) / (self.b - self.a) | ||||||||||
elif self.b < x < self.c: | ||||||||||
return (self.c - x) / (self.c - self.b) | ||||||||||
|
||||||||||
def union(self, other): | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: Please provide type hint for the parameter: |
||||||||||
""" | ||||||||||
Calculate the union of this fuzzy set | ||||||||||
with another fuzzy set. | ||||||||||
|
||||||||||
Args: | ||||||||||
other (FuzzySet): Another fuzzy set | ||||||||||
to union with. | ||||||||||
|
||||||||||
Returns: | ||||||||||
FuzzySet: A new fuzzy | ||||||||||
set representing the union. | ||||||||||
""" | ||||||||||
|
||||||||||
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, | ||||||||||
) | ||||||||||
|
||||||||||
def intersection(self, other): | ||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: Please provide type hint for the parameter: |
||||||||||
""" | ||||||||||
Calculate the intersection of this | ||||||||||
fuzzy set with another fuzzy set. | ||||||||||
|
||||||||||
Args: | ||||||||||
other (FuzzySet): Another fuzzy set to intersect with. | ||||||||||
|
||||||||||
Returns: | ||||||||||
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, | ||||||||||
) | ||||||||||
|
||||||||||
def complement(self): | ||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||||||||||
""" | ||||||||||
Calculate the complement (negation) of this fuzzy set. | ||||||||||
|
||||||||||
Returns: | ||||||||||
FuzzySet: A new fuzzy set representing the complement. | ||||||||||
""" | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All methods should have at least one doctest.
Suggested change
|
||||||||||
complement_name = f"¬{self.name}" | ||||||||||
return FuzzySet(complement_name, 1 - self.c, 1 - self.a, 1 - self.b) | ||||||||||
|
||||||||||
def plot(self): | ||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function:
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
""" | ||||||||||
Plot the membership function of the fuzzy set. | ||||||||||
""" | ||||||||||
x = np.linspace(0, 1, 1000) | ||||||||||
y = [self.membership(xi) for xi in x] | ||||||||||
|
||||||||||
plt.plot(x, y, label=self.name) | ||||||||||
|
||||||||||
def __str__(self): | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||||||||||
return f"{self.name}: [{self.a}, {self.b}, {self.c}]" | ||||||||||
|
||||||||||
|
||||||||||
# Example usage: | ||||||||||
if __name__ == "__main__": | ||||||||||
A = FuzzySet("A", 0, 0.5, 1) | ||||||||||
B = FuzzySet("B", 0.2, 0.7, 1) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
A.plot() | ||||||||||
B.plot() | ||||||||||
|
||||||||||
plt.xlabel("x") | ||||||||||
plt.ylabel("Membership") | ||||||||||
plt.legend() | ||||||||||
plt.show() | ||||||||||
|
||||||||||
union_ab = A.union(B) | ||||||||||
intersection_ab = A.intersection(B) | ||||||||||
complement_a = A.complement() | ||||||||||
|
||||||||||
union_ab.plot() | ||||||||||
intersection_ab.plot() | ||||||||||
complement_a.plot() | ||||||||||
|
||||||||||
plt.xlabel("x") | ||||||||||
plt.ylabel("Membership") | ||||||||||
plt.legend() | ||||||||||
plt.show() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import unittest | ||
from fuzzy_operations import FuzzySet | ||
|
||
class TestFuzzySet(unittest.TestCase): | ||
def test_membership_within_boundaries(self): | ||
A = FuzzySet("A", 0, 0.5, 1) | ||
|
||
self.assertAlmostEqual(A.membership(0), 1.0) # Left boundary | ||
self.assertAlmostEqual(A.membership(0.25), 0.5) # Peak value | ||
self.assertAlmostEqual(A.membership(0.5), 0.0) # Right boundary | ||
|
||
def test_membership_outside_boundaries(self): | ||
A = FuzzySet("A", 0, 0.5, 1) | ||
|
||
self.assertAlmostEqual(A.membership(0.75), 0.0) # Outside boundaries | ||
self.assertAlmostEqual(A.membership(-0.1), 0.0) # Outside boundaries | ||
|
||
def test_union(self): | ||
A = FuzzySet("A", 0, 0.5, 1) | ||
B = FuzzySet("B", 0.2, 0.7, 1) | ||
|
||
union_ab = A.union(B) | ||
|
||
self.assertAlmostEqual(union_ab.membership(0.1), 1.0) # Member of A | ||
self.assertAlmostEqual(union_ab.membership(0.35), 0.5) # Member of both A and B | ||
self.assertAlmostEqual(union_ab.membership(0.75), 0.0) # Outside boundaries | ||
|
||
def test_intersection(self): | ||
A = FuzzySet("A", 0, 0.5, 1) | ||
B = FuzzySet("B", 0.2, 0.7, 1) | ||
|
||
intersection_ab = A.intersection(B) | ||
|
||
self.assertAlmostEqual(intersection_ab.membership(0.1), 0.0) # Not a member of B | ||
self.assertAlmostEqual(intersection_ab.membership(0.35), 0.5) # Member of both A and B | ||
self.assertAlmostEqual(intersection_ab.membership(0.75), 0.0) # Not a member of A | ||
|
||
def test_complement(self): | ||
A = FuzzySet("A", 0, 0.5, 1) | ||
|
||
complement_a = A.complement() | ||
|
||
self.assertAlmostEqual(complement_a.membership(0.1), 0.0) # Member of A | ||
self.assertAlmostEqual(complement_a.membership(0.75), 1.0) # Outside boundaries | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make this a https://docs.python.org/3/library/dataclasses.html