Fuzzy Sets Functions#

Fuzzy Sets Module for Ex-Fuzzy Library

This module contains the core fuzzy set classes and functionality for the ex-fuzzy library. It implements Type-1, Type-2, and General Type-2 fuzzy sets with their associated membership functions and operations.

Main Components:
  • FUZZY_SETS enum: Defines the types of fuzzy sets supported

  • Membership function implementations (trapezoidal, triangular, gaussian)

  • Fuzzy set classes: FS, IVFS, gaussianFS, gaussianIVFS, categoricalFS, etc.

  • fuzzyVariable class: Container for linguistic variables with multiple fuzzy sets

  • Validation and statistical testing methods for fuzzy variables

The module supports both numerical computation and provides interfaces for PyTorch tensors when available, making it suitable for both traditional fuzzy logic applications and modern machine learning workflows.

ex_fuzzy.fuzzy_sets.trapezoidal_membership(x, params, epsilon=0.0001)[source]#

Compute trapezoidal membership function values.

This function computes the membership degree for input values using a trapezoidal membership function defined by four parameters representing the trapezoid vertices.

Parameters:
  • x (np.array) – Input values in the fuzzy set domain for which to compute membership

  • params (list[float]) – Four numbers [a, b, c, d] defining the trapezoid: - a: left foot (membership starts rising) - b: left shoulder (membership reaches 1.0) - c: right shoulder (membership starts falling) - d: right foot (membership reaches 0.0)

  • epsilon (float, optional) – Small value for numerical stability. Defaults to 10E-5.

Returns:

Membership values in [0, 1] for each input value

Return type:

np.array

Example

>>> x = np.array([0, 1, 2, 3, 4, 5])
>>> params = [1, 2, 3, 4]  # trapezoid from 1 to 4, flat from 2 to 3
>>> membership = trapezoidal_membership(x, params)
>>> print(membership)  # [0, 0, 1, 1, 0, 0]

Note

Special case: if a == d (singleton), returns 1.0 for exact matches, 0.0 otherwise. This handles degenerate trapezoids that collapse to single points.

class ex_fuzzy.fuzzy_sets.FS(name, membership_parameters, domain)[source]#

Bases: object

Base class for Type-1 fuzzy sets (Zadeh fuzzy sets).

This class implements the fundamental Type-1 fuzzy set with crisp membership functions. It serves as the base class for more specialized fuzzy set types like triangular, gaussian, and categorical fuzzy sets.

name#

The linguistic name of the fuzzy set (e.g., “low”, “medium”, “high”)

Type:

str

membership_parameters#

Parameters defining the membership function

Type:

list[float]

domain#

Two-element list defining the universe of discourse [min, max]

Type:

list[float]

Example

>>> fs = FS("medium", [1, 2, 3, 4], [0, 5])  # Trapezoidal fuzzy set
>>> membership = fs.membership(2.5)
>>> print(membership)  # Should be 1.0 (fully in the set)

Note

This class uses trapezoidal membership functions by default. For other shapes, use specialized subclasses like gaussianFS or triangularFS.

__init__(name, membership_parameters, domain)[source]#

Initialize a Type-1 fuzzy set.

Parameters:
  • name (str) – Linguistic name for the fuzzy set

  • membership_parameters (list[float]) – Four parameters [a, b, c, d] defining the trapezoidal membership function where: - a: left foot (membership starts rising from 0) - b: left shoulder (membership reaches 1.0) - c: right shoulder (membership starts falling from 1.0) - d: right foot (membership reaches 0)

  • domain (list[float]) – Two-element list [min, max] defining the universe of discourse for this fuzzy set

Example

>>> fs = FS("medium", [2, 3, 7, 8], [0, 10])
>>> # Creates a trapezoidal set: rises from 2-3, flat 3-7, falls 7-8
membership(x)[source]#

Compute membership degrees for input values.

This method calculates the membership degree(s) for the given input value(s) using the trapezoidal membership function defined by this fuzzy set’s parameters.

Parameters:

x (np.array) – Input value(s) for which to compute membership degrees. Can be a single value, list, or numpy array.

