Compare commits

...

14 Commits

Author SHA1 Message Date
Shreya
437f2a026a
Merge af4a64b125 into f3f32ae3ca 2024-11-19 10:58:29 +08:00
pre-commit-ci[bot]
f3f32ae3ca
[pre-commit.ci] pre-commit autoupdate (#12385)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.3 → v0.7.4](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.3...v0.7.4)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-18 22:07:12 +01:00
Christian Clauss
e3bd7721c8
validate_filenames.py Shebang python for Windows (#12371) 2024-11-15 14:59:14 +01:00
pre-commit-ci[bot]
e3f3d668be
[pre-commit.ci] pre-commit autoupdate (#12370)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.2 → v0.7.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.2...v0.7.3)
- [github.com/abravalheri/validate-pyproject: v0.22 → v0.23](https://github.com/abravalheri/validate-pyproject/compare/v0.22...v0.23)

* Update sudoku_solver.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2024-11-11 21:05:50 +01:00
pre-commit-ci[bot]
af4a64b125 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-30 12:17:45 +00:00
Shreya
8735fb0e66
Update gaussian_fuzzyset.py
Fix membership method to return standard float type
2024-10-30 17:47:21 +05:30
Shreya
4cdeb391c2
Update gaussian_fuzzyset.py 2024-10-30 17:41:52 +05:30
pre-commit-ci[bot]
b4a3e412e7 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-30 12:03:03 +00:00
Shreya
63603cfe51
Update gaussian_fuzzyset.py 2024-10-30 17:32:38 +05:30
pre-commit-ci[bot]
5f14a63eda [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-30 11:51:58 +00:00
Shreya
03ae118193
Update gaussian_fuzzyset.py
corrected the np.float64(1.0) error
2024-10-30 17:21:35 +05:30
pre-commit-ci[bot]
a05f867d5b [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-30 11:29:12 +00:00
Shreya123714
fe0cc8dbf8 updating DIRECTORY.md 2024-10-30 11:20:12 +00:00
Shreya123714
c072d085da Add guassian fuzzy set and its operations 2024-10-30 16:46:13 +05:30
5 changed files with 121 additions and 4 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.2
rev: v0.7.4
hooks:
- id: ruff
- id: ruff-format
@ -42,7 +42,7 @@ repos:
pass_filenames: false
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.22
rev: v0.23
hooks:
- id: validate-pyproject

View File

@ -447,6 +447,7 @@
## Fuzzy Logic
* [Fuzzy Operations](fuzzy_logic/fuzzy_operations.py)
* [Gaussian Fuzzyset](fuzzy_logic/gaussian_fuzzyset.py)
## Genetic Algorithm
* [Basic String](genetic_algorithm/basic_string.py)

View File

@ -172,7 +172,7 @@ def solved(values):
def from_file(filename, sep="\n"):
"Parse a file into a list of strings, separated by sep."
return open(filename).read().strip().split(sep) # noqa: SIM115
return open(filename).read().strip().split(sep)
def random_puzzle(assignments=17):

View File

@ -0,0 +1,116 @@
"""
By @Shreya123714
https://en.wikipedia.org/wiki/Fuzzy_set
https://en.wikipedia.org/wiki/Fuzzy_set_operations
https://en.wikipedia.org/wiki/Membership_function_(mathematics)
"""
from __future__ import annotations
from dataclasses import dataclass
import matplotlib.pyplot as plt
import numpy as np
@dataclass
class GaussianFuzzySet:
"""
A class for representing and manipulating Gaussian fuzzy sets.
Attributes:
name: The name or label of the fuzzy set.
mean: The mean value (center) of the Gaussianfuzzy set.
std_dev: The standard deviation (controls the spread) of
the Gaussian fuzzy set.
is_complement: Indicates whether this is the complement
of the original fuzzy set.
Methods:
membership(member): Calculate the membership value of
an input 'member'in the fuzzy set.
complement(): Create a new GaussianFuzzySet instance representing
the complement.
plot(): Plot the membership function of the fuzzy set.
>>> fuzzy_set = GaussianFuzzySet("Medium Temperature", mean=25, std_dev=5)
>>> fuzzy_set.membership(25)
1.0
>>> fuzzy_set.membership(30)
0.6065306597126334
>>> fuzzy_set.complement().membership(25)
0.0
"""
name: str
mean: float
std_dev: float
is_complement: bool = False # This flag indicates if it's the complement set
def membership(self, member: float) -> float:
"""
Calculate the membership value of an input 'member' in the Gaussian fuzzy set.
If it's a complement set, returns 1 - the Gaussian membership.
>>> GaussianFuzzySet("Medium", 0, 1).membership(0)
1.0
>>> GaussianFuzzySet("Medium", 0, 1).membership(1)
0.6065306597126334
"""
membership_value = np.exp(-0.5 * ((member - self.mean) / self.std_dev) ** 2)
# Directly return for non-complement or return 1 - membership for complement
return (
float(membership_value)
if not self.is_complement
else 1 - float(membership_value)
)
def complement(self) -> GaussianFuzzySet:
"""
Create a new GaussianFuzzySet instance representing the complement.
>>> GaussianFuzzySet("Medium", 0, 1).complement().membership(0)
0.0
"""
return GaussianFuzzySet(
f"¬{self.name}",
self.mean,
self.std_dev,
is_complement=not self.is_complement,
)
def plot(self) -> None:
"""
Plot the membership function of the Gaussian fuzzy set.
"""
x = np.linspace(
self.mean - 3 * self.std_dev, self.mean + 3 * self.std_dev, 1000
)
y = [self.membership(xi) for xi in x]
plt.plot(x, y, label=self.name)
plt.xlabel("x")
plt.ylabel("Membership")
plt.legend()
if __name__ == "__main__":
from doctest import testmod
testmod()
# Create an instance of GaussianFuzzySet
fuzzy_set = GaussianFuzzySet("Medium Temperature", mean=25, std_dev=5)
# Display some membership values
print(f"Membership at mean (25): {fuzzy_set.membership(25)}")
print(f"Membership at 30: {fuzzy_set.membership(30)}")
print(
f"Complement Membership at mean (25): {fuzzy_set.complement().membership(25)}"
)
# Plot the Gaussian Fuzzy Set and its complement
fuzzy_set.plot()
fuzzy_set.complement().plot()
plt.title("Gaussian Fuzzy Set and its Complement")
plt.show()

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!python
import os
try: