Skip to content

Variable

InputVariable

Bases: Variable

Variable to represent the input of a fuzzy logic controller.

related

Functions

__init__

__init__(
    name: str = "",
    description: str = "",
    enabled: bool = True,
    minimum: float = -inf,
    maximum: float = inf,
    lock_range: bool = False,
    terms: Iterable[Term] | None = None,
) -> None

Constructor.

Parameters:

Name Type Description Default
name str

name of the variable

''
description str

description of the variable

''
enabled bool

enable the variable

True
minimum float

minimum value of the variable

-inf
maximum float

maximum value of the variable

inf
lock_range bool

lock the value to the range of the variable

False
terms Iterable[Term] | None

list of terms.

None

__str__

__str__() -> str

Return the code to construct the input variable in the FuzzyLite Language.

Returns:

Type Description
str

code to construct the input variable in the FuzzyLite Language.

fuzzy_value

fuzzy_value() -> Array[str_]

Return the current fuzzy input value.

The fuzzy value is computed by evaluating the membership function of the current input value \(x\) for each term \(i\), resulting in a fuzzy input value in the form \(\tilde{x}=\sum_i{\mu_i(x)/i}\).

Returns:

Type Description
Array[str_]

current fuzzy value expressed as \(\sum_i{\mu_i(x)/i}\).

OutputVariable

Bases: Variable

Variable to represents the output of a fuzzy logic controller.

During the activation of a rule block, the activated terms of each rule are aggregated in the fuzzy output, which represents a fuzzy set hereinafter referred to as \(\tilde{y}\).

The defuzzification of \(\tilde{y}\) converts the fuzzy output value \(\tilde{y}\) into a crisp output value \(y\), which is stored as the value of this variable.

related

Attributes

aggregation property writable

aggregation: SNorm | None

Get/Set the aggregation operator.

Getter

Returns:

Type Description
SNorm | None

aggregation operator.

Setter

Parameters:

Name Type Description Default
value SNorm

aggregation operator

required

default_value instance-attribute

default_value = default_value

defuzzifier instance-attribute

defuzzifier = defuzzifier

fuzzy instance-attribute

fuzzy = Aggregated(name=name, minimum=minimum, maximum=maximum, aggregation=aggregation)

lock_previous instance-attribute

lock_previous = lock_previous

maximum property writable

maximum: float

Get/Set the maximum value of the range of the output variable.

Getter

Returns:

Type Description
float

maximum value of the range of the output variable.

Setter

Parameters:

Name Type Description Default
value float

maximum value of the output variable.

required

minimum property writable

minimum: float

Get/Set the minimum value of the range of the output variable.

Getter

Returns:

Type Description
float

minimum value of the range of the output variable.

Setter

Parameters:

Name Type Description Default
value float

minimum value of the output variable.

required

previous_value instance-attribute

previous_value = nan

Functions

__init__

__init__(
    name: str = "",
    description: str = "",
    enabled: bool = True,
    minimum: float = -inf,
    maximum: float = inf,
    lock_range: bool = False,
    lock_previous: bool = False,
    default_value: float = nan,
    aggregation: SNorm | None = None,
    defuzzifier: Defuzzifier | None = None,
    terms: Iterable[Term] | None = None,
) -> None

Constructor.

Parameters:

Name Type Description Default
name str

name of the variable

''
description str

description of the variable

''
enabled bool

enable the variable

True
minimum float

minimum value of the variable

-inf
maximum float

maximum value of the variable

inf
lock_range bool

lock the value to the range of the variable

False
lock_previous bool

lock the previous value of the output variable

False
default_value float

default value of the output variable

nan
aggregation SNorm | None

aggregation operator

None
defuzzifier Defuzzifier | None

defuzzifier of the output variable

None
terms Iterable[Term] | None

list of terms.

None

__repr__

__repr__() -> str

Return the code to construct the output variable in Python.

Returns:

Type Description
str

code to construct the output variable in Python.

__str__

__str__() -> str

Return the code to construct the output variable in the FuzzyLite Language.

Returns:

Type Description
str

code to construct the output variable in the FuzzyLite Language.

clear

clear() -> None

Clear the output variable.

defuzzify

defuzzify() -> None

Defuzzify the output variable and store the output value and the previous output value.

