From bb8f7f716b52b86f367027121cd7202e96a9ca47 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Fri, 27 Oct 2023 23:29:00 +0530 Subject: [PATCH 01/41] Added Opertation for triangular fuzzy sets --- fuzzy_logic/fuzzy_operations.py | 138 ++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 fuzzy_logic/fuzzy_operations.py diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py new file mode 100644 index 000000000000..cd1e96203b29 --- /dev/null +++ b/fuzzy_logic/fuzzy_operations.py @@ -0,0 +1,138 @@ +import matplotlib.pyplot as plt +import numpy as np + +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, a, b, c): + """ + 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.a = a # Left boundary + 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. + + 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): + """ + 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): + """ + 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): + """ + Calculate the complement (negation) of this fuzzy set. + + Returns: + FuzzySet: A new fuzzy set representing the complement. + """ + 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. + """ + x = np.linspace(0, 1, 1000) + y = [self.membership(xi) for xi in x] + + plt.plot(x, y, label=self.name) + + + + def __str__(self): + 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) + + 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() + + From 5e1dfe63d12ba6cb428bb91bfeaedb196e0cb080 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Fri, 27 Oct 2023 23:39:32 +0530 Subject: [PATCH 02/41] Added Sources --- fuzzy_logic/fuzzy_operations.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index cd1e96203b29..947d6cc6e429 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -1,5 +1,10 @@ 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: """ From 6d751f806da640d9ad6f1466a9406e37fac81e00 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Oct 2023 18:48:45 +0000 Subject: [PATCH 03/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 947d6cc6e429..16407b13c3a1 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -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 @@ def __init__(self, name, a, b, c): 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 @@ def membership(self, x): 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,11 +71,15 @@ def union(self, other): """ 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): - """ + """ Calculate the intersection of this fuzzy set with another fuzzy set. Args: @@ -84,9 +88,13 @@ def intersection(self, other): 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) - + 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): """ @@ -98,7 +106,6 @@ def complement(self): 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 @@ def plot(self): plt.plot(x, y, label=self.name) - - def __str__(self): return f"{self.name}: [{self.a}, {self.b}, {self.c}]" @@ -121,7 +126,7 @@ def __str__(self): A.plot() B.plot() - + plt.xlabel("x") plt.ylabel("Membership") plt.legend() @@ -139,5 +144,3 @@ def __str__(self): plt.ylabel("Membership") plt.legend() plt.show() - - From e778cb35b76753c4dca4d63e4261400e420946fc Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 11:00:43 +0530 Subject: [PATCH 04/41] Fix the bug , for which test were failing --- fuzzy_logic/fuzzy_operations.py | 54 +++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 16407b13c3a1..1a7dd91fd8f6 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -10,7 +10,8 @@ class FuzzySet: """ - A class for representing and manipulating triangular fuzzy sets. + A class for representing and + manipulating triangular fuzzy sets. Attributes: name (str): The name or label of the fuzzy set. @@ -19,21 +20,27 @@ class FuzzySet: 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. + 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, a, b, c): """ - Initializes a triangular fuzzy set with the given parameters. + 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. + 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 @@ -43,13 +50,16 @@ def __init__(self, name, a, b, c): def membership(self, x): """ - Calculate the membership value of an input 'x' in the fuzzy set. + Calculate the membership value of + an input 'x' in the fuzzy set. Args: - x (float): The input value for which the membership is calculated. + x (float): The input value for + which the membership is calculated. Returns: - float: The membership value of 'x' in the fuzzy set. + float: The membership value of 'x' in + the fuzzy set. """ if x <= self.a or x >= self.c: @@ -61,13 +71,16 @@ def membership(self, x): def union(self, other): """ - Calculate the union of this fuzzy set with another fuzzy set. + Calculate the union of this fuzzy set + with another fuzzy set. Args: - other (FuzzySet): Another fuzzy set to union with. + other (FuzzySet): Another fuzzy set + to union with. Returns: - FuzzySet: A new fuzzy set representing the union. + FuzzySet: A new fuzzy + set representing the union. """ union_name = f"{self.name} ∪ {other.name}" @@ -80,7 +93,8 @@ def union(self, other): def intersection(self, other): """ - Calculate the intersection of this fuzzy set with another fuzzy set. + Calculate the intersection of this + fuzzy set with another fuzzy set. Args: other (FuzzySet): Another fuzzy set to intersect with. @@ -132,13 +146,13 @@ def __str__(self): plt.legend() plt.show() - union_AB = A.union(B) - intersection_AB = A.intersection(B) - complement_A = A.complement() + union_ab = A.union(B) + intersection_ab = A.intersection(B) + complement_a = A.complement() - union_AB.plot() - intersection_AB.plot() - complement_A.plot() + union_ab.plot() + intersection_ab.plot() + complement_a.plot() plt.xlabel("x") plt.ylabel("Membership") From abe936179211c32fbb13ed76cc908547336e67e6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 05:31:21 +0000 Subject: [PATCH 05/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 1a7dd91fd8f6..fe52cac3d6a8 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -10,7 +10,7 @@ class FuzzySet: """ - A class for representing and + A class for representing and manipulating triangular fuzzy sets. Attributes: @@ -20,26 +20,26 @@ class FuzzySet: c (float): The right boundary of the fuzzy set. Methods: - membership(x): Calculate the membership value + membership(x): Calculate the membership value of an input 'x' in the fuzzy set. - union(other): Calculate the union of this fuzzy set + union(other): Calculate the union of this fuzzy set with another fuzzy set. - intersection(other): Calculate the intersection of this fuzzy set + intersection(other): Calculate the intersection of this fuzzy set with another fuzzy set. - complement(): Calculate the complement (negation) + complement(): Calculate the complement (negation) of this fuzzy set. plot(): Plot the membership function of the fuzzy set. """ def __init__(self, name, a, b, c): """ - Initializes a triangular fuzzy set + 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 + b (float): The peak (central) value of the fuzzy set. c (float): The right boundary of the fuzzy set. """ @@ -50,15 +50,15 @@ def __init__(self, name, a, b, c): def membership(self, x): """ - Calculate the membership value of + Calculate the membership value of an input 'x' in the fuzzy set. Args: - x (float): The input value for + x (float): The input value for which the membership is calculated. Returns: - float: The membership value of 'x' in + float: The membership value of 'x' in the fuzzy set. """ @@ -71,7 +71,7 @@ def membership(self, x): def union(self, other): """ - Calculate the union of this fuzzy set + Calculate the union of this fuzzy set with another fuzzy set. Args: @@ -79,7 +79,7 @@ def union(self, other): to union with. Returns: - FuzzySet: A new fuzzy + FuzzySet: A new fuzzy set representing the union. """ @@ -93,7 +93,7 @@ def union(self, other): def intersection(self, other): """ - Calculate the intersection of this + Calculate the intersection of this fuzzy set with another fuzzy set. Args: From c51022bc2e103510a445bbb807df91a4114bc870 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 11:14:57 +0530 Subject: [PATCH 06/41] Add type hints and improve parameter names --- fuzzy_logic/fuzzy_operations.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 1a7dd91fd8f6..73ddfa96e582 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -31,22 +31,21 @@ class FuzzySet: plot(): Plot the membership function of the fuzzy set. """ - def __init__(self, name, a, b, c): + def __init__(self, name: str, left_boundary: float, peak: float, right_boundary: float) -> None: """ - Initializes a triangular fuzzy set - with the given parameters. + 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. - """ + Args: + name (str): The name or label of the fuzzy set. + left_boundary (float): The left boundary of the fuzzy set. + peak (float): The peak (central) value of the fuzzy set. + right_boundary (float): The right boundary of the fuzzy set. + """ self.name = name # Fuzzy set name - self.a = a # Left boundary - self.b = b # Peak value - self.c = c # Right boundary + self.left_boundary = left_boundary # Left boundary + self.peak = peak # Peak value + self.right_boundary = right_boundary # Right boundary + def membership(self, x): """ From 548666895bf8db944379f89d6b5ac41150cc6a91 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 05:48:22 +0000 Subject: [PATCH 07/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 8e5792c0d6c8..dbcf66445f06 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -31,15 +31,17 @@ class FuzzySet: plot(): Plot the membership function of the fuzzy set. """ - def __init__(self, name: str, left_boundary: float, peak: float, right_boundary: float) -> None: + def __init__( + self, name: str, left_boundary: float, peak: float, right_boundary: float + ) -> None: """ - Initializes a triangular fuzzy set + 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 + b (float): The peak (central) value of the fuzzy set. c (float): The right boundary of the fuzzy set. """ @@ -48,7 +50,6 @@ def __init__(self, name: str, left_boundary: float, peak: float, right_boundary: self.peak = peak # Peak value self.right_boundary = right_boundary # Right boundary - def membership(self, x): """ Calculate the membership value of From 231b33ea1a85189002f0e229303b0c97a546340f Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 11:43:05 +0530 Subject: [PATCH 08/41] Add Test For fuzzy_operations.py --- fuzzy_logic/test_fuzzy_logic.py | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 fuzzy_logic/test_fuzzy_logic.py diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py new file mode 100644 index 000000000000..e94b0a76363d --- /dev/null +++ b/fuzzy_logic/test_fuzzy_logic.py @@ -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() From 798e78f5e853b9411ada75d4014dcf15a47bbb96 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 06:14:07 +0000 Subject: [PATCH 09/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/test_fuzzy_logic.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py index e94b0a76363d..0a660257153f 100644 --- a/fuzzy_logic/test_fuzzy_logic.py +++ b/fuzzy_logic/test_fuzzy_logic.py @@ -1,26 +1,27 @@ 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 @@ -28,20 +29,27 @@ def test_union(self): 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 + + 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() From a7b7a9169a80f1a27e903a8650a9e86ff91564fc Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 14:09:18 +0530 Subject: [PATCH 10/41] Fix the bug in fuzzy_operations.py --- fuzzy_logic/fuzzy_operations.py | 169 +++++++++++++------------------- 1 file changed, 69 insertions(+), 100 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index dbcf66445f06..9000706a99c1 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -7,18 +7,15 @@ 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. @@ -37,7 +34,6 @@ def __init__( """ 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. @@ -50,113 +46,86 @@ def __init__( self.peak = peak # Peak value self.right_boundary = right_boundary # Right boundary - def membership(self, x): - """ - 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): - """ - 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 membership(self, x): + """ + Calculate the membership value of + an input 'x' in the fuzzy set. - def intersection(self, other): - """ - Calculate the intersection of this - fuzzy set with another fuzzy set. + Args: + x (float): The input value for + which the membership is calculated. - Args: - other (FuzzySet): Another fuzzy set to intersect with. + Returns: + float: The membership value of 'x' in + the fuzzy set. + """ - 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): - """ - Calculate the complement (negation) of this fuzzy set. + if x <= self.left_boundary or x >= self.right_boundary: + return 0 + elif self.left_boundary < x <= self.peak: + return (x - self.left_boundary) / (self.peak - self.left_boundary) + elif self.peak < x < self.right_boundary: + return (self.right_boundary - x) / (self.right_boundary - self.peak) - Returns: - FuzzySet: A new fuzzy set representing the complement. - """ - complement_name = f"¬{self.name}" - return FuzzySet(complement_name, 1 - self.c, 1 - self.a, 1 - self.b) +def union(self, other): + """ + Calculate the union of this fuzzy set + with another fuzzy set. - def plot(self): - """ - Plot the membership function of the fuzzy set. - """ - x = np.linspace(0, 1, 1000) - y = [self.membership(xi) for xi in x] + Args: + other (FuzzySet): Another fuzzy set + to union with. - plt.plot(x, y, label=self.name) + Returns: + FuzzySet: A new fuzzy + set representing the union. + """ - def __str__(self): - return f"{self.name}: [{self.a}, {self.b}, {self.c}]" + union_name = f"{self.name} ∪ {other.name}" + return FuzzySet( + union_name, + min(self.left_boundary, other.left_boundary), + max(self.right_boundary, other.right_boundary), + (self.peak + other.peak) / 2, + ) +def intersection(self, other): + """ + Calculate the intersection of this + fuzzy set with another fuzzy set. -# Example usage: -if __name__ == "__main__": - A = FuzzySet("A", 0, 0.5, 1) - B = FuzzySet("B", 0.2, 0.7, 1) + Args: + other (FuzzySet): Another fuzzy set to intersect with. - A.plot() - B.plot() + Returns: + FuzzySet: A new fuzzy set representing the intersection. + """ + intersection_name = f"{self.name} ∩ {other.name}" + return FuzzySet( + intersection_name, + max(self.left_boundary, other.left_boundary), + min(self.right_boundary, other.right_boundary), + (self.peak + other.peak) / 2, + ) + +def complement(self): + """ + Calculate the complement (negation) of this fuzzy set. - plt.xlabel("x") - plt.ylabel("Membership") - plt.legend() - plt.show() + Returns: + FuzzySet: A new fuzzy set representing the complement. + """ + complement_name = f"¬{self.name}" + return FuzzySet(complement_name, 1 - self.right_boundary, 1 - self.left_boundary, 1 - self.peak) - union_ab = A.union(B) - intersection_ab = A.intersection(B) - complement_a = A.complement() +def plot(self): + """ + Plot the membership function of the fuzzy set. + """ + x = np.linspace(0, 1, 1000) + y = [self.membership(xi) for xi in x] - union_ab.plot() - intersection_ab.plot() - complement_a.plot() + plt.plot(x, y, label=self.name) - plt.xlabel("x") - plt.ylabel("Membership") - plt.legend() - plt.show() +def __str__(self): + return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]" From 1183dd9ee075da7bf214dc7ff4b5b0b618911710 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 14:19:11 +0530 Subject: [PATCH 11/41] Add test_fuzzy_logic.py --- fuzzy_logic/test_fuzzy_logic.py | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py index 0a660257153f..41e9207f1ee8 100644 --- a/fuzzy_logic/test_fuzzy_logic.py +++ b/fuzzy_logic/test_fuzzy_logic.py @@ -5,6 +5,7 @@ class TestFuzzySet(unittest.TestCase): def test_membership_within_boundaries(self): A = FuzzySet("A", 0, 0.5, 1) +<<<<<<< Updated upstream self.assertAlmostEqual(A.membership(0), 1.0) # Left boundary self.assertAlmostEqual(A.membership(0.25), 0.5) # Peak value @@ -25,12 +26,35 @@ def test_union(self): 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 +======= + + self.assertEqual(A.membership(0), 1.0) # Left boundary + self.assertEqual(A.membership(0.25), 0.5) # Peak value + self.assertEqual(A.membership(0.5), 0.0) # Right boundary + + def test_membership_outside_boundaries(self): + a = FuzzySet("A", 0, 0.5, 1) + + self.assertEqual(a.membership(0.75), 0.0) # Outside boundaries + self.assertEqual(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.assertEqual(union_ab.membership(0.1), 1.0) # Member of A + self.assertEqual(union_ab.membership(0.35), 0.5) # Member of both A and B + self.assertEqual(union_ab.membership(0.75), 0.0) # Outside boundaries +>>>>>>> Stashed changes def test_intersection(self): A = FuzzySet("A", 0, 0.5, 1) B = FuzzySet("B", 0.2, 0.7, 1) intersection_ab = A.intersection(B) +<<<<<<< Updated upstream self.assertAlmostEqual( intersection_ab.membership(0.1), 0.0 @@ -49,6 +73,20 @@ def test_complement(self): self.assertAlmostEqual(complement_a.membership(0.1), 0.0) # Member of A self.assertAlmostEqual(complement_a.membership(0.75), 1.0) # Outside boundaries +======= + + self.assertEqual(intersection_ab.membership(0.1), 0.0) # Not a member of B + self.assertEqual(intersection_ab.membership(0.35), 0.5) # Member of both A and B + self.assertEqual(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.assertEqual(complement_a.membership(0.1), 0.0) # Member of A + self.assertEqual(complement_a.membership(0.75), 1.0) # Outside boundaries +>>>>>>> Stashed changes if __name__ == "__main__": From ee0a7c00f2c942dacdce8d737129424b7aa38186 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 08:50:23 +0000 Subject: [PATCH 12/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 11 ++++++++++- fuzzy_logic/test_fuzzy_logic.py | 14 +++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 9000706a99c1..404bbde43b4a 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -7,6 +7,7 @@ Source: https://en.wikipedia.org/wiki/Fuzzy_set """ + class FuzzySet: """ A class for representing and @@ -46,6 +47,7 @@ def __init__( self.peak = peak # Peak value self.right_boundary = right_boundary # Right boundary + def membership(self, x): """ Calculate the membership value of @@ -67,6 +69,7 @@ def membership(self, x): elif self.peak < x < self.right_boundary: return (self.right_boundary - x) / (self.right_boundary - self.peak) + def union(self, other): """ Calculate the union of this fuzzy set @@ -89,6 +92,7 @@ def union(self, other): (self.peak + other.peak) / 2, ) + def intersection(self, other): """ Calculate the intersection of this @@ -108,6 +112,7 @@ def intersection(self, other): (self.peak + other.peak) / 2, ) + def complement(self): """ Calculate the complement (negation) of this fuzzy set. @@ -116,7 +121,10 @@ def complement(self): FuzzySet: A new fuzzy set representing the complement. """ 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): """ @@ -127,5 +135,6 @@ def plot(self): plt.plot(x, y, label=self.name) + def __str__(self): return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]" diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py index 41e9207f1ee8..be9e2fcabc85 100644 --- a/fuzzy_logic/test_fuzzy_logic.py +++ b/fuzzy_logic/test_fuzzy_logic.py @@ -27,23 +27,23 @@ def test_union(self): 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 ======= - + self.assertEqual(A.membership(0), 1.0) # Left boundary self.assertEqual(A.membership(0.25), 0.5) # Peak value self.assertEqual(A.membership(0.5), 0.0) # Right boundary def test_membership_outside_boundaries(self): a = FuzzySet("A", 0, 0.5, 1) - + self.assertEqual(a.membership(0.75), 0.0) # Outside boundaries self.assertEqual(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.assertEqual(union_ab.membership(0.1), 1.0) # Member of A self.assertEqual(union_ab.membership(0.35), 0.5) # Member of both A and B self.assertEqual(union_ab.membership(0.75), 0.0) # Outside boundaries @@ -74,16 +74,16 @@ def test_complement(self): self.assertAlmostEqual(complement_a.membership(0.1), 0.0) # Member of A self.assertAlmostEqual(complement_a.membership(0.75), 1.0) # Outside boundaries ======= - + self.assertEqual(intersection_ab.membership(0.1), 0.0) # Not a member of B self.assertEqual(intersection_ab.membership(0.35), 0.5) # Member of both A and B self.assertEqual(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.assertEqual(complement_a.membership(0.1), 0.0) # Member of A self.assertEqual(complement_a.membership(0.75), 1.0) # Outside boundaries >>>>>>> Stashed changes From de13593d4e0d72052363bf3021d5a1243b68ca0d Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 14:48:55 +0530 Subject: [PATCH 13/41] Fix the bug in fuzzy_operations.py & deleted test --- fuzzy_logic/fuzzy_operations.py | 63 +++++++++++++--------- fuzzy_logic/test_fuzzy_logic.py | 93 --------------------------------- 2 files changed, 37 insertions(+), 119 deletions(-) delete mode 100644 fuzzy_logic/test_fuzzy_logic.py diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 404bbde43b4a..5534b9822b9e 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -7,7 +7,6 @@ Source: https://en.wikipedia.org/wiki/Fuzzy_set """ - class FuzzySet: """ A class for representing and @@ -47,28 +46,20 @@ def __init__( self.peak = peak # Peak value self.right_boundary = right_boundary # Right boundary - def membership(self, x): - """ + """ 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. + Returns: + float: The membership value of 'x' in + the fuzzy set. """ - - if x <= self.left_boundary or x >= self.right_boundary: - return 0 - elif self.left_boundary < x <= self.peak: - return (x - self.left_boundary) / (self.peak - self.left_boundary) - elif self.peak < x < self.right_boundary: - return (self.right_boundary - x) / (self.right_boundary - self.peak) - + if x <= self.left_boundary or x >= self.right_boundary: + return 0 + elif self.left_boundary < x <= self.peak: + return (x - self.left_boundary) / (self.peak - self.left_boundary) + elif self.peak < x < self.right_boundary: + return (self.right_boundary - x) / (self.right_boundary - self.peak) def union(self, other): """ @@ -91,7 +82,7 @@ def union(self, other): max(self.right_boundary, other.right_boundary), (self.peak + other.peak) / 2, ) - + def intersection(self, other): """ @@ -112,7 +103,6 @@ def intersection(self, other): (self.peak + other.peak) / 2, ) - def complement(self): """ Calculate the complement (negation) of this fuzzy set. @@ -121,10 +111,7 @@ def complement(self): FuzzySet: A new fuzzy set representing the complement. """ 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): """ @@ -135,6 +122,30 @@ def plot(self): plt.plot(x, y, label=self.name) - def __str__(self): return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]" + +if __name__ == "__main__": + A = FuzzySet("A", 0, 0.5, 1) + B = FuzzySet("B", 0.2, 0.7, 1) + + 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() \ No newline at end of file diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py deleted file mode 100644 index be9e2fcabc85..000000000000 --- a/fuzzy_logic/test_fuzzy_logic.py +++ /dev/null @@ -1,93 +0,0 @@ -import unittest -from fuzzy_operations import FuzzySet - - -class TestFuzzySet(unittest.TestCase): - def test_membership_within_boundaries(self): - A = FuzzySet("A", 0, 0.5, 1) -<<<<<<< Updated upstream - - 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 -======= - - self.assertEqual(A.membership(0), 1.0) # Left boundary - self.assertEqual(A.membership(0.25), 0.5) # Peak value - self.assertEqual(A.membership(0.5), 0.0) # Right boundary - - def test_membership_outside_boundaries(self): - a = FuzzySet("A", 0, 0.5, 1) - - self.assertEqual(a.membership(0.75), 0.0) # Outside boundaries - self.assertEqual(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.assertEqual(union_ab.membership(0.1), 1.0) # Member of A - self.assertEqual(union_ab.membership(0.35), 0.5) # Member of both A and B - self.assertEqual(union_ab.membership(0.75), 0.0) # Outside boundaries ->>>>>>> Stashed changes - - def test_intersection(self): - A = FuzzySet("A", 0, 0.5, 1) - B = FuzzySet("B", 0.2, 0.7, 1) - - intersection_ab = A.intersection(B) -<<<<<<< Updated upstream - - 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 -======= - - self.assertEqual(intersection_ab.membership(0.1), 0.0) # Not a member of B - self.assertEqual(intersection_ab.membership(0.35), 0.5) # Member of both A and B - self.assertEqual(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.assertEqual(complement_a.membership(0.1), 0.0) # Member of A - self.assertEqual(complement_a.membership(0.75), 1.0) # Outside boundaries ->>>>>>> Stashed changes - - -if __name__ == "__main__": - unittest.main() From cacc1d104c9e972b1d7600d3d079570718a56ebc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 09:19:39 +0000 Subject: [PATCH 14/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 5534b9822b9e..b8e3982075db 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -7,6 +7,7 @@ Source: https://en.wikipedia.org/wiki/Fuzzy_set """ + class FuzzySet: """ A class for representing and @@ -46,20 +47,22 @@ def __init__( self.peak = peak # Peak value self.right_boundary = right_boundary # Right boundary + def membership(self, x): - """ + """ Calculate the membership value of an input 'x' in the fuzzy set. Returns: float: The membership value of 'x' in the fuzzy set. """ - if x <= self.left_boundary or x >= self.right_boundary: - return 0 - elif self.left_boundary < x <= self.peak: - return (x - self.left_boundary) / (self.peak - self.left_boundary) - elif self.peak < x < self.right_boundary: - return (self.right_boundary - x) / (self.right_boundary - self.peak) + if x <= self.left_boundary or x >= self.right_boundary: + return 0 + elif self.left_boundary < x <= self.peak: + return (x - self.left_boundary) / (self.peak - self.left_boundary) + elif self.peak < x < self.right_boundary: + return (self.right_boundary - x) / (self.right_boundary - self.peak) + def union(self, other): """ @@ -82,7 +85,7 @@ def union(self, other): max(self.right_boundary, other.right_boundary), (self.peak + other.peak) / 2, ) - + def intersection(self, other): """ @@ -103,6 +106,7 @@ def intersection(self, other): (self.peak + other.peak) / 2, ) + def complement(self): """ Calculate the complement (negation) of this fuzzy set. @@ -111,7 +115,10 @@ def complement(self): FuzzySet: A new fuzzy set representing the complement. """ 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): """ @@ -122,16 +129,18 @@ def plot(self): plt.plot(x, y, label=self.name) + def __str__(self): return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]" + if __name__ == "__main__": A = FuzzySet("A", 0, 0.5, 1) B = FuzzySet("B", 0.2, 0.7, 1) A.plot() B.plot() - + plt.xlabel("x") plt.ylabel("Membership") plt.legend() @@ -148,4 +157,4 @@ def __str__(self): plt.xlabel("x") plt.ylabel("Membership") plt.legend() - plt.show() \ No newline at end of file + plt.show() From 472e7399f45503333924b098e330797e8703bbfa Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 14:53:02 +0530 Subject: [PATCH 15/41] fixed the typo error --- fuzzy_logic/fuzzy_operations.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 5534b9822b9e..5351d4f33be0 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -126,24 +126,24 @@ def __str__(self): return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]" if __name__ == "__main__": - A = FuzzySet("A", 0, 0.5, 1) - B = FuzzySet("B", 0.2, 0.7, 1) + a = FuzzySet("A", 0, 0.5, 1) + b = FuzzySet("B", 0.2, 0.7, 1) - A.plot() - B.plot() + 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 = A.union(B) + intersection_ab = A.intersection(B) + complement_a = A.complement() - union_AB.plot() - intersection_AB.plot() - complement_A.plot() + union_ab.plot() + intersection_ab.plot() + complement_a.plot() plt.xlabel("x") plt.ylabel("Membership") From d2cdcb85ecfe8ab76abbdc47188bba4dd07031f3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 09:24:05 +0000 Subject: [PATCH 16/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 86f722ec1f41..bab9a516f457 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -141,7 +141,7 @@ def __str__(self): <<<<<<< HEAD a.plot() b.plot() - + ======= A.plot() B.plot() From d091358d82eb7446ab367823400a2de75e030f6d Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 14:54:24 +0530 Subject: [PATCH 17/41] Again done --- fuzzy_logic/fuzzy_operations.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 86f722ec1f41..e88a0d0aa3a4 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -137,16 +137,6 @@ def __str__(self): if __name__ == "__main__": a = FuzzySet("A", 0, 0.5, 1) b = FuzzySet("B", 0.2, 0.7, 1) - -<<<<<<< HEAD - a.plot() - b.plot() - -======= - A.plot() - B.plot() - ->>>>>>> cacc1d104c9e972b1d7600d3d079570718a56ebc plt.xlabel("x") plt.ylabel("Membership") plt.legend() From 70c378717564cca0bb235904d70ca6065e665ae6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 09:27:06 +0000 Subject: [PATCH 18/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 1 - 1 file changed, 1 deletion(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 36f5acf16543..8c281f0af145 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -141,7 +141,6 @@ def __str__(self): a.plot() b.plot() - plt.xlabel("x") plt.ylabel("Membership") plt.legend() From 81340cd1dd54e732cc3379a24c1c89923c0ee7d8 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 15:14:29 +0530 Subject: [PATCH 19/41] corrected wrong intendation due to which test fail --- fuzzy_logic/fuzzy_operations.py | 189 +++++++++++++------------------- 1 file changed, 77 insertions(+), 112 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 8c281f0af145..06c36679dc0e 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -1,138 +1,103 @@ 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. + 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. + left_boundary (float): The left boundary of the fuzzy set. + peak (float): The peak (central) value of the fuzzy set. + right_boundary (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. + 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: + def __init__(self, name: str, left_boundary: float, peak: float, right_boundary: float) -> None: """ - Initializes a triangular fuzzy set - with the given parameters. + 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. + left_boundary (float): The left boundary of the fuzzy set. + peak (float): The peak (central) value of the fuzzy set. + right_boundary (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): - """ - Calculate the membership value of - an input 'x' in the fuzzy set. + def membership(self, x) -> float: + """ + Calculate the membership value of an input 'x' in the fuzzy set. Returns: - float: The membership value of 'x' in - the fuzzy set. - """ - if x <= self.left_boundary or x >= self.right_boundary: - return 0 - elif self.left_boundary < x <= self.peak: - return (x - self.left_boundary) / (self.peak - self.left_boundary) - elif self.peak < x < self.right_boundary: - return (self.right_boundary - x) / (self.right_boundary - self.peak) - - -def union(self, other): - """ - 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.left_boundary, other.left_boundary), - max(self.right_boundary, other.right_boundary), - (self.peak + other.peak) / 2, - ) - - -def intersection(self, other): - """ - 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.left_boundary, other.left_boundary), - min(self.right_boundary, other.right_boundary), - (self.peak + other.peak) / 2, - ) - - -def complement(self): - """ - Calculate the complement (negation) of this fuzzy set. - - Returns: - FuzzySet: A new fuzzy set representing the complement. - """ - complement_name = f"¬{self.name}" - return FuzzySet( - complement_name, 1 - self.right_boundary, 1 - self.left_boundary, 1 - self.peak - ) - - -def plot(self): - """ - 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) + float: The membership value of 'x' in the fuzzy set. + """ + if x <= self.left_boundary or x >= self.right_boundary: + return 0 + elif self.left_boundary < x <= self.peak: + return (x - self.left_boundary) / (self.peak - self.left_boundary) + elif self.peak < x < self.right_boundary: + return (self.right_boundary - x) / (self.right_boundary - self.peak) + + def union(self, other) -> 'FuzzySet': + """ + 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.left_boundary, other.left_boundary), + max(self.right_boundary, other.right_boundary), + (self.peak + other.peak) / 2, + ) + + def intersection(self, other) -> 'FuzzySet': + """ + 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.left_boundary, other.left_boundary), + min(self.right_boundary, other.right_boundary), + (self.peak + other.peak) / 2, + ) + + def complement(self) -> 'FuzzySet': + """ + Calculate the complement (negation) of this fuzzy set. + Returns: + FuzzySet: A new fuzzy set representing the complement. + """ + complement_name = f"¬{self.name}" + return FuzzySet( + complement_name, 1 - self.right_boundary, 1 - self.left_boundary, 1 - self.peak + ) + def plot(self): + """ + Plot the membership function of the fuzzy set. + """ + x = np.linspace(0, 1, 1000) + y = [self.membership(xi) for xi in x] -def __str__(self): - return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]" + plt.plot(x, y, label=self.name) + def __str__(self): + return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]" if __name__ == "__main__": a = FuzzySet("A", 0, 0.5, 1) @@ -157,4 +122,4 @@ def __str__(self): plt.xlabel("x") plt.ylabel("Membership") plt.legend() - plt.show() + plt.show() \ No newline at end of file From 26cf8e731f854fb0bbd540dd6ad6700f76de5b63 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 09:45:42 +0000 Subject: [PATCH 20/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 06c36679dc0e..276c105c173e 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -1,6 +1,7 @@ import matplotlib.pyplot as plt import numpy as np + class FuzzySet: """ A class for representing and manipulating triangular fuzzy sets. @@ -17,7 +18,9 @@ class FuzzySet: plot(): Plot the membership function of the fuzzy set. """ - def __init__(self, name: str, left_boundary: float, peak: float, right_boundary: float) -> None: + def __init__( + self, name: str, left_boundary: float, peak: float, right_boundary: float + ) -> None: """ Initializes a triangular fuzzy set with the given parameters. Args: @@ -44,7 +47,7 @@ def membership(self, x) -> float: elif self.peak < x < self.right_boundary: return (self.right_boundary - x) / (self.right_boundary - self.peak) - def union(self, other) -> 'FuzzySet': + def union(self, other) -> "FuzzySet": """ Calculate the union of this fuzzy set with another fuzzy set. Args: @@ -60,7 +63,7 @@ def union(self, other) -> 'FuzzySet': (self.peak + other.peak) / 2, ) - def intersection(self, other) -> 'FuzzySet': + def intersection(self, other) -> "FuzzySet": """ Calculate the intersection of this fuzzy set with another fuzzy set. Args: @@ -76,7 +79,7 @@ def intersection(self, other) -> 'FuzzySet': (self.peak + other.peak) / 2, ) - def complement(self) -> 'FuzzySet': + def complement(self) -> "FuzzySet": """ Calculate the complement (negation) of this fuzzy set. Returns: @@ -84,7 +87,10 @@ def complement(self) -> 'FuzzySet': """ complement_name = f"¬{self.name}" return FuzzySet( - complement_name, 1 - self.right_boundary, 1 - self.left_boundary, 1 - self.peak + complement_name, + 1 - self.right_boundary, + 1 - self.left_boundary, + 1 - self.peak, ) def plot(self): @@ -97,7 +103,10 @@ def plot(self): plt.plot(x, y, label=self.name) 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__": a = FuzzySet("A", 0, 0.5, 1) @@ -122,4 +131,4 @@ def __str__(self): plt.xlabel("x") plt.ylabel("Membership") plt.legend() - plt.show() \ No newline at end of file + plt.show() From 54a3a42d182e25bb2c6fe8c89d246ccd4b653b5b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 09:45:42 +0000 Subject: [PATCH 21/41] Fix --- fuzzy_logic/fuzzy_operations.py | 41 ++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 06c36679dc0e..46526e7c249a 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -1,6 +1,7 @@ import matplotlib.pyplot as plt import numpy as np + class FuzzySet: """ A class for representing and manipulating triangular fuzzy sets. @@ -10,14 +11,20 @@ class FuzzySet: peak (float): The peak (central) value of the fuzzy set. right_boundary (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. + 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: + def __init__( + self, name: str, left_boundary: float, peak: float, right_boundary: float + ) -> None: """ Initializes a triangular fuzzy set with the given parameters. Args: @@ -33,7 +40,8 @@ def __init__(self, name: str, left_boundary: float, peak: float, right_boundary: def membership(self, x) -> float: """ - Calculate the membership value of an input 'x' in the fuzzy set. + Calculate the membership value of an + input 'x' in the fuzzy set. Returns: float: The membership value of 'x' in the fuzzy set. """ @@ -44,7 +52,7 @@ def membership(self, x) -> float: elif self.peak < x < self.right_boundary: return (self.right_boundary - x) / (self.right_boundary - self.peak) - def union(self, other) -> 'FuzzySet': + def union(self, other) -> "FuzzySet": """ Calculate the union of this fuzzy set with another fuzzy set. Args: @@ -60,9 +68,10 @@ def union(self, other) -> 'FuzzySet': (self.peak + other.peak) / 2, ) - def intersection(self, other) -> 'FuzzySet': + def intersection(self, other) -> "FuzzySet": """ - Calculate the intersection of this fuzzy set with another fuzzy set. + Calculate the intersection of this fuzzy set + with another fuzzy set. Args: other (FuzzySet): Another fuzzy set to intersect with. Returns: @@ -76,7 +85,7 @@ def intersection(self, other) -> 'FuzzySet': (self.peak + other.peak) / 2, ) - def complement(self) -> 'FuzzySet': + def complement(self) -> "FuzzySet": """ Calculate the complement (negation) of this fuzzy set. Returns: @@ -84,7 +93,10 @@ def complement(self) -> 'FuzzySet': """ complement_name = f"¬{self.name}" return FuzzySet( - complement_name, 1 - self.right_boundary, 1 - self.left_boundary, 1 - self.peak + complement_name, + 1 - self.right_boundary, + 1 - self.left_boundary, + 1 - self.peak, ) def plot(self): @@ -97,7 +109,10 @@ def plot(self): plt.plot(x, y, label=self.name) 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__": a = FuzzySet("A", 0, 0.5, 1) @@ -122,4 +137,4 @@ def __str__(self): plt.xlabel("x") plt.ylabel("Membership") plt.legend() - plt.show() \ No newline at end of file + plt.show() From 8a01993d37cadb02e66a21fc2c966bb36cfe68c8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 09:59:40 +0000 Subject: [PATCH 22/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 46526e7c249a..527c38393619 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -11,13 +11,13 @@ class FuzzySet: peak (float): The peak (central) value of the fuzzy set. right_boundary (float): The right boundary of the fuzzy set. Methods: - membership(x): Calculate the membership value of an + membership(x): Calculate the membership value of an input 'x' in the fuzzy set. - union(other): Calculate the union of + union(other): Calculate the union of this fuzzy set with another fuzzy set. - intersection(other): Calculate the intersection of + intersection(other): Calculate the intersection of this fuzzy set with another fuzzy set. - complement(): Calculate the complement (negation) of + complement(): Calculate the complement (negation) of this fuzzy set. plot(): Plot the membership function of the fuzzy set. """ @@ -40,7 +40,7 @@ def __init__( def membership(self, x) -> float: """ - Calculate the membership value of an + Calculate the membership value of an input 'x' in the fuzzy set. Returns: float: The membership value of 'x' in the fuzzy set. @@ -70,7 +70,7 @@ def union(self, other) -> "FuzzySet": def intersection(self, other) -> "FuzzySet": """ - Calculate the intersection of this fuzzy set + Calculate the intersection of this fuzzy set with another fuzzy set. Args: other (FuzzySet): Another fuzzy set to intersect with. From 14410df373bcb5a71e9e96569d0c41cef148c3ce Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 15:34:55 +0530 Subject: [PATCH 23/41] bug fixed --- fuzzy_logic/fuzzy_operations.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 527c38393619..02730dc7613a 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -38,20 +38,20 @@ def __init__( self.peak = peak # Peak value self.right_boundary = right_boundary # Right boundary - def membership(self, x) -> float: - """ - Calculate the membership value of an - input 'x' in the fuzzy set. - Returns: - float: The membership value of 'x' in the fuzzy set. + def membership(self, x: float) -> float: """ + Calculate the membership value of an input 'x' in the fuzzy set. + Returns: + float: The membership value of 'x' in the fuzzy set. + """ if x <= self.left_boundary or x >= self.right_boundary: - return 0 + return 0.0 elif self.left_boundary < x <= self.peak: return (x - self.left_boundary) / (self.peak - self.left_boundary) elif self.peak < x < self.right_boundary: return (self.right_boundary - x) / (self.right_boundary - self.peak) + def union(self, other) -> "FuzzySet": """ Calculate the union of this fuzzy set with another fuzzy set. From 10bc34131bb314802b8473dd667c5ca6ac779329 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 10:05:50 +0000 Subject: [PATCH 24/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 02730dc7613a..486a52912433 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -40,10 +40,10 @@ def __init__( def membership(self, x: float) -> float: """ - Calculate the membership value of an input 'x' in the fuzzy set. - Returns: - float: The membership value of 'x' in the fuzzy set. - """ + Calculate the membership value of an input 'x' in the fuzzy set. + Returns: + float: The membership value of 'x' in the fuzzy set. + """ if x <= self.left_boundary or x >= self.right_boundary: return 0.0 elif self.left_boundary < x <= self.peak: @@ -51,7 +51,6 @@ def membership(self, x: float) -> float: elif self.peak < x < self.right_boundary: return (self.right_boundary - x) / (self.right_boundary - self.peak) - def union(self, other) -> "FuzzySet": """ Calculate the union of this fuzzy set with another fuzzy set. From 3ff4ec9f42d5691734e16f851c95ac2b55675c48 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 17:15:34 +0530 Subject: [PATCH 25/41] Add test_fuzzy_logic --- fuzzy_logic/test_fuzzy_logic.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 fuzzy_logic/test_fuzzy_logic.py diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py new file mode 100644 index 000000000000..9c05580570e8 --- /dev/null +++ b/fuzzy_logic/test_fuzzy_logic.py @@ -0,0 +1,29 @@ +import unittest +from fuzzy_operations import FuzzySet + +class TestFuzzySet(unittest.TestCase): + def setUp(self): + self.A = FuzzySet("A", 0, 0.5, 1) + self.B = FuzzySet("B", 0.2, 0.7, 1) + + def test_membership_within_boundaries(self): + self.assertEqual(self.A.membership(0.1),0.2) # Modify with the expected value + self.assertEqual(self.B.membership(0.6),0.8) # Modify with the expected value + + def test_union(self): + union_ab = self.A.union(self.B) + self.assertEqual(union_ab.membership(0.1),0.1) # Modify with the expected value + self.assertEqual(union_ab.membership(0.35),0.35) # Modify with the expected value + + def test_intersection(self): + intersection_ab = self.A.intersection(self.B) + self.assertEqual(intersection_ab.membership(0.1),0.0) # Modify with the expected value + self.assertEqual(intersection_ab.membership(0.35),0.18749999999999994) # Modify with the expected value + + def test_complement(self): + complement_a = self.A.complement() + self.assertEqual(complement_a.membership(0.1),0.1) # Modify with the expected value + self.assertEqual(complement_a.membership(0.75), 0.0) # Modify with the expected value + +if __name__ == "__main__": + unittest.main() From 06db23964988de3e7b1d7cdd601f82957c5273e5 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 17:16:06 +0530 Subject: [PATCH 26/41] Modified fuzzy_operations.py --- fuzzy_logic/fuzzy_operations.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 486a52912433..e360f571ddf1 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -137,3 +137,9 @@ def __str__(self): plt.ylabel("Membership") plt.legend() plt.show() +else: + import unittest + from test_fuzzy_logic import TestFuzzySet + + suite = unittest.TestLoader().loadTestsFromTestCase(TestFuzzySet) + unittest.TextTestRunner(verbosity=2).run(suite) \ No newline at end of file From 4e9ded8e221a31b5f0780c25b226ec49835b40ad Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 11:46:37 +0000 Subject: [PATCH 27/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/test_fuzzy_logic.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py index 9c05580570e8..72a313c363e8 100644 --- a/fuzzy_logic/test_fuzzy_logic.py +++ b/fuzzy_logic/test_fuzzy_logic.py @@ -1,29 +1,43 @@ import unittest from fuzzy_operations import FuzzySet + class TestFuzzySet(unittest.TestCase): def setUp(self): self.A = FuzzySet("A", 0, 0.5, 1) self.B = FuzzySet("B", 0.2, 0.7, 1) def test_membership_within_boundaries(self): - self.assertEqual(self.A.membership(0.1),0.2) # Modify with the expected value - self.assertEqual(self.B.membership(0.6),0.8) # Modify with the expected value + self.assertEqual(self.A.membership(0.1), 0.2) # Modify with the expected value + self.assertEqual(self.B.membership(0.6), 0.8) # Modify with the expected value def test_union(self): union_ab = self.A.union(self.B) - self.assertEqual(union_ab.membership(0.1),0.1) # Modify with the expected value - self.assertEqual(union_ab.membership(0.35),0.35) # Modify with the expected value + self.assertEqual( + union_ab.membership(0.1), 0.1 + ) # Modify with the expected value + self.assertEqual( + union_ab.membership(0.35), 0.35 + ) # Modify with the expected value def test_intersection(self): intersection_ab = self.A.intersection(self.B) - self.assertEqual(intersection_ab.membership(0.1),0.0) # Modify with the expected value - self.assertEqual(intersection_ab.membership(0.35),0.18749999999999994) # Modify with the expected value + self.assertEqual( + intersection_ab.membership(0.1), 0.0 + ) # Modify with the expected value + self.assertEqual( + intersection_ab.membership(0.35), 0.18749999999999994 + ) # Modify with the expected value def test_complement(self): complement_a = self.A.complement() - self.assertEqual(complement_a.membership(0.1),0.1) # Modify with the expected value - self.assertEqual(complement_a.membership(0.75), 0.0) # Modify with the expected value + self.assertEqual( + complement_a.membership(0.1), 0.1 + ) # Modify with the expected value + self.assertEqual( + complement_a.membership(0.75), 0.0 + ) # Modify with the expected value + if __name__ == "__main__": unittest.main() From 8ccf12adad883ebc7317530d83174c50ceb65f6a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 11:47:06 +0000 Subject: [PATCH 28/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index e360f571ddf1..c567135c285f 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -142,4 +142,4 @@ def __str__(self): from test_fuzzy_logic import TestFuzzySet suite = unittest.TestLoader().loadTestsFromTestCase(TestFuzzySet) - unittest.TextTestRunner(verbosity=2).run(suite) \ No newline at end of file + unittest.TextTestRunner(verbosity=2).run(suite) From a357f852f9e4402c7f7a59c256afdb35b09820b4 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 21:12:42 +0530 Subject: [PATCH 29/41] Fixed the bug, made a FuzzySet dataclass --- fuzzy_logic/fuzzy_operations.py | 49 ++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index c567135c285f..33f0dfe2ba3d 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -1,7 +1,32 @@ import matplotlib.pyplot as plt import numpy as np +from dataclasses import dataclass +""" +By @Shreya123714 +#How fuzzy set is created using FuzzySet Class +>>>me = FuzzySet("Sheru",0.4,1,0.6) +>>>me.__str__() +'Sheru: [0.4, 1, 0.6]' +>>>me +FuzzySet(name='Sheru', left_boundary=0.4, peak=1, right_boundary=0.6) +>>>s = FuzzySet("Siya",0.5,1,0.7) +#Union Operations +>>>s.union(me) +FuzzySet(name='Siya ∪ Sheru', left_boundary=0.4, peak=0.7, right_boundary=1.0) + +#Intersection Operation +>>>s.intersection(me) +FuzzySet(name='Siya ∩ Sheru', left_boundary=0.5, peak=0.6, right_boundary=1.0) + +#Complement Operation +>>>s.complement() +FuzzySet(name='¬Siya', left_boundary=0.30000000000000004, peak=0.5, right_boundary=0) +""" + + +@dataclass class FuzzySet: """ A class for representing and manipulating triangular fuzzy sets. @@ -21,22 +46,26 @@ class FuzzySet: 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: - """ + name: str + left_boundary: float + peak: float + right_boundary: float + + # 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. left_boundary (float): The left boundary of the fuzzy set. peak (float): The peak (central) value of the fuzzy set. right_boundary (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 + """ + # 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: float) -> float: """ From d961a5030c4e678d88e4775888367f65de21b03d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 15:43:21 +0000 Subject: [PATCH 30/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 33f0dfe2ba3d..a1ada7e45003 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -1,6 +1,7 @@ import matplotlib.pyplot as plt import numpy as np from dataclasses import dataclass + """ By @Shreya123714 @@ -46,6 +47,7 @@ class FuzzySet: this fuzzy set. plot(): Plot the membership function of the fuzzy set. """ + name: str left_boundary: float peak: float @@ -62,10 +64,10 @@ class FuzzySet: peak (float): The peak (central) value of the fuzzy set. right_boundary (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 + # 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: float) -> float: """ From 050debb9e82699f485cbb80be7ad128bf5485b24 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 21:16:25 +0530 Subject: [PATCH 31/41] Replaced assertEqual of unittest to assert python --- fuzzy_logic/test_fuzzy_logic.py | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py index 72a313c363e8..9b95b3d99f3b 100644 --- a/fuzzy_logic/test_fuzzy_logic.py +++ b/fuzzy_logic/test_fuzzy_logic.py @@ -1,43 +1,31 @@ import unittest from fuzzy_operations import FuzzySet - class TestFuzzySet(unittest.TestCase): def setUp(self): self.A = FuzzySet("A", 0, 0.5, 1) self.B = FuzzySet("B", 0.2, 0.7, 1) def test_membership_within_boundaries(self): - self.assertEqual(self.A.membership(0.1), 0.2) # Modify with the expected value - self.assertEqual(self.B.membership(0.6), 0.8) # Modify with the expected value + assert self.A.membership(0.1) == 0.2 # Modify with the expected value + assert self.B.membership(0.6) == 0.8 # Modify with the expected value def test_union(self): union_ab = self.A.union(self.B) - self.assertEqual( - union_ab.membership(0.1), 0.1 - ) # Modify with the expected value - self.assertEqual( - union_ab.membership(0.35), 0.35 - ) # Modify with the expected value + assert union_ab.membership(0.1) == 0.1 # Modify with the expected value + assert union_ab.membership(0.35) == 0.35 # Modify with the expected value def test_intersection(self): intersection_ab = self.A.intersection(self.B) - self.assertEqual( - intersection_ab.membership(0.1), 0.0 - ) # Modify with the expected value - self.assertEqual( - intersection_ab.membership(0.35), 0.18749999999999994 + assert intersection_ab.membership(0.1) == 0.0 # Modify with the expected value + assert ( + intersection_ab.membership(0.35) == 0.18749999999999994 ) # Modify with the expected value def test_complement(self): complement_a = self.A.complement() - self.assertEqual( - complement_a.membership(0.1), 0.1 - ) # Modify with the expected value - self.assertEqual( - complement_a.membership(0.75), 0.0 - ) # Modify with the expected value - + assert complement_a.membership(0.1) == 0.1 # Modify with the expected value + assert complement_a.membership(0.75) == 0.0 # Modify with the expected value if __name__ == "__main__": unittest.main() From f3ae7edb6b1bb9f868dbe50726a218cf0a6f9aeb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 15:47:21 +0000 Subject: [PATCH 32/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/test_fuzzy_logic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py index 9b95b3d99f3b..5de64f04efab 100644 --- a/fuzzy_logic/test_fuzzy_logic.py +++ b/fuzzy_logic/test_fuzzy_logic.py @@ -1,6 +1,7 @@ import unittest from fuzzy_operations import FuzzySet + class TestFuzzySet(unittest.TestCase): def setUp(self): self.A = FuzzySet("A", 0, 0.5, 1) @@ -27,5 +28,6 @@ def test_complement(self): assert complement_a.membership(0.1) == 0.1 # Modify with the expected value assert complement_a.membership(0.75) == 0.0 # Modify with the expected value + if __name__ == "__main__": unittest.main() From d9a8c65f5dda500dce5bf524b4f05010a8af91ef Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 21:21:48 +0530 Subject: [PATCH 33/41] lets see --- fuzzy_logic/test_fuzzy_logic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py index 5de64f04efab..a6bd5a50beaf 100644 --- a/fuzzy_logic/test_fuzzy_logic.py +++ b/fuzzy_logic/test_fuzzy_logic.py @@ -1,5 +1,5 @@ import unittest -from fuzzy_operations import FuzzySet +from fuzzy_logic.fuzzy_operations import FuzzySet class TestFuzzySet(unittest.TestCase): From faed0446f44301b975eb117d22b4d9b469089a07 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 21:33:09 +0530 Subject: [PATCH 34/41] Changed test --- fuzzy_logic/test_fuzzy_logic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py index a6bd5a50beaf..b431b91c4a84 100644 --- a/fuzzy_logic/test_fuzzy_logic.py +++ b/fuzzy_logic/test_fuzzy_logic.py @@ -1,5 +1,6 @@ +from fuzzy_operations import FuzzySet + import unittest -from fuzzy_logic.fuzzy_operations import FuzzySet class TestFuzzySet(unittest.TestCase): From 2b1cef873bf089855d65636c10870ac9c24ba9e7 Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 21:38:05 +0530 Subject: [PATCH 35/41] orderd the import statements --- fuzzy_logic/fuzzy_operations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index a1ada7e45003..e5a053d7a0ef 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -1,5 +1,5 @@ -import matplotlib.pyplot as plt import numpy as np +import matplotlib.pyplot as plt from dataclasses import dataclass """ From 2dd85213c0e5cb32fc61711f24796476be6afa0d Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Sat, 28 Oct 2023 23:30:36 +0530 Subject: [PATCH 36/41] Add docstring and dataclass the FuzzySet --- fuzzy_logic/fuzzy_operations.py | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index e5a053d7a0ef..3eaec0676a7b 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -1,7 +1,8 @@ import numpy as np + import matplotlib.pyplot as plt -from dataclasses import dataclass +from dataclasses import dataclass """ By @Shreya123714 @@ -53,22 +54,6 @@ class FuzzySet: peak: float right_boundary: float - # 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. - left_boundary (float): The left boundary of the fuzzy set. - peak (float): The peak (central) value of the fuzzy set. - right_boundary (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: float) -> float: """ Calculate the membership value of an input 'x' in the fuzzy set. @@ -170,6 +155,7 @@ def __str__(self): plt.show() else: import unittest + from test_fuzzy_logic import TestFuzzySet suite = unittest.TestLoader().loadTestsFromTestCase(TestFuzzySet) From c343a09ebaecb06c8264122af0546c66260af520 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 18:01:17 +0000 Subject: [PATCH 37/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 3eaec0676a7b..dbbd056c9aa0 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -3,6 +3,7 @@ import matplotlib.pyplot as plt from dataclasses import dataclass + """ By @Shreya123714 @@ -155,7 +156,7 @@ def __str__(self): plt.show() else: import unittest - + from test_fuzzy_logic import TestFuzzySet suite = unittest.TestLoader().loadTestsFromTestCase(TestFuzzySet) From 005f852d1bf4b3b823217038998bb5dc2cfb98ee Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 28 Oct 2023 20:26:09 +0200 Subject: [PATCH 38/41] Update fuzzy_operations.py --- fuzzy_logic/fuzzy_operations.py | 195 ++++++++++++++++++-------------- 1 file changed, 112 insertions(+), 83 deletions(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index dbbd056c9aa0..635cf1009f35 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -1,32 +1,13 @@ -import numpy as np +""" +By @Shreya123714 +""" -import matplotlib.pyplot as plt +from __future__ import annotations from dataclasses import dataclass -""" -By @Shreya123714 - -#How fuzzy set is created using FuzzySet Class ->>>me = FuzzySet("Sheru",0.4,1,0.6) ->>>me.__str__() -'Sheru: [0.4, 1, 0.6]' ->>>me -FuzzySet(name='Sheru', left_boundary=0.4, peak=1, right_boundary=0.6) ->>>s = FuzzySet("Siya",0.5,1,0.7) - -#Union Operations ->>>s.union(me) -FuzzySet(name='Siya ∪ Sheru', left_boundary=0.4, peak=0.7, right_boundary=1.0) - -#Intersection Operation ->>>s.intersection(me) -FuzzySet(name='Siya ∩ Sheru', left_boundary=0.5, peak=0.6, right_boundary=1.0) - -#Complement Operation ->>>s.complement() -FuzzySet(name='¬Siya', left_boundary=0.30000000000000004, peak=0.5, right_boundary=0) -""" +import matplotlib.pyplot as plt +import numpy as np @dataclass @@ -34,85 +15,142 @@ class FuzzySet: """ A class for representing and manipulating triangular fuzzy sets. Attributes: - name (str): The name or label of the fuzzy set. - left_boundary (float): The left boundary of the fuzzy set. - peak (float): The peak (central) value of the fuzzy set. - right_boundary (float): The right boundary of the fuzzy set. + name: The name or label of the fuzzy set. + left_boundary: The left boundary of the fuzzy set. + peak: The peak (central) value of the fuzzy set. + right_boundary: 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. + 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. + complement(): Calculate the complement (negation) of this fuzzy set. plot(): Plot the membership function of the fuzzy set. - """ + >>> sheru = FuzzySet("Sheru", 0.4, 1, 0.6) + >>> sheru + FuzzySet(name='Sheru', left_boundary=0.4, peak=1, right_boundary=0.6) + >>> str(sheru) + 'Sheru: [0.4, 1, 0.6]' + + >>> siya = FuzzySet("Siya", 0.5, 1, 0.7) + >>> siya + FuzzySet(name='Siya', left_boundary=0.5, peak=1, right_boundary=0.7) + + # Complement Operation + >>> sheru.complement() + FuzzySet(name='¬Sheru', left_boundary=0.4, peak=0.6, right_boundary=0) + >>> siya.complement() # doctest: +NORMALIZE_WHITESPACE + FuzzySet(name='¬Siya', left_boundary=0.30000000000000004, peak=0.5, + right_boundary=0) + + # Intersection Operation + >>> siya.intersection(sheru) + FuzzySet(name='Siya ∩ Sheru', left_boundary=0.5, peak=0.6, right_boundary=1.0) + + # Membership Operation + >>> sheru.membership(0.5) + 0.16666666666666663 + >>> sheru.membership(0.6) + 0.0 + + # Union Operations + >>> siya.union(sheru) + FuzzySet(name='Siya ∪ Sheru', left_boundary=0.4, peak=0.7, right_boundary=1.0) + """ name: str left_boundary: float peak: float right_boundary: float - def membership(self, x: float) -> float: + def __str__(self) -> str: """ - Calculate the membership value of an input 'x' in the fuzzy set. - Returns: - float: The membership value of 'x' in the fuzzy set. + >>> FuzzySet("fuzzy_set", 0.1, 0.2, 0.3) + FuzzySet(name='fuzzy_set', left_boundary=0.1, peak=0.2, right_boundary=0.3) """ - if x <= self.left_boundary or x >= self.right_boundary: - return 0.0 - elif self.left_boundary < x <= self.peak: - return (x - self.left_boundary) / (self.peak - self.left_boundary) - elif self.peak < x < self.right_boundary: - return (self.right_boundary - x) / (self.right_boundary - self.peak) + return ( + f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]" + ) - def union(self, other) -> "FuzzySet": + def complement(self) -> FuzzySet: """ - Calculate the union of this fuzzy set with another fuzzy set. - Args: - other (FuzzySet): Another fuzzy set to union with. + Calculate the complement (negation) of this fuzzy set. Returns: - FuzzySet: A new fuzzy set representing the union. + FuzzySet: A new fuzzy set representing the complement. + + >>> FuzzySet("fuzzy_set", 0.1, 0.2, 0.3).complement() + FuzzySet(name='¬fuzzy_set', left_boundary=0.7, peak=0.9, right_boundary=0.8) """ - union_name = f"{self.name} ∪ {other.name}" return FuzzySet( - union_name, - min(self.left_boundary, other.left_boundary), - max(self.right_boundary, other.right_boundary), - (self.peak + other.peak) / 2, + f"¬{self.name}", + 1 - self.right_boundary, + 1 - self.left_boundary, + 1 - self.peak, ) - def intersection(self, other) -> "FuzzySet": + def intersection(self, other) -> FuzzySet: """ Calculate the intersection of this fuzzy set with another fuzzy set. Args: - other (FuzzySet): Another fuzzy set to intersect with. + other: Another fuzzy set to intersect with. Returns: - FuzzySet: A new fuzzy set representing the intersection. + A new fuzzy set representing the intersection. + + >>> FuzzySet("a", 0.1, 0.2, 0.3).intersection(FuzzySet("b", 0.4, 0.5, 0.6)) + FuzzySet(name='a ∩ b', left_boundary=0.4, peak=0.3, right_boundary=0.35) """ - intersection_name = f"{self.name} ∩ {other.name}" return FuzzySet( - intersection_name, + f"{self.name} ∩ {other.name}", max(self.left_boundary, other.left_boundary), min(self.right_boundary, other.right_boundary), (self.peak + other.peak) / 2, ) - def complement(self) -> "FuzzySet": + def membership(self, x: float) -> float: """ - Calculate the complement (negation) of this fuzzy set. + Calculate the membership value of an input 'x' in the fuzzy set. Returns: - FuzzySet: A new fuzzy set representing the complement. + The membership value of 'x' in the fuzzy set. + + >>> a = FuzzySet("a", 0.1, 0.2, 0.3) + >>> a.membership(0.09) + 0.0 + >>> a.membership(0.1) + 0.0 + >>> a.membership(0.11) + 0.09999999999999995 + >>> a.membership(0.4) + 0.0 + >>> FuzzySet("A", 0, 0.5, 1).membership(0.1) + 0.2 + >>> FuzzySet("B", 0.2, 0.7, 1).membership(0.6) + 0.8 + """ + if x <= self.left_boundary or x >= self.right_boundary: + return 0.0 + elif self.left_boundary < x <= self.peak: + return (x - self.left_boundary) / (self.peak - self.left_boundary) + elif self.peak < x < self.right_boundary: + return (self.right_boundary - x) / (self.right_boundary - self.peak) + msg = f"Invalid value {x} for fuzzy set {self}" + raise ValueError(msg) + + def union(self, other) -> FuzzySet: + """ + 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. + + >>> FuzzySet("a", 0.1, 0.2, 0.3).union(FuzzySet("b", 0.4, 0.5, 0.6)) + FuzzySet(name='a ∪ b', left_boundary=0.1, peak=0.6, right_boundary=0.35) """ - complement_name = f"¬{self.name}" return FuzzySet( - complement_name, - 1 - self.right_boundary, - 1 - self.left_boundary, - 1 - self.peak, + f"{self.name} ∪ {other.name}", + min(self.left_boundary, other.left_boundary), + max(self.right_boundary, other.right_boundary), + (self.peak + other.peak) / 2, ) def plot(self): @@ -124,13 +162,11 @@ def plot(self): plt.plot(x, y, label=self.name) - def __str__(self): - return ( - f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]" - ) - if __name__ == "__main__": + from doctest import testmod + + testmod() a = FuzzySet("A", 0, 0.5, 1) b = FuzzySet("B", 0.2, 0.7, 1) @@ -154,10 +190,3 @@ def __str__(self): plt.ylabel("Membership") plt.legend() plt.show() -else: - import unittest - - from test_fuzzy_logic import TestFuzzySet - - suite = unittest.TestLoader().loadTestsFromTestCase(TestFuzzySet) - unittest.TextTestRunner(verbosity=2).run(suite) From dc849bd888facc15f4f58034876805e2281a8f58 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 28 Oct 2023 20:26:28 +0200 Subject: [PATCH 39/41] Delete fuzzy_logic/test_fuzzy_logic.py --- fuzzy_logic/test_fuzzy_logic.py | 34 --------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 fuzzy_logic/test_fuzzy_logic.py diff --git a/fuzzy_logic/test_fuzzy_logic.py b/fuzzy_logic/test_fuzzy_logic.py deleted file mode 100644 index b431b91c4a84..000000000000 --- a/fuzzy_logic/test_fuzzy_logic.py +++ /dev/null @@ -1,34 +0,0 @@ -from fuzzy_operations import FuzzySet - -import unittest - - -class TestFuzzySet(unittest.TestCase): - def setUp(self): - self.A = FuzzySet("A", 0, 0.5, 1) - self.B = FuzzySet("B", 0.2, 0.7, 1) - - def test_membership_within_boundaries(self): - assert self.A.membership(0.1) == 0.2 # Modify with the expected value - assert self.B.membership(0.6) == 0.8 # Modify with the expected value - - def test_union(self): - union_ab = self.A.union(self.B) - assert union_ab.membership(0.1) == 0.1 # Modify with the expected value - assert union_ab.membership(0.35) == 0.35 # Modify with the expected value - - def test_intersection(self): - intersection_ab = self.A.intersection(self.B) - assert intersection_ab.membership(0.1) == 0.0 # Modify with the expected value - assert ( - intersection_ab.membership(0.35) == 0.18749999999999994 - ) # Modify with the expected value - - def test_complement(self): - complement_a = self.A.complement() - assert complement_a.membership(0.1) == 0.1 # Modify with the expected value - assert complement_a.membership(0.75) == 0.0 # Modify with the expected value - - -if __name__ == "__main__": - unittest.main() From 8c8b200527cc5f0445ee40862dd427f6e5b32c93 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Oct 2023 18:26:44 +0000 Subject: [PATCH 40/41] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 635cf1009f35..c811ee50ebfd 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -57,6 +57,7 @@ class FuzzySet: >>> siya.union(sheru) FuzzySet(name='Siya ∪ Sheru', left_boundary=0.4, peak=0.7, right_boundary=1.0) """ + name: str left_boundary: float peak: float From cec6c1d4dd014d1cad9f5d9c4e446f6d0a9af276 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 28 Oct 2023 20:31:49 +0200 Subject: [PATCH 41/41] https://en.wikipedia.org/wiki/Fuzzy_set --- fuzzy_logic/fuzzy_operations.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index c811ee50ebfd..e41cd2120049 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -1,5 +1,7 @@ """ By @Shreya123714 + +https://en.wikipedia.org/wiki/Fuzzy_set """ from __future__ import annotations