Returns:

Membership degree(s) in the range [0, 1]. Shape matches input.

Return type:

np.array

Example

>>> fs = FS("medium", [2, 3, 7, 8], [0, 10])
>>> print(fs.membership(5))    # 1.0 (in flat region)
>>> print(fs.membership(2.5)) # 0.5 (on rising slope)
>>> print(fs.membership([1, 5, 9])) # [0.0, 1.0, 0.0]
type()[source]#

Return the fuzzy set type identifier.

Returns:

The type identifier (FUZZY_SETS.t1 for Type-1 fuzzy sets)

Return type:

FUZZY_SETS

__call__(x)[source]#

Calling the Fuzzy set returns its membership.

Parameters:

x (array) – input values in the fuzzy set referencial domain.

Returns:

membership of the fuzzy set.

Return type:

array

__str__()[source]#

Returns the name of the fuzzy set, its type and its parameters.

Returns:

string.

Return type:

str

shape()[source]#

Returns the shape of the fuzzy set.

Returns:

string.

Return type:

str

class ex_fuzzy.fuzzy_sets.triangularFS(name, membership_parameters, domain)[source]#

Bases: FS

__init__(name, membership_parameters, domain)[source]#

Initialize a Type-1 fuzzy set.

Parameters:
  • name (str) – Linguistic name for the fuzzy set

  • membership_parameters (list[float]) – Four parameters [a, b, c, d] defining the trapezoidal membership function where: - a: left foot (membership starts rising from 0) - b: left shoulder (membership reaches 1.0) - c: right shoulder (membership starts falling from 1.0) - d: right foot (membership reaches 0)

  • domain (list[float]) – Two-element list [min, max] defining the universe of discourse for this fuzzy set

Example

>>> fs = FS("medium", [2, 3, 7, 8], [0, 10])
>>> # Creates a trapezoidal set: rises from 2-3, flat 3-7, falls 7-8
shape()[source]#

Returns the shape of the fuzzy set.

Returns:

string.

Return type:

str

class ex_fuzzy.fuzzy_sets.categoricalFS(name, category)[source]#

Bases: FS

__init__(name, category)[source]#

Creates a categorical fuzzy set. It gives 1 to the category and 0 to the rest. Use it when the variable is categorical and the categories are known, so that rule inference systems can naturally support both crisp and fuzzy variables.

Parameters:
  • name (str) – string.

  • categories – list of strings. Possible categories.

membership(x)[source]#

Computes the membership of a point or vector of elements.

Parameters:

x (array) – input values in the referencial domain.

type()[source]#

Returns the corresponding fuzzy set type according to FUZZY_SETS enum.

__str__()[source]#

Returns the name of the fuzzy set, its type and its parameters.

Returns:

string.

Return type:

str

shape()[source]#

Returns the shape of the fuzzy set.

Returns:

string.

Return type:

str

class ex_fuzzy.fuzzy_sets.IVFS(name, secondMF_lower, secondMF_upper, domain, lower_height=1.0)[source]#

Bases: FS

Interval-Valued Fuzzy Set (Type-2 Fuzzy Set) class.

This class implements Type-2 fuzzy sets using interval-valued membership functions. Each point in the domain has an interval [lower, upper] representing the uncertainty in the membership degree, providing a more flexible representation than Type-1 sets.

name#

Linguistic name of the fuzzy set

Type:

str

domain#

Universe of discourse [min, max]

Type:

list[float]

secondMF_lower#

Parameters for the lower membership function

Type:

list[float]

secondMF_upper#

Parameters for the upper membership function

Type:

list[float]

lower_height#

Height of the lower membership function

Type:

float

Example

>>> ivfs = IVFS("medium", [2,3,7,8], [1,2,8,9], [0,10], 0.8)
>>> membership = ivfs.membership(5)
>>> print(membership)  # Returns [lower_membership, upper_membership]

Note

The upper membership function should be contained within the lower membership function to maintain mathematical consistency of Type-2 fuzzy sets.

