From f6c24ce10b733dd16b07d4d1c9139e2811ff364f Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Wed, 30 Oct 2024 15:57:30 +0530 Subject: [PATCH 1/3] Add Gaussian fuzzy set implementation --- fuzzy_logic/Gaussian_fuzzy_set.py | 93 +++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 fuzzy_logic/Gaussian_fuzzy_set.py diff --git a/fuzzy_logic/Gaussian_fuzzy_set.py b/fuzzy_logic/Gaussian_fuzzy_set.py new file mode 100644 index 000000000000..8ca8947d98ac --- /dev/null +++ b/fuzzy_logic/Gaussian_fuzzy_set.py @@ -0,0 +1,93 @@ +""" +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 numpy as np +import matplotlib.pyplot as plt + +@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 Gaussian fuzzy 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(x): Calculate the membership value of an input 'x' 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, x: float) -> float: + """ + Calculate the membership value of an input 'x' 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 * ((x - self.mean) / self.std_dev) ** 2) + return 1 - membership_value if self.is_complement else 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): + """ + 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() From 21819c123838c0a9ae533de86ec529bc483ba9af Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Wed, 30 Oct 2024 10:29:10 +0000 Subject: [PATCH 2/3] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..fd916ee9df09 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -446,6 +446,7 @@ * [Vicsek](fractals/vicsek.py) ## Fuzzy Logic + * [Gaussian Fuzzy Set](fuzzy_logic/Gaussian_fuzzy_set.py) * [Fuzzy Operations](fuzzy_logic/fuzzy_operations.py) ## Genetic Algorithm From a30c9e52362401d32c16f3118f0c7d3ec0fec870 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:59:50 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/Gaussian_fuzzy_set.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/fuzzy_logic/Gaussian_fuzzy_set.py b/fuzzy_logic/Gaussian_fuzzy_set.py index 8ca8947d98ac..b319f6a56fa9 100644 --- a/fuzzy_logic/Gaussian_fuzzy_set.py +++ b/fuzzy_logic/Gaussian_fuzzy_set.py @@ -11,11 +11,12 @@ import numpy as np import matplotlib.pyplot as plt + @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 Gaussian fuzzy set. @@ -26,7 +27,7 @@ class GaussianFuzzySet: membership(x): Calculate the membership value of an input 'x' 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 @@ -45,7 +46,7 @@ def membership(self, x: float) -> float: """ Calculate the membership value of an input 'x' 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) @@ -57,25 +58,34 @@ def membership(self, x: float) -> float: 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) + return GaussianFuzzySet( + f"¬{self.name}", + self.mean, + self.std_dev, + is_complement=not self.is_complement, + ) def plot(self): """ 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) + 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 @@ -84,7 +94,9 @@ def plot(self): # 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)}") + print( + f"Complement Membership at mean (25): {fuzzy_set.complement().membership(25)}" + ) # Plot the Gaussian Fuzzy Set and its complement fuzzy_set.plot()