Python/fuzzy_logic/fuzzy_operations.py
2023-10-28 14:09:18 +05:30

132 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import matplotlib.pyplot as plt
import numpy as np
""""
FuzzySet class for triangular fuzzy sets
Author: Shreya123714
Source: https://en.wikipedia.org/wiki/Fuzzy_set
"""
class FuzzySet:
"""
A class for representing and
manipulating triangular fuzzy sets.
Attributes:
name (str): The name or label of the fuzzy set.
a (float): The left boundary of the fuzzy set.
b (float): The peak (central) value of the fuzzy set.
c (float): 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 fuzzy set.
complement(): Calculate the complement (negation)
of this fuzzy set.
plot(): Plot the membership function of the fuzzy set.
"""
def __init__(
self, name: str, left_boundary: float, peak: float, right_boundary: float
) -> None:
"""
Initializes a triangular fuzzy set
with the given parameters.
Args:
name (str): The name or label of the fuzzy set.
a (float): The left boundary of the fuzzy set.
b (float): The peak (central) value of
the fuzzy set.
c (float): The right boundary of the fuzzy set.
"""
self.name = name # Fuzzy set name
self.left_boundary = left_boundary # Left boundary
self.peak = peak # Peak value
self.right_boundary = right_boundary # Right boundary
def membership(self, x):
"""
Calculate the membership value of
an input 'x' in the fuzzy set.
Args:
x (float): The input value for
which the membership is calculated.
Returns:
float: The membership value of 'x' in
the fuzzy set.
"""
if x <= self.left_boundary or x >= self.right_boundary:
return 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)
def union(self, other):
"""
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.
"""
union_name = f"{self.name} {other.name}"
return FuzzySet(
union_name,
min(self.left_boundary, other.left_boundary),
max(self.right_boundary, other.right_boundary),
(self.peak + other.peak) / 2,
)
def intersection(self, other):
"""
Calculate the intersection of this
fuzzy set with another fuzzy set.
Args:
other (FuzzySet): Another fuzzy set to intersect with.
Returns:
FuzzySet: A new fuzzy set representing the intersection.
"""
intersection_name = f"{self.name}{other.name}"
return FuzzySet(
intersection_name,
max(self.left_boundary, other.left_boundary),
min(self.right_boundary, other.right_boundary),
(self.peak + other.peak) / 2,
)
def complement(self):
"""
Calculate the complement (negation) of this fuzzy set.
Returns:
FuzzySet: A new fuzzy set representing the complement.
"""
complement_name = f"¬{self.name}"
return FuzzySet(complement_name, 1 - self.right_boundary, 1 - self.left_boundary, 1 - self.peak)
def plot(self):
"""
Plot the membership function of the fuzzy set.
"""
x = np.linspace(0, 1, 1000)
y = [self.membership(xi) for xi in x]
plt.plot(x, y, label=self.name)
def __str__(self):
return f"{self.name}: [{self.left_boundary}, {self.peak}, {self.right_boundary}]"