__init__(name, secondMF_lower, secondMF_upper, domain, lower_height=1.0)[source]#

Initialize an Interval-Valued Fuzzy Set.

Parameters:
  • name (str) – Linguistic name for the fuzzy set

  • secondMF_lower (list[float]) – Four parameters [a,b,c,d] for lower trapezoidal membership function (outer boundary)

  • secondMF_upper (list[float]) – Four parameters [a,b,c,d] for upper trapezoidal membership function (inner boundary)

  • domain (list[float]) – Two-element list [min, max] defining universe of discourse

  • lower_height (float, optional) – Maximum height of lower membership function. Defaults to 1.0.

Raises:

AssertionError – If membership function parameters are inconsistent or invalid

Example

>>> ivfs = IVFS("high", [6,7,9,10], [6.5,7.5,8.5,9.5], [0,10], 0.9)
membership(x)[source]#

Computes the iv-membership of a point or a vector.

Parameters:

x (array) – input values in the fuzzy set referencial domain.

Returns:

iv-membership of the fuzzy set.

Return type:

array

type()[source]#

Returns the corresponding fuzzy set type according to FUZZY_SETS enum: (t2)

__str__()[source]#

Returns the name of the fuzzy set, its type and its parameters.

Returns:

string.

Return type:

str

class ex_fuzzy.fuzzy_sets.categoricalIVFS(name, category)[source]#

Bases: IVFS

Class to define a iv fuzzy set with categorical membership.

__init__(name, category)[source]#

Creates a categorical iv fuzzy set. It gives 1 to the category and 0 to the rest. Use it when the variable is categorical and the categories are known, so that rule inference systems can naturally support both crisp and fuzzy variables.

Parameters:
  • name (str) – string.

  • categories – list of strings. Possible categories.

  • domain – list of two numbers. Limits of the domain if the fuzzy set.

membership(x)[source]#

Computes the membership of a point or vector of elements.

Parameters:

x (array) – input values in the referencial domain.

type()[source]#

Returns the corresponding fuzzy set type according to FUZZY_SETS enum.

__str__()[source]#

Returns the name of the fuzzy set, its type and its parameters.

Returns:

string.

Return type:

str

shape()[source]#

Returns the shape of the fuzzy set.

Returns:

string.

Return type:

str

class ex_fuzzy.fuzzy_sets.GT2(name, secondary_memberships, domain, significant_decimals, alpha_cuts=[0.2, 0.4, 0.5, 0.7, 0.9, 1.0], unit_resolution=0.2)[source]#

Bases: FS

Class to define a gt2 fuzzy set.

MAX_RES_SUPPORT = 4#
__init__(name, secondary_memberships, domain, significant_decimals, alpha_cuts=[0.2, 0.4, 0.5, 0.7, 0.9, 1.0], unit_resolution=0.2)[source]#

Creates a GT2 fuzzy set.

Parameters:
  • name (str) – string.

  • secondary_memberships (dict[float, FS]) – list of fuzzy sets. Secondary membership that maps original domain to [0, 1]

  • secondMF_upper – four real numbers. Parameters of the upper trapezoid/gaussian function.

  • domain (list[float]) – list of two numbers. Limits of the domain if the fuzzy set.

  • alpha_cuts (list[float]) – list of real numbers. Alpha cuts of the fuzzy set.

  • unit_resolution (float) – real number. Resolution of the primary membership function.

membership(x)[source]#

Computes the alpha cut memberships of a point.

Parameters:

x (array) – input values in the fuzzy set referencial domain.

Returns:

np array samples x alpha_cuts x 2

Return type:

array

type()[source]#

Return the fuzzy set type identifier.

Returns:

The type identifier (FUZZY_SETS.t1 for Type-1 fuzzy sets)

Return type:

FUZZY_SETS

alpha_reduction(x)[source]#

Computes the type reduction to reduce the alpha cuts to one value.

Parameters:

x – array with the values of the inputs.

Returns:

array with the memberships of the consequents for each sample.

Return type:

array

class ex_fuzzy.fuzzy_sets.gaussianIVFS(name, secondMF_lower, secondMF_upper, domain, lower_height=1.0)[source]#

