Skip to content

Example 01: Fuzzy numbers

Arithmetic on triangular, intuitionistic, Pythagorean and spherical numbers, a second operator family, scores and a weighted average.

Source

examples/01_fuzzy_numbers.py
"""Fuzzy numbers: arithmetic, operator families, scores and weighted aggregation."""

import math

from bulanik import IFN, PFN, TFN, SphericalFN
from bulanik.aggregate import wa

# Triangular numbers use interval-style arithmetic.
cost = TFN(1, 2, 3) + TFN(3, 4, 5)
print("TFN sum:", cost, "centroid score:", cost.score())

# Intuitionistic numbers: the algebraic sum is the probabilistic sum of mu.
a, b = IFN(0.6, 0.3), IFN(0.5, 0.4)
total = a + b
assert math.isclose(total.mu, 0.8, rel_tol=1e-9)
print("IFN sum:", total)

# Scalar multiplication, and the algebraic vs Einstein operator families.
print("2 x a  :", a * 2)
print("algebraic:", a.add(b, ops="algebraic"))
print("einstein :", a.add(b, ops="einstein"))

# Scores are named too; results report the name you picked.
print("score difference:", round(total.score("difference"), 4), "normalized:", round(total.score("normalized"), 4))

# Pythagorean and spherical numbers allow more room than intuitionistic ones.
p = PFN(0.8, 0.5)  # 0.64 + 0.25 <= 1, impossible as an IFN
s = SphericalFN(0.6, 0.3, 0.4)
print("PFN:", p, "score:", round(p.score(), 4), "| Spherical:", s, "score:", round(s.score(), 4))

# Weighted average of two opinions.
print("WA:", wa([a, b], [0.7, 0.3]))

Output

Run when this page is built:

TFN sum: TFN(l=4.0, m=6.0, u=8.0) centroid score: 6.0
IFN sum: IFN(mu=0.8, nu=0.12)
2 x a  : IFN(mu=0.84, nu=0.09)
algebraic: IFN(mu=0.8, nu=0.12)
einstein : IFN(mu=0.8461538461538461, nu=0.08450704225352113)
score difference: 0.68 normalized: 0.84
PFN: PFN(mu=0.8, nu=0.5) score: 0.39 | Spherical: SphericalFN(mu=0.6, nu=0.3, pi=0.4) score: 0.03
WA: IFN(mu=0.5723061600035249, nu=0.32704150727080533)

What to notice

  • The algebraic and Einstein families give different sums for the same two IFNs; the result always reports the operator used.
  • score() is a number you can sort on, but each type has its own default (centroid for triangular, difference for orthopair).
  • Weights are validated, and wa returns the same number type you put in.

Go further

Run it yourself: python examples/01_fuzzy_numbers.py.