mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-09-28 21:45:27 +08:00
* TLC for fuzzy_logic/: restore disabled demo dependency-free + add README - Restore fuzzy_operations.py.DISABLED.txt as fuzzy_set_operations.py, rewritten to drop the scikit-fuzzy dependency (NumPy only). Implements the Zadeh operators on sampled membership vectors: union, intersection, complement, difference, algebraic sum/product, bounded sum/difference, plus a triangular_membership helper. Every function has doctests; the __main__ block reproduces the original 'young vs. middle-aged' demo and degrades gracefully when matplotlib is absent. - Add fuzzy_logic/README.md covering both modules with Wikipedia links and a runnable example. - Remove the obsolete .DISABLED.txt (its intent is now the working module). Credit to Jigyasa Gandhi (original scikit-fuzzy demo) and @Shreya123714 (FuzzySet). Passes pytest --doctest-modules, ruff check, ruff format --check. * fuzzy_logic: descriptive parameter names + drop __future__ annotations Address review on #15166: - rename single-letter params (x->grid, a/b->membership_a/membership_b, fuzzy_complement arg -> membership) per algorithms-keeper + cclauss - drop 'from __future__ import annotations' (repo is Python 3.14t-only) * fuzzy_logic: descriptive parameter names + drop __future__ annotations Address review on #15166: - rename single-letter params (x->grid, a/b->membership_a/membership_b, fuzzy_complement arg -> membership) per algorithms-keeper + cclauss - drop 'from __future__ import annotations' (repo is Python 3.14t-only)
Fuzzy Logic
Fuzzy logic generalizes classical (crisp) set theory: instead of an element
either belonging to a set or not, it belongs to a degree between 0 and 1.
That degree is given by a membership function mu: X -> [0, 1] over a universe
of discourse X. Fuzzy logic is widely used in control systems, decision making,
and pattern recognition where boundaries are naturally vague ("young", "warm",
"fast").
Learn more:
- Fuzzy logic
- Fuzzy set
- Membership function
- T-norm (the family of fuzzy AND/OR operators)
Contents
| File | What it does |
|---|---|
fuzzy_operations.py |
A FuzzySet class modelling a triangular fuzzy number by its (left, peak, right) points, with membership, union, intersection, complement, and plotting. Best when your fuzzy sets are triangular and you want to keep working with the parameters. |
fuzzy_set_operations.py |
The classic Zadeh operators on sampled membership vectors: union, intersection, complement, difference, algebraic_sum/product, and bounded_sum/difference. Works on any membership shape (triangular, trapezoidal, Gaussian, ...) because it operates on the sampled values directly. Dependency-free (NumPy only). |
The two files are complementary: fuzzy_operations.py stays in the parametric
(left, peak, right) representation, while fuzzy_set_operations.py works on the
discretized membership arrays and therefore supports the full set of Zadeh
operators for arbitrary shapes.
Quick example
import numpy as np
from fuzzy_logic.fuzzy_set_operations import (
triangular_membership,
fuzzy_union,
fuzzy_intersection,
)
universe = np.linspace(0, 75, 75)
young = triangular_membership(universe, 0, 25, 50)
middle_aged = triangular_membership(universe, 25, 50, 75)
young_or_middle_aged = fuzzy_union(young, middle_aged)
young_and_middle_aged = fuzzy_intersection(young, middle_aged)
Run the doctests for either module with:
python -m doctest -v fuzzy_logic/fuzzy_set_operations.py