Bases: IVFS

Gaussian Interval-Valued (Type-2) Fuzzy Set Implementation.

This class implements a Gaussian interval-valued fuzzy set with two Gaussian membership functions (upper and lower) representing the uncertainty boundaries. This allows for modeling higher-order uncertainty in fuzzy systems.

secondMF_lower#

Parameters [mean, std] for the lower membership function

Type:

list

secondMF_upper#

Parameters [mean, std] for the upper membership function

Type:

list

name#

Human-readable name for the fuzzy set

Type:

str

universe_size#

Size of the universe of discourse

Type:

int

Example

>>> lower_params = [5.0, 1.0]
>>> upper_params = [5.0, 1.5]
>>> iv_gauss = gaussianIVFS(lower_params, upper_params, "Medium", 10)
>>> membership = iv_gauss.membership(np.array([4.0, 5.0, 6.0]))
>>> print(membership.shape)  # (3, 2) - lower and upper bounds

Note

The interval-valued membership function provides both lower and upper bounds for each input, enabling Type-2 fuzzy reasoning.

membership(input)[source]#

Computes the Gaussian interval-valued membership values for input points.

Returns both lower and upper membership bounds for each input value, enabling Type-2 fuzzy set operations.

Parameters:

input (np.array) – Input values in the fuzzy set domain

Returns:

Array of shape (n, 2) with [lower, upper] bounds for each input

Return type:

np.array

Example

>>> iv_gauss = gaussianIVFS([0, 1], [0, 1.5], "Zero", 5)
>>> values = iv_gauss.membership(np.array([0.0, 1.0]))
>>> print(values.shape)  # (2, 2) - 2 inputs, 2 bounds each
type()[source]#

Returns the type of the fuzzy set.

Returns:

Always returns FUZZY_SETS.t2 for Type-2 fuzzy sets

Return type:

FUZZY_SETS

shape()[source]#

Returns the shape of the fuzzy set.

Returns:

Always returns ‘gaussian’

Return type:

str

class ex_fuzzy.fuzzy_sets.gaussianFS(name, membership_parameters, domain)[source]#

Bases: FS

Gaussian Type-1 Fuzzy Set Implementation.

This class implements a Gaussian fuzzy set with bell-shaped membership function. Gaussian fuzzy sets are characterized by their mean and standard deviation, providing smooth membership transitions ideal for continuous variables.

membership_parameters#

[mean, standard_deviation] defining the Gaussian curve

Type:

list

name#

Human-readable name for the fuzzy set

Type:

str

universe_size#

Size of the universe of discourse

Type:

int

Example

>>> params = [5.0, 1.5]  # mean=5, std=1.5
>>> gauss_fs = gaussianFS(params, "Medium", 10)
>>> membership = gauss_fs.membership(np.array([4.0, 5.0, 6.0]))
>>> print(membership)  # [0.606, 1.0, 0.606]

Note

The membership function follows the formula: μ(x) = exp(-0.5 * ((x - mean) / std)^2)

membership(input)[source]#

Computes the Gaussian membership values for input points.

The membership function implements the Gaussian curve formula using the mean and standard deviation parameters.

Parameters:

input (np.array) – Input values in the fuzzy set domain

Returns:

Membership values in range [0, 1]

Return type:

np.array

Example

>>> gauss_fs = gaussianFS([0, 1], "Zero", 5)
>>> values = gauss_fs.membership(np.array([-1, 0, 1]))
>>> print(values)  # [0.606, 1.0, 0.606]
type()[source]#

Returns the type of the fuzzy set.

Returns:

Always returns FUZZY_SETS.t1 for Type-1 fuzzy sets

Return type:

FUZZY_SETS

shape()[source]#

Returns the shape of the fuzzy set.

Returns:

Always returns ‘gaussian’

Return type:

str

class ex_fuzzy.fuzzy_sets.fuzzyVariable(name, fuzzy_sets, units=None)[source]#

Bases: object

Fuzzy Variable Container and Management Class.

