Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FuzzySet Class for Triangular Fuzzy Sets #11036

Merged
merged 49 commits into from
Oct 28, 2023
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bb8f7f7
Added Opertation for triangular fuzzy sets
Shreya123714 Oct 27, 2023
48f246d
Merge branch 'master' of https://github.com/Shreya123714/Python
Shreya123714 Oct 27, 2023
5e1dfe6
Added Sources
Shreya123714 Oct 27, 2023
6d751f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2023
e778cb3
Fix the bug , for which test were failing
Shreya123714 Oct 28, 2023
abe9361
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
c51022b
Add type hints and improve parameter names
Shreya123714 Oct 28, 2023
cb77a2a
Merge branch 'master' of https://github.com/Shreya123714/Python
Shreya123714 Oct 28, 2023
5486668
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
231b33e
Add Test For fuzzy_operations.py
Shreya123714 Oct 28, 2023
798e78f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
a7b7a91
Fix the bug in fuzzy_operations.py
Shreya123714 Oct 28, 2023
c6022c2
Merge branch 'master' of https://github.com/Shreya123714/Python
Shreya123714 Oct 28, 2023
1183dd9
Add test_fuzzy_logic.py
Shreya123714 Oct 28, 2023
ee0a7c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
de13593
Fix the bug in fuzzy_operations.py & deleted test
Shreya123714 Oct 28, 2023
cacc1d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
472e739
fixed the typo error
Shreya123714 Oct 28, 2023
5565063
Merge branch 'master' of https://github.com/Shreya123714/Python
Shreya123714 Oct 28, 2023
d2cdcb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
d091358
Again done
Shreya123714 Oct 28, 2023
0db5402
Merge branch 'master' of https://github.com/Shreya123714/Python
Shreya123714 Oct 28, 2023
70c3787
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
81340cd
corrected wrong intendation due to which test fail
Shreya123714 Oct 28, 2023
26cf8e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
54a3a42
Fix
pre-commit-ci[bot] Oct 28, 2023
f0f581a
Merge branch 'master' of https://github.com/Shreya123714/Python
Shreya123714 Oct 28, 2023
8a01993
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
14410df
bug fixed
Shreya123714 Oct 28, 2023
10bc341
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
3ff4ec9
Add test_fuzzy_logic
Shreya123714 Oct 28, 2023
06db239
Modified fuzzy_operations.py
Shreya123714 Oct 28, 2023
4e9ded8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
8ccf12a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
a357f85
Fixed the bug, made a FuzzySet dataclass
Shreya123714 Oct 28, 2023
d961a50
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
050debb
Replaced assertEqual of unittest to assert python
Shreya123714 Oct 28, 2023
ae179fa
Merge branch 'master' of https://github.com/Shreya123714/Python
Shreya123714 Oct 28, 2023
f3ae7ed
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
d9a8c65
lets see
Shreya123714 Oct 28, 2023
faed044
Changed test
Shreya123714 Oct 28, 2023
2b1cef8
orderd the import statements
Shreya123714 Oct 28, 2023
2dd8521
Add docstring and dataclass the FuzzySet
Shreya123714 Oct 28, 2023
c343a09
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
16a712a
Merge branch 'TheAlgorithms:master' into master
Shreya123714 Oct 28, 2023
005f852
Update fuzzy_operations.py
cclauss Oct 28, 2023
dc849bd
Delete fuzzy_logic/test_fuzzy_logic.py
cclauss Oct 28, 2023
8c8b200
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
cec6c1d
https://en.wikipedia.org/wiki/Fuzzy_set
cclauss Oct 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions fuzzy_logic/fuzzy_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
"""
By @Shreya123714

https://en.wikipedia.org/wiki/Fuzzy_set
"""

from __future__ import annotations

from dataclasses import dataclass

import matplotlib.pyplot as plt
import numpy as np


@dataclass
class FuzzySet:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""
A class for representing and manipulating triangular fuzzy sets.
Attributes:
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.
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 __str__(self) -> str:
"""
>>> FuzzySet("fuzzy_set", 0.1, 0.2, 0.3)
FuzzySet(name='fuzzy_set', left_boundary=0.1, peak=0.2, right_boundary=0.3)
"""
return (
f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]"
)

def complement(self) -> FuzzySet:
"""
Calculate the complement (negation) of this fuzzy set.
Returns:
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)
"""
return FuzzySet(
f"¬{self.name}",
1 - self.right_boundary,
1 - self.left_boundary,
1 - self.peak,
)

def intersection(self, other) -> FuzzySet:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: other

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: other

"""
Calculate the intersection of this fuzzy set
with another fuzzy set.
Args:
other: Another fuzzy set to intersect with.
Returns:
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)
"""
return FuzzySet(
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 membership(self, x: float) -> float:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

"""
Calculate the membership value of an input 'x' in the fuzzy set.
Returns:
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
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All methods should have at least one doctest.

Suggested change
"""
>>> FuzzySet("test", 10, 20, 30).complement()
'¬test: [-30, -10, -20]'
"""

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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: other

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: 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.

>>> 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)
"""
return FuzzySet(
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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file fuzzy_logic/fuzzy_operations.py, please provide doctest for the function plot

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file fuzzy_logic/fuzzy_operations.py, please provide doctest for the function plot

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file fuzzy_logic/fuzzy_operations.py, please provide doctest for the function plot

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file fuzzy_logic/fuzzy_operations.py, please provide doctest for the function plot

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot. If the function does not return a value, please provide the type hint as: def function() -> None:

"""
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)


if __name__ == "__main__":
from doctest import testmod

testmod()
a = FuzzySet("A", 0, 0.5, 1)
b = FuzzySet("B", 0.2, 0.7, 1)

a.plot()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: fuzzy_logic/fuzzy_operations.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 142:3.
parser error: error at 141:2: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, elif, else, lambda, match, not, pass, ~

    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()