Skip to content

Engine

Engine

Core class of the library that groups the necessary components of a fuzzy logic controller.

related

Attributes

description instance-attribute

description = description

input_values property writable

input_values: ScalarArray

Get/Set 2D array where columns represent input variables and rows their input values.

Getter

Returns:

Type Description
ScalarArray

2D array of input values (rows) for each input variable (columns)

Setter

Parameters:

Name Type Description Default
values ScalarArray

input values of the engine.

required
Tip
when values is a: the result:
single scalar value sets the values of all input variables
1D array on an engine with a single variable sets the values for the input variable
1D array on an engine with multiple variables sets each value to each input variable
2D array sets each column of values to each input variable

Raises:

Type Description
RuntimeError

when there are no input variables

ValueError

when the dimensionality of values is greater than 2

ValueError

when the number of columns in the values is different from the number of input variables

input_variables instance-attribute

input_variables = list(input_variables or [])

name instance-attribute

name = name

output_values property

output_values: ScalarArray

Return a 2D array of output values (rows) for each output variable (columns).

Returns:

Type Description
ScalarArray

2D array of output values (rows) for each output variable (columns).

output_variables instance-attribute

output_variables = list(output_variables or [])

rule_blocks instance-attribute

rule_blocks = list(rule_blocks or [])

values property

values: ScalarArray

Return a 2D array of current input and output values.

Returns:

Type Description
ScalarArray

2D array of current input and output values.

variables property

Return the list of input and output variables.

Returns:

Type Description
list[InputVariable | OutputVariable]

list of input and output variables.

Classes

Type

Bases: Enum

Determine type of engine.

Attributes
Hybrid class-attribute instance-attribute
Hybrid = auto()
InverseTsukamoto class-attribute instance-attribute
InverseTsukamoto = auto()
Larsen class-attribute instance-attribute
Larsen = auto()
Mamdani class-attribute instance-attribute
Mamdani = auto()
TakagiSugeno class-attribute instance-attribute
TakagiSugeno = auto()
Tsukamoto class-attribute instance-attribute
Tsukamoto = auto()
Unknown class-attribute instance-attribute
Unknown = auto()

Functions

__getitem__

__getitem__(item: str) -> InputVariable | OutputVariable | RuleBlock

Allow operation of engines as engine["power"].value.

Parameters:

Name Type Description Default
item str

name of the component to find in input variables, output variables, or rule blocks

required

Returns:

Type Description
InputVariable | OutputVariable | RuleBlock

first component found with the name

__init__

__init__(
    name: str = "",
    description: str = "",
    input_variables: Iterable[InputVariable] | None = None,
    output_variables: Iterable[OutputVariable] | None = None,
    rule_blocks: Iterable[RuleBlock] | None = None,
    load: bool = True,
) -> None

Constructor.

Parameters:

Name Type Description Default
name str

name of the engine

''
description str

description of the engine

''
input_variables Iterable[InputVariable] | None

list of input variables

None
output_variables Iterable[OutputVariable] | None

list of output variables

None
rule_blocks Iterable[RuleBlock] | None

list of rule blocks

None
load bool

whether to automatically update references to this engine and load the rules in the rule blocks.

True

__repr__

__repr__() -> str

Return the code to construct the engine in Python.

Returns:

Type Description
str

code to construct the engine in Python.

__str__

__str__() -> str

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

Returns:

Type Description
str

code to construct the engine in the FuzzyLite Language.

configure

configure(
    conjunction: TNorm | str | None = None,
    disjunction: SNorm | str | None = None,
    implication: TNorm | str | None = None,
    aggregation: SNorm | str | None = None,
    defuzzifier: Defuzzifier | str | None = None,
    activation: Activation | str | None = None,
) -> None

Configure the engine with the given operators.

Parameters:

Name Type Description Default
conjunction TNorm | str | None

object or name of TNorm registered in the TNormFactory

None
disjunction SNorm | str | None

object or name of SNorm registered in the SNormFactory

None
implication TNorm | str | None

object or name of TNorm registered in the TNormFactory

None
aggregation SNorm | str | None

object or name of SNorm registered in the SNormFactory

None
defuzzifier Defuzzifier | str | None

object or name of defuzzifier registered in the DefuzzifierFactory

None
activation Activation | str | None

object or name of activation method registered in the ActivationFactory

None

copy

copy() -> Engine

Create a deep copy of the engine.

Returns:

Type Description
Engine

deep copy of the engine

infer_type

infer_type(reasons: list[str] | None = None) -> Type

Infer the type of the engine based on its configuration.

Parameters:

Name Type Description Default
reasons list[str] | None

optional output list explaining the reasons for the inferred type

None

Returns:

Type Description
Type

type of engine inferred from its configuration.

input_variable

input_variable(name_or_index: str | int) -> InputVariable

Find the input variable 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 of input variables.

Parameters:

Name Type Description Default
name_or_index str | int

name or index of the input variable

required

Returns:

Type Description
InputVariable

input variable by the name or index

Raises:

Type Description
ValueError

when there is no variable by the given name.

IndexError

when the index is out of range

is_ready

is_ready(errors: list[str] | None = None) -> bool

Determine whether the engine is configured correctly and ready for operation.

Note

In advanced engines, the result of this method should be taken as a suggestion and not as a prerequisite to operate the engine.

Parameters:

Name Type Description Default
errors list[str] | None

optional output list that stores the errors found if the engine is not ready

None

Returns:

Type Description
bool

whether the engine is ready.

output_variable

output_variable(name_or_index: str | int) -> OutputVariable

Find the output variable of the given name or at the given 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 of output variables.

Parameters:

Name Type Description Default
name_or_index str | int

name or index of the output variable

required

Returns:

Type Description
OutputVariable

output variable by the given name or at the given index

Raises:

Type Description
ValueError

when there is no variable with the given name.

IndexError

when the index is out of range

process

process() -> None

Process the engine in its current state as follows.

  1. clear the aggregated fuzzy output variables,
  2. activate the rule blocks, and
  3. defuzzify the output variables
related

restart

restart() -> None

Restart the engine as follows.

  1. setting the values of the input variables to nan,
  2. reloading the rules of the rule blocks, and
  3. clearing the output variables
related

rule_block

rule_block(name_or_index: str | int) -> RuleBlock

Find the rule block of the given name or at the given 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 of rule blocks.

Parameters:

Name Type Description Default
name_or_index str | int

name or index of the rule block

required

Returns:

Type Description
RuleBlock

rule block by the given name or at the given index

Raises:

Type Description
ValueError

when there is no variable with the given name.

IndexError

when the index is out of range

variable

variable(name: str) -> Variable

Find the variable by the name, iterating first over the input variables and then over the output variables.

The cost of this method is \(O(n)\), where \(n\) is the number of variables in the engine. For better performance, get the variables by index.

Parameters:

Name Type Description Default
name str

name of the input or output variable

required

Returns:

Type Description
Variable

variable of the given name

Raises:

Type Description
ValueError

when there is no variable by the given name.