This class represents a fuzzy variable composed of multiple fuzzy sets (linguistic variables). It provides methods to compute memberships across all fuzzy sets and manage the linguistic terms of the variable.

linguistic_variables#

List of fuzzy sets that define the variable

Type:

list

name#

Name of the fuzzy variable

Type:

str

units#

Units of measurement (optional, for display purposes)

Type:

str

fs_type#

Type of fuzzy sets (t1 or t2)

Type:

FUZZY_SETS

Example

>>> # Create fuzzy sets for temperature
>>> low_temp = gaussianFS([15, 5], "Low", 100)
>>> medium_temp = gaussianFS([25, 5], "Medium", 100)
>>> high_temp = gaussianFS([35, 5], "High", 100)
>>>
>>> # Create fuzzy variable
>>> temp_var = fuzzyVariable("Temperature", [low_temp, medium_temp, high_temp], "°C")
>>> memberships = temp_var.membership([20, 25, 30])
>>> print(memberships.shape)  # (3, 3) - 3 inputs, 3 fuzzy sets

Note

All fuzzy sets in the variable must be of the same type (t1 or t2).

__init__(name, fuzzy_sets, units=None)[source]#

Creates a fuzzy variable with the specified fuzzy sets.

Parameters:
  • name (str) – Name of the fuzzy variable

  • fuzzy_sets (list[FS]) – List of fuzzy sets that comprise the linguistic variables

  • units (str, optional) – Units of the fuzzy variable for display purposes

Raises:

ValueError – If fuzzy_sets is empty or contains mixed types

Example

>>> fs1 = gaussianFS([0, 1], "Low", 10)
>>> fs2 = gaussianFS([5, 1], "High", 10)
>>> var = fuzzyVariable("Speed", [fs1, fs2], "m/s")
append(fs)[source]#

Appends a fuzzy set to the fuzzy variable.

Parameters:

fs (FS) – FS. Fuzzy set to append.

linguistic_variable_names()[source]#

Returns the name of the linguistic variables.

Returns:

list of strings.

Return type:

list

get_linguistic_variables()[source]#

Returns the name of the linguistic variables.

Returns:

list of strings.

Return type:

list[FS]

compute_memberships(x)[source]#

Computes the membership to each of the FS in the fuzzy variables.

Parameters:

x (array) – numeric value or array. Computes the membership to each of the FS in the fuzzy variables.

Returns:

list of floats. Membership to each of the FS in the fuzzy variables.

Return type:

list

domain()[source]#

Returns the domain of the fuzzy variable.

Returns:

list of floats.

Return type:

list[float]

fuzzy_type()[source]#

Returns the fuzzy type of the domain

Returns:

the type of the fuzzy set present in the fuzzy variable.

Return type:

FUZZY_SETS

validate(X, verbose=False)[source]#

Validates the fuzzy variable. Checks that all the fuzzy sets have the same type and domain.

Parameters:

X – np.array. Input data to validate the fuzzy variable.

Returns:

bool. True if the fuzzy variable is valid, False otherwise.

Return type:

bool

__str__()[source]#

Returns the name of the fuzzy variable, its type and its parameters.

Returns:

string.

Return type:

str

__getitem__(item)[source]#

Returns the corresponding fs.

Parameters:

item – int. Index of the FS.

Returns:

FS. The corresponding FS.

Return type:

FS

__setitem__(item, elem)[source]#

Sets the corresponding fs.

Parameters:
  • item (int) – int. Index of the FS.

  • elem (FS) – FS. The FS to set.

__iter__()[source]#

Returns the corresponding fs.

Parameters:

item – int. Index of the FS.

Returns:

FS. The corresponding FS.

Return type:

Generator[FS, None, None]

__len__()[source]#

Returns the number of linguistic variables.

Returns:

int. Number of linguistic variables.

Return type:

int

__call__(x)[source]#

Computes the membership to each of the FS in the fuzzy variables.

Parameters:

x (array) – numeric value or array.

Returns:

list of floats. Membership to each of the FS in the fuzzy variables.

Return type:

list