by shreya123714 https en wikipedia orgwikifuzzyset a class for representing and manipulating triangular fuzzy sets attributes name the name or label of the fuzzy set leftboundary the left boundary of the fuzzy set peak the peak central value of the fuzzy set rightboundary the right boundary of the fuzzy set methods membershipx calculate the membership value of an input x in the fuzzy set unionother calculate the union of this fuzzy set with another fuzzy set intersectionother 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 fuzzysetsheru 0 4 1 0 6 sheru fuzzysetname sheru leftboundary0 4 peak1 rightboundary0 6 strsheru sheru 0 4 1 0 6 siya fuzzysetsiya 0 5 1 0 7 siya fuzzysetname siya leftboundary0 5 peak1 rightboundary0 7 complement operation sheru complement fuzzysetname sheru leftboundary0 4 peak0 6 rightboundary0 siya complement doctest normalizewhitespace fuzzysetname siya leftboundary0 30000000000000004 peak0 5 rightboundary0 intersection operation siya intersectionsheru fuzzysetname siya sheru leftboundary0 5 peak0 6 rightboundary1 0 membership operation sheru membership0 5 0 16666666666666663 sheru membership0 6 0 0 union operations siya unionsheru fuzzysetname siya sheru leftboundary0 4 peak0 7 rightboundary1 0 fuzzysetfuzzyset 0 1 0 2 0 3 fuzzysetname fuzzyset leftboundary0 1 peak0 2 rightboundary0 3 calculate the complement negation of this fuzzy set returns fuzzyset a new fuzzy set representing the complement fuzzysetfuzzyset 0 1 0 2 0 3 complement fuzzysetname fuzzyset leftboundary0 7 peak0 9 rightboundary0 8 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 fuzzyseta 0 1 0 2 0 3 intersectionfuzzysetb 0 4 0 5 0 6 fuzzysetname a b leftboundary0 4 peak0 3 rightboundary0 35 calculate the membership value of an input x in the fuzzy set returns the membership value of x in the fuzzy set a fuzzyseta 0 1 0 2 0 3 a membership0 09 0 0 a membership0 1 0 0 a membership0 11 0 09999999999999995 a membership0 4 0 0 fuzzyseta 0 0 5 1 membership0 1 0 2 fuzzysetb 0 2 0 7 1 membership0 6 0 8 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 fuzzyseta 0 1 0 2 0 3 unionfuzzysetb 0 4 0 5 0 6 fuzzysetname a b leftboundary0 1 peak0 6 rightboundary0 35 plot the membership function of the fuzzy set 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 fuzzyset fuzzy_set 0 1 0 2 0 3 fuzzyset name fuzzy_set left_boundary 0 1 peak 0 2 right_boundary 0 3 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 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 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 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 plot the membership function of the fuzzy set
from __future__ import annotations

from dataclasses import dataclass

import matplotlib.pyplot as plt
import numpy as np


@dataclass
class FuzzySet:
    

    name: str
    left_boundary: float
    peak: float
    right_boundary: float

    def __str__(self) -> str:
        
        return (
            f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]"
        )

    def complement(self) -> FuzzySet:
        
        return FuzzySet(
            f"¬{self.name}",
            1 - self.right_boundary,
            1 - self.left_boundary,
            1 - self.peak,
        )

    def intersection(self, other) -> FuzzySet:
        
        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:
        
        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:
        
        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):
        
        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()
    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()