From 47d55a8fedc8eea80eb8e4e1b241bcb52214b51c Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Wed, 30 Oct 2024 18:51:27 +0530 Subject: [PATCH 1/5] created a trapazoidal fuzzy set class --- fuzzy_logic/trapazoidal_fuzzyset.py | 98 +++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 fuzzy_logic/trapazoidal_fuzzyset.py diff --git a/fuzzy_logic/trapazoidal_fuzzyset.py b/fuzzy_logic/trapazoidal_fuzzyset.py new file mode 100644 index 000000000000..f4ca2ea0bc32 --- /dev/null +++ b/fuzzy_logic/trapazoidal_fuzzyset.py @@ -0,0 +1,98 @@ +from __future__ import annotations + +from dataclasses import dataclass + +import matplotlib.pyplot as plt +import numpy as np + + +@dataclass +class TrapezoidalFuzzySet: + """ + Represents and manipulates trapezoidal fuzzy sets. + + Attributes: + name: The name or label of the fuzzy set. + left_base: The left base of the trapezoid. + left_peak: The top left vertex of the trapezoid. + right_peak: The top right vertex of the trapezoid. + right_base: The right base of the trapezoid. + is_complement: Indicates if this is the complement of the fuzzy set. + + Methods: + membership(value): Calculates membership value for input 'value'. + complement(): Creates left_base TrapezoidalFuzzySet instance representing + the complement. + plot(): Plots the membership function of the fuzzy set. + """ + + name: str + left_base: float + left_peak: float + right_peak: float + right_base: float + is_complement: bool = False # Flag for complement set + + def membership(self, value: float) -> float: + """ + Calculates membership value for input 'value'. For complement sets, + returns 1 - trapezoidal membership. + + >>> TrapezoidalFuzzySet("Medium", 0, 1, 2, 3).membership(1) + 1.0 + >>> TrapezoidalFuzzySet("Medium", 0, 1, 2, 3).membership(0.5) + 0.5 + """ + if value < self.left_base or value > self.right_base: + membership_value = 0.0 + elif self.left_base <= value < self.left_peak: + membership_value = (value - self.left_base) / (self.left_peak - self.left_base) + elif self.left_peak <= value <= self.right_peak: + membership_value = 1.0 + elif self.right_peak < value <= self.right_base: + membership_value = (self.right_base - value) / (self.right_base - self.right_peak) + + # For complements, invert the membership value + return membership_value if not self.is_complement else 1 - membership_value + + def complement(self) -> TrapezoidalFuzzySet: + """ + Creates a new TrapezoidalFuzzySet instance representing the complement. + + >>> TrapezoidalFuzzySet("Medium", 0, 1, 2, 3).complement().membership(1) + 0.0 + """ + return TrapezoidalFuzzySet(f"¬{self.name}", self.left_base, self.left_peak, self.right_peak, self.right_base, + is_complement=not self.is_complement) + + def plot(self) -> None: + """ + Plots the membership function of the trapezoidal fuzzy set. + """ + x = np.linspace(self.left_base - 1, self.right_base + 1, 1000) + y = [self.membership(xi) for xi in x] + plt.plot(x, y, label=self.name) + plt.xlabel("Value") + plt.ylabel("Membership") + plt.title(f"Membership Function for {self.name}") + plt.grid() + plt.legend() + +if __name__ == "__main__": + from doctest import testmod + testmod() + + # Create an instance of TrapezoidalFuzzySet + fuzzy_set = TrapezoidalFuzzySet("Medium Temperature", left_base=20, left_peak=25, right_peak=30, right_base=35) + + # Display some membership values + print(f"Membership at 25: {fuzzy_set.membership(25)}") + print(f"Membership at 22: {fuzzy_set.membership(22)}") + print(f"Complement Membership at 25: {fuzzy_set.complement().membership(25)}") + + # Plot of Trapezoidal Fuzzy Set and its complement + fuzzy_set.plot() + fuzzy_set.complement().plot() + plt.title("Trapezoidal Fuzzy Set and its Complement") + plt.show() + From 92ed43df763ef927cd2ad02c6be7223b846af5bc Mon Sep 17 00:00:00 2001 From: Shreya123714 Date: Wed, 30 Oct 2024 13:21:52 +0000 Subject: [PATCH 2/5] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..08ea338dd4ec 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -447,6 +447,7 @@ ## Fuzzy Logic * [Fuzzy Operations](fuzzy_logic/fuzzy_operations.py) + * [Trapazoidal Fuzzyset](fuzzy_logic/trapazoidal_fuzzyset.py) ## Genetic Algorithm * [Basic String](genetic_algorithm/basic_string.py) From fd26b96052b13bbf89b833d9827b88f12a1ddb73 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 13:28:20 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/trapazoidal_fuzzyset.py | 31 ++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/fuzzy_logic/trapazoidal_fuzzyset.py b/fuzzy_logic/trapazoidal_fuzzyset.py index f4ca2ea0bc32..e53d7f16d2b3 100644 --- a/fuzzy_logic/trapazoidal_fuzzyset.py +++ b/fuzzy_logic/trapazoidal_fuzzyset.py @@ -10,7 +10,7 @@ class TrapezoidalFuzzySet: """ Represents and manipulates trapezoidal fuzzy sets. - + Attributes: name: The name or label of the fuzzy set. left_base: The left base of the trapezoid. @@ -46,24 +46,34 @@ def membership(self, value: float) -> float: if value < self.left_base or value > self.right_base: membership_value = 0.0 elif self.left_base <= value < self.left_peak: - membership_value = (value - self.left_base) / (self.left_peak - self.left_base) + membership_value = (value - self.left_base) / ( + self.left_peak - self.left_base + ) elif self.left_peak <= value <= self.right_peak: membership_value = 1.0 elif self.right_peak < value <= self.right_base: - membership_value = (self.right_base - value) / (self.right_base - self.right_peak) - + membership_value = (self.right_base - value) / ( + self.right_base - self.right_peak + ) + # For complements, invert the membership value return membership_value if not self.is_complement else 1 - membership_value def complement(self) -> TrapezoidalFuzzySet: """ Creates a new TrapezoidalFuzzySet instance representing the complement. - + >>> TrapezoidalFuzzySet("Medium", 0, 1, 2, 3).complement().membership(1) 0.0 """ - return TrapezoidalFuzzySet(f"¬{self.name}", self.left_base, self.left_peak, self.right_peak, self.right_base, - is_complement=not self.is_complement) + return TrapezoidalFuzzySet( + f"¬{self.name}", + self.left_base, + self.left_peak, + self.right_peak, + self.right_base, + is_complement=not self.is_complement, + ) def plot(self) -> None: """ @@ -78,12 +88,16 @@ def plot(self) -> None: plt.grid() plt.legend() + if __name__ == "__main__": from doctest import testmod + testmod() # Create an instance of TrapezoidalFuzzySet - fuzzy_set = TrapezoidalFuzzySet("Medium Temperature", left_base=20, left_peak=25, right_peak=30, right_base=35) + fuzzy_set = TrapezoidalFuzzySet( + "Medium Temperature", left_base=20, left_peak=25, right_peak=30, right_base=35 + ) # Display some membership values print(f"Membership at 25: {fuzzy_set.membership(25)}") @@ -95,4 +109,3 @@ def plot(self) -> None: fuzzy_set.complement().plot() plt.title("Trapezoidal Fuzzy Set and its Complement") plt.show() - From c73ceb89dc2e4743db1e182d7e4c1689feff662f Mon Sep 17 00:00:00 2001 From: Shreya <95279016+Shreya123714@users.noreply.github.com> Date: Wed, 30 Oct 2024 19:47:26 +0530 Subject: [PATCH 4/5] Update trapazoidal_fuzzyset.py Added doctest for plot() function --- fuzzy_logic/trapazoidal_fuzzyset.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/fuzzy_logic/trapazoidal_fuzzyset.py b/fuzzy_logic/trapazoidal_fuzzyset.py index e53d7f16d2b3..9378f2de5607 100644 --- a/fuzzy_logic/trapazoidal_fuzzyset.py +++ b/fuzzy_logic/trapazoidal_fuzzyset.py @@ -1,3 +1,10 @@ +""" +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 @@ -78,6 +85,10 @@ def complement(self) -> TrapezoidalFuzzySet: def plot(self) -> None: """ Plots the membership function of the trapezoidal fuzzy set. + + >>> fuzzy_set = TrapezoidalFuzzySet("Medium", left_base=0,left_peak=1, + ... right_peak=2, right_base=3) + >>> fuzzy_set.plot() """ x = np.linspace(self.left_base - 1, self.right_base + 1, 1000) y = [self.membership(xi) for xi in x] From 2f23aeb12f5724977649b9a1ce4e3fcbb26eff74 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 14:19:46 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/trapazoidal_fuzzyset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fuzzy_logic/trapazoidal_fuzzyset.py b/fuzzy_logic/trapazoidal_fuzzyset.py index 9378f2de5607..6b318c243708 100644 --- a/fuzzy_logic/trapazoidal_fuzzyset.py +++ b/fuzzy_logic/trapazoidal_fuzzyset.py @@ -85,8 +85,8 @@ def complement(self) -> TrapezoidalFuzzySet: def plot(self) -> None: """ Plots the membership function of the trapezoidal fuzzy set. - - >>> fuzzy_set = TrapezoidalFuzzySet("Medium", left_base=0,left_peak=1, + + >>> fuzzy_set = TrapezoidalFuzzySet("Medium", left_base=0,left_peak=1, ... right_peak=2, right_base=3) >>> fuzzy_set.plot() """