The final value \(y\) depends on the following cascade of properties in order of precedence expressed in the FuzzyLite Language:

  • lock-previous: boolean: when the output value is not finite (ie, nan or inf) and lock-previous: true, then the output value is replaced with the value defuzzified in the previous iteration.

  • default: scalar: when the output value is (still) not finite and the default value is not nan, then the output value is replaced with the default value.

  • lock-range: boolean: when lock-range: true, the output value is clipped to the range of the variable.

fuzzy_value

fuzzy_value() -> Array[str_]

Return the current fuzzy output value.

Returns:

Type Description
Array[str_]

current fuzzy output value.

Variable

Base class for linguistic variables.

related

Attributes

description instance-attribute

description = description

drange property

drange: float

Return the magnitude of the range of the variable.

Returns:

Type Description
float

maximum - minimum

enabled instance-attribute

enabled = enabled

lock_range instance-attribute

lock_range = lock_range

maximum instance-attribute

maximum = maximum

minimum instance-attribute

minimum = minimum

name instance-attribute

name = name

range property writable

range: tuple[float, float]

Return the range of the variable.

Getter

Returns:

Type Description
tuple[float, float]

tuple of (minimum, maximum).

Setter

Parameters:

Name Type Description Default
min_max tuple[float, float]

range of the variable

required

terms instance-attribute

terms = list(terms or [])

value property writable

value: Scalar

Get/Set the value of the variable.

Getter

Returns:

Type Description
Scalar

value of the variable

Setter

when lock_range = true, the value is clipped to the range of the variable

Parameters:

Name Type Description Default
value Scalar

value of the variable

required

Functions

__getitem__

__getitem__(item: int | str) -> Term
__getitem__(item: slice) -> list[Term]
__getitem__(item: int | str | slice) -> Term | list[Term]

Allow indexing terms by index, name, or slices (eg, engine["power"]["low"]).

Parameters:

Name Type Description Default
item int | str | slice

index, name, or slice of terms

required

Returns:

Type Description
Term | list[Term]

term by index or name, or slice of terms

__init__

__init__(
    name: str = "",
    description: str = "",
    enabled: bool = True,
    minimum: float = -inf,
    maximum: float = inf,
    lock_range: bool = False,
    terms: Iterable[Term] | None = None,
) -> None

Constructor.

Parameters:

Name Type Description Default
name str

name of the variable

''
description str

description of the variable

''
enabled bool

enable the variable

True
minimum float

minimum value of the range

-inf
maximum float

maximum value of the range

inf
lock_range bool

lock the value to the range of the variable

False
terms Iterable[Term] | None

list of terms

None

__iter__

__iter__() -> Iterator[Term]

Return the iterator of the terms.

Returns:

Type Description
Iterator[Term]

iterator of the terms

__len__

__len__() -> int

Return the number of terms.

Returns:

Type Description
int

number of terms

__repr__

__repr__() -> str

Return the code to construct the variable in Python.

Returns:

Type Description
str

code to construct the variable in Python.

__str__

__str__() -> str

Return the code to construct the variable in the FuzzyLite Language.

Returns:

Type Description
str

code to construct the variable in the FuzzyLite Language.

clear

clear() -> None

Clear the variable to its initial state.

fuzzify

fuzzify(x: Scalar) -> Array[str_]

Return the fuzzy representation of \(x\).

The fuzzy representation is computed by evaluating the membership function of \(x\) for each term \(i\), resulting in a fuzzy value in the form \(\tilde{x}=\sum_i{\mu_i(x)/i}\)

Parameters:

Name Type Description Default
x Scalar

value to fuzzify

required

Returns:

Type Description
Array[str_]

fuzzy value expressed as \(\sum_i{\mu_i(x)/i}\).

highest_membership

highest_membership(x: float) -> Activated | None

Return the term that has the highest membership function value for \(x\).

Parameters:

Name Type Description Default
x float

value

required

Returns:

Type Description
Activated | None

term \(i\) that maximimizes \(\mu_i(x)\)

term

term(name_or_index: str | int) -> Term

Find the term by the name or index.

The best performance is \(O(1)\) when using indices, and the worst performance is \(O(n)\) when using names, where \(n\) is the number terms.

Parameters:

Name Type Description Default
name_or_index str | int

name or index of the term

required

Returns:

Type Description
Term

term by the name or index

Raises:

Type Description
ValueError

when there is no term by the given name.

IndexError

when the index is out of range