fuzzy_set.fuzzy_set module#

class fuzzy_set.fuzzy_set.FuzzySet(mu: dict[float, float], is_continuous: bool = True, e: tuple[float, float] = (-inf, inf))[source]#

Bases: dict

A fuzzy set is defined by a \(A = (U, \mu)\) pair, where:

  • \(U\) is a set called universe;

  • \(m: E \rightarrow [0, 1]\) is the membership function of :math:̀`A`.

The FuzzySet class implements a Fuzzy set \(U \subseteq \mathbb{R}\) having either

  • a piecewise linear membership function;

  • a discrete membership function.

In the FuzzySet class implementation, you should choose a universe as tight as possible with respect to the support of the fuzzy set you need to manipulate.

Example:

>>> import matplotlib.pyplot as plt
>>> from fuzzy_set import FuzzySet
>>> a1 = FuzzySet(
...     dict(enumerate([0, 0, 0.1, 0.2, 0.7, 1, 1, 0.7, 0.2, 0.1, 0]))
... )
>>> a1.plot(label="$a_1$", marker="x")
[<matplotlib.lines.Line2D object at ...>]
>>> plt.legend()
<matplotlib.legend.Legend object at ...>
>>> plt.grid()
absolute_difference(other: FuzzySet) FuzzySet[source]#

Computes the absolute default difference of two fuzzy-sets.

Parameters:

other (FuzzySet) – The right operand.

Returns:

The default fuzzy set absolute difference.

at(alpha: float) set[source]#

Assuming this fuzzy set is discrete, retrieves the element that matches a given arbitrary \(alpha\) value.

Parameters:

alpha (float) – The \(alpha value, contained in the :math:`[0.0, 1.0]\) interval.

card() float[source]#

Computes the scalar cardinality of a fuzzy set.

Returns:

The scalar cardinality of this fuzzy set.

concentration() FuzzySet[source]#

Computes the concentration of this fuzzy set.

Returns:

The corresponding fuzzy set.

core() set[source]#

Assuming this fuzzy set is discrete, computes the core of this fuzzy set, denoted by \(A^{=1}\).

Returns:

The crisp set containing every element involved in the core.

cut(alpha: float) set[source]#

Computes an \(\alpha\)-cut of this fuzzy set, denoted by \(A^{x \ge \alpha}\).

Parameters:

alpha (float) – The threshold involved in the \(\alpha\)-cut.

Returns:

The crisp set containing every element involved in the \(\alpha\)-cut.

difference(other: FuzzySet, tnorm: callable) FuzzySet[source]#

Computes the difference of two fuzzy-sets.

Parameters:
  • other (FuzzySet) – The right operand.

  • tnorm (callable) – A T-norm.

Returns:

The fuzzy set difference.

doesnt_contain(x: float) bool[source]#

Checks whether this FuzzySet instance doesn’t contains an element.

Parameters:

x (float) – The considered element.

Returns:

True if x isn’t contained in self, False otherwise.

fully_contains(x: float) bool[source]#

Checks whether this FuzzySet instance fully contains an element.

Parameters:

x (float) – The considered element.

Returns:

True if x is fully contained in self, False otherwise.

height() float[source]#

Computes the height of this fuzzy set.

Returns:

The height of this fuzzy set.

intersection(other: ~fuzzy_set.fuzzy_set.FuzzySet, tnorm: callable = <built-in function min>) FuzzySet[source]#

Computes the intersection of two fuzzy-sets according to an arbitrary T-norm (e.g., \(min\) ).

Parameters:
  • other (FuzzySet) – The right operand.

  • tnorm (callable) – A T-norm.

Returns:

The fuzzy set intersection.

is_crossover(x: float) bool[source]#

Checks whether a value is a crossover point of this fuzzy set.

Parameters:

x (float) – A value in self.

Returns:

True if x is a crossover points of this fuzzy set, False otherwise.

is_disjoint(other: FuzzySet) bool[source]#

Tests whether this fuzzy set and another are disjoint.

Parameters:

other (FuzzySet) – The fuzzy set compared to self.

Returns:

True if the fuzzy sets are disjoint, False otherwise.

is_empty() bool[source]#

Assuming this fuzzy set is discrete, checks whether this fuzzy set is empty.

Returns:

True if this fuzzy set is empty, False otherwise.

is_normalized() bool[source]#

Tests whether this fuzzy set is normalized.

Returns:

True if this fuzzy set is normalized, False otherwise.

m(x: float) float[source]#

Retrieves the membership degrees of an element.

If this FuzzySet instance is continuous, (i.e., if self.is_continuous), the value use obtained using a linear interpolation.

Parameters:

x (float) – The considered element.

Returns:

The membership value corresponding to x.

partially_contains(x: float) bool[source]#

Checks whether this FuzzySet instance partially contains an element.

Parameters:

x (float) – The considered element.

Returns:

True if x is partially contained in self, False otherwise.

plot(*cls, ax: Axes = None, **kwargs) PathCollection[source]#

Plots this Fuzzy Set.

Parameters:
  • cls – See the plt.plot() function.

  • args – See the plt.plot() function.

  • ax (matplotlib.axes.Axes) – The axes of the figure where this FuzzySet instance must be plotted. Pass None if not needed.

Returns:

The resulting corresponding plot.

relative_card(other: FuzzySet = None) float[source]#

Computes the relative cardinality of this fuzzy set or the intersection of two fuzzy set.

Parameters:

g (FuzzySet) – Another fuzzy set. Pass None or self is equivalent.

Returns:

The relative cardinality of this fuzzy set.

strong_cut(alpha: float) set[source]#

Assuming this fuzzy set is discrete, computes a strong \(\alpha\)-cut of this fuzzy set, denoted by \(A^{x \gt \alpha}\).

Parameters:

alpha (float) – The \(alpha value, contained in the :math:`[0.0, 1.0]\) interval.

Returns:

The crisp set containing every element involved in the \(\alpha\)-cut.

support() set[source]#

Assuming this fuzzy set is discrete, computes the support of this fuzzy set, denoted by \(A^{>0}\).

Returns:

The crisp set containing every element involved in the support.

union(other: ~fuzzy_set.fuzzy_set.FuzzySet, snorm: callable = <built-in function min>) FuzzySet[source]#

Computes the union of two fuzzy-sets according to an arbitrary S-norm (e.g., \(min\) ).

Parameters:
  • other (FuzzySet) – The right operand.

  • snorm (callable) – A T-norm.

Returns:

The fuzzy set intersection.

width() float[source]#

Computes the width of this fuzzy set.

Returns:

The width of this fuzzy set.

fuzzy_set.fuzzy_set.INFINITY = inf#

Infinity (\(\infty\))

fuzzy_set.fuzzy_set.REALS = (-inf, inf)#

The set of real numbers (\(\mathbb{R}`\))