|
| 1 | +""" |
| 2 | +By @Shreya123714 |
| 3 | +
|
| 4 | +https://en.wikipedia.org/wiki/Fuzzy_set |
| 5 | +https://en.wikipedia.org/wiki/Fuzzy_set_operations |
| 6 | +https://en.wikipedia.org/wiki/Membership_function_(mathematics) |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | +from dataclasses import dataclass |
| 11 | +import numpy as np |
| 12 | +import matplotlib.pyplot as plt |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class GaussianFuzzySet: |
| 16 | + """ |
| 17 | + A class for representing and manipulating Gaussian fuzzy sets. |
| 18 | + |
| 19 | + Attributes: |
| 20 | + name: The name or label of the fuzzy set. |
| 21 | + mean: The mean value (center) of the Gaussian fuzzy set. |
| 22 | + std_dev: The standard deviation (controls the spread) of the Gaussian fuzzy set. |
| 23 | + is_complement: Indicates whether this is the complement of the original fuzzy set. |
| 24 | +
|
| 25 | + Methods: |
| 26 | + membership(x): Calculate the membership value of an input 'x' in the fuzzy set. |
| 27 | + complement(): Create a new GaussianFuzzySet instance representing the complement. |
| 28 | + plot(): Plot the membership function of the fuzzy set. |
| 29 | + |
| 30 | + >>> fuzzy_set = GaussianFuzzySet("Medium Temperature", mean=25, std_dev=5) |
| 31 | + >>> fuzzy_set.membership(25) |
| 32 | + 1.0 |
| 33 | + >>> fuzzy_set.membership(30) |
| 34 | + 0.6065306597126334 |
| 35 | + >>> fuzzy_set.complement().membership(25) |
| 36 | + 0.0 |
| 37 | + """ |
| 38 | + |
| 39 | + name: str |
| 40 | + mean: float |
| 41 | + std_dev: float |
| 42 | + is_complement: bool = False # This flag indicates if it's the complement set |
| 43 | + |
| 44 | + def membership(self, x: float) -> float: |
| 45 | + """ |
| 46 | + Calculate the membership value of an input 'x' in the Gaussian fuzzy set. |
| 47 | + If it's a complement set, returns 1 - the Gaussian membership. |
| 48 | + |
| 49 | + >>> GaussianFuzzySet("Medium", 0, 1).membership(0) |
| 50 | + 1.0 |
| 51 | + >>> GaussianFuzzySet("Medium", 0, 1).membership(1) |
| 52 | + 0.6065306597126334 |
| 53 | + """ |
| 54 | + membership_value = np.exp(-0.5 * ((x - self.mean) / self.std_dev) ** 2) |
| 55 | + return 1 - membership_value if self.is_complement else membership_value |
| 56 | + |
| 57 | + def complement(self) -> GaussianFuzzySet: |
| 58 | + """ |
| 59 | + Create a new GaussianFuzzySet instance representing the complement. |
| 60 | + |
| 61 | + >>> GaussianFuzzySet("Medium", 0, 1).complement().membership(0) |
| 62 | + 0.0 |
| 63 | + """ |
| 64 | + return GaussianFuzzySet(f"¬{self.name}", self.mean, self.std_dev, is_complement=not self.is_complement) |
| 65 | + |
| 66 | + def plot(self): |
| 67 | + """ |
| 68 | + Plot the membership function of the Gaussian fuzzy set. |
| 69 | + """ |
| 70 | + x = np.linspace(self.mean - 3 * self.std_dev, self.mean + 3 * self.std_dev, 1000) |
| 71 | + y = [self.membership(xi) for xi in x] |
| 72 | + plt.plot(x, y, label=self.name) |
| 73 | + plt.xlabel("x") |
| 74 | + plt.ylabel("Membership") |
| 75 | + plt.legend() |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + from doctest import testmod |
| 79 | + testmod() |
| 80 | + |
| 81 | + # Create an instance of GaussianFuzzySet |
| 82 | + fuzzy_set = GaussianFuzzySet("Medium Temperature", mean=25, std_dev=5) |
| 83 | + |
| 84 | + # Display some membership values |
| 85 | + print(f"Membership at mean (25): {fuzzy_set.membership(25)}") |
| 86 | + print(f"Membership at 30: {fuzzy_set.membership(30)}") |
| 87 | + print(f"Complement Membership at mean (25): {fuzzy_set.complement().membership(25)}") |
| 88 | + |
| 89 | + # Plot the Gaussian Fuzzy Set and its complement |
| 90 | + fuzzy_set.plot() |
| 91 | + fuzzy_set.complement().plot() |
| 92 | + plt.title("Gaussian Fuzzy Set and its Complement") |
| 93 | + plt.show() |
0 commit comments