Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
98 changes: 98 additions & 0 deletions fuzzy_logic/trapazoidal_fuzzyset.py
Original file line number Diff line number Diff line change
@@ -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.

Check failure on line 13 in fuzzy_logic/trapazoidal_fuzzyset.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

fuzzy_logic/trapazoidal_fuzzyset.py:13:1: W293 Blank line contains whitespace
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)

Check failure on line 49 in fuzzy_logic/trapazoidal_fuzzyset.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

fuzzy_logic/trapazoidal_fuzzyset.py:49:89: E501 Line too long (91 > 88)
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)

Check failure on line 53 in fuzzy_logic/trapazoidal_fuzzyset.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

fuzzy_logic/trapazoidal_fuzzyset.py:53:89: E501 Line too long (94 > 88)

Check failure on line 54 in fuzzy_logic/trapazoidal_fuzzyset.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

fuzzy_logic/trapazoidal_fuzzyset.py:54:1: W293 Blank line contains whitespace
# 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.

Check failure on line 61 in fuzzy_logic/trapazoidal_fuzzyset.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

fuzzy_logic/trapazoidal_fuzzyset.py:61:1: W293 Blank line contains whitespace
>>> 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,

Check failure on line 65 in fuzzy_logic/trapazoidal_fuzzyset.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

fuzzy_logic/trapazoidal_fuzzyset.py:65:89: E501 Line too long (117 > 88)
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)

Check failure on line 86 in fuzzy_logic/trapazoidal_fuzzyset.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

fuzzy_logic/trapazoidal_fuzzyset.py:86:89: E501 Line too long (115 > 88)

# 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()

Loading