mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-05-18 07:06:46 +00:00
Fix the bug in fuzzy_operations.py & deleted test
This commit is contained in:
parent
ee0a7c00f2
commit
de13593d4e
@ -7,7 +7,6 @@ import numpy as np
|
|||||||
Source: https://en.wikipedia.org/wiki/Fuzzy_set
|
Source: https://en.wikipedia.org/wiki/Fuzzy_set
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class FuzzySet:
|
class FuzzySet:
|
||||||
"""
|
"""
|
||||||
A class for representing and
|
A class for representing and
|
||||||
@ -47,28 +46,20 @@ class FuzzySet:
|
|||||||
self.peak = peak # Peak value
|
self.peak = peak # Peak value
|
||||||
self.right_boundary = right_boundary # Right boundary
|
self.right_boundary = right_boundary # Right boundary
|
||||||
|
|
||||||
|
|
||||||
def membership(self, x):
|
def membership(self, x):
|
||||||
"""
|
"""
|
||||||
Calculate the membership value of
|
Calculate the membership value of
|
||||||
an input 'x' in the fuzzy set.
|
an input 'x' in the fuzzy set.
|
||||||
|
Returns:
|
||||||
Args:
|
float: The membership value of 'x' in
|
||||||
x (float): The input value for
|
the fuzzy set.
|
||||||
which the membership is calculated.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
float: The membership value of 'x' in
|
|
||||||
the fuzzy set.
|
|
||||||
"""
|
"""
|
||||||
|
if x <= self.left_boundary or x >= self.right_boundary:
|
||||||
if x <= self.left_boundary or x >= self.right_boundary:
|
return 0
|
||||||
return 0
|
elif self.left_boundary < x <= self.peak:
|
||||||
elif self.left_boundary < x <= self.peak:
|
return (x - self.left_boundary) / (self.peak - self.left_boundary)
|
||||||
return (x - self.left_boundary) / (self.peak - self.left_boundary)
|
elif self.peak < x < self.right_boundary:
|
||||||
elif self.peak < x < self.right_boundary:
|
return (self.right_boundary - x) / (self.right_boundary - self.peak)
|
||||||
return (self.right_boundary - x) / (self.right_boundary - self.peak)
|
|
||||||
|
|
||||||
|
|
||||||
def union(self, other):
|
def union(self, other):
|
||||||
"""
|
"""
|
||||||
@ -112,7 +103,6 @@ def intersection(self, other):
|
|||||||
(self.peak + other.peak) / 2,
|
(self.peak + other.peak) / 2,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def complement(self):
|
def complement(self):
|
||||||
"""
|
"""
|
||||||
Calculate the complement (negation) of this fuzzy set.
|
Calculate the complement (negation) of this fuzzy set.
|
||||||
@ -121,10 +111,7 @@ def complement(self):
|
|||||||
FuzzySet: A new fuzzy set representing the complement.
|
FuzzySet: A new fuzzy set representing the complement.
|
||||||
"""
|
"""
|
||||||
complement_name = f"¬{self.name}"
|
complement_name = f"¬{self.name}"
|
||||||
return FuzzySet(
|
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):
|
def plot(self):
|
||||||
"""
|
"""
|
||||||
@ -135,6 +122,30 @@ def plot(self):
|
|||||||
|
|
||||||
plt.plot(x, y, label=self.name)
|
plt.plot(x, y, label=self.name)
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]"
|
return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]"
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
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()
|
@ -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()
|
|
Loading…
x
Reference in New Issue
Block a user