fuzz.engine.Engine

class fuzz.engine.Engine(operands: fuzz.operators.OperatorEnum = OperatorEnum.DEFAULT, rule_agg: fuzz.operators.RuleAggregationEnum = RuleAggregationEnum.MAX, defuzz_method: fuzz.operators.DefuzzEnum = DefuzzEnum.LINGUISTIC)[source]

Bases: object

The Fuzzy Engine wraps the input kernels, rules and inference system to provide the fuzzyfy, defuzzyfy and generate surface methods.

Usage steps: 1: create engine

2: add input kernels (see Kernel for further reference)

3: add inference system (Kernel or Takagi-Sugeno based)

4: add rules to map the input kernels to the inference system

5: call engine.fuzzyfy() to run the system

6: call engine.defuzzyfy() to reduce the fuzzy result to a single float number

7: call engine.gen_surface() to build a iterable cache-like map to greatly reduce time compute

__init__(operands: fuzz.operators.OperatorEnum = OperatorEnum.DEFAULT, rule_agg: fuzz.operators.RuleAggregationEnum = RuleAggregationEnum.MAX, defuzz_method: fuzz.operators.DefuzzEnum = DefuzzEnum.LINGUISTIC) None[source]

Initializes a new engine object

Parameters

operands (OperandEnum, optional) – Operation definitions for AND, OR and NOT methods. Defaults to OperandEnum.DEFAULT.

Methods

__init__([operands, rule_agg, defuzz_method])

Initializes a new engine object

add_inference_kernel(kernel)

Adds a Kernel to map the inference system to its membership functions fuzzy output

add_kernel(name, kernel)

Adds a Kernel object, to map a particular variable of interest to its membership functions

add_rule(name, rule)

Add a declarative rule, mapping each input kernel membership values to the inference system membership functions.

del_inference_kernel()

Deletes the registered inference kernel, if there is one

del_kernel(name)

Deletes a registered input kernel

delete_rule(name)

Deletes a registered rule at self.ruleset

gen_surface(map_size, granularity)

Very expensive operation, if used with Linguistic Fuzzy Systems.

run_defuzz(measurements[, granularity])

Passing a dictionary of data and granularity (mandatory for Linguistic Defuzz) returns a crisp result of the inference system.

run_fuzz(measurements)

Passing a dictionary of data, runs the Engine and return a fuzzy output mapped to the inference system

add_inference_kernel(kernel: fuzz.kernel.Kernel)[source]

Adds a Kernel to map the inference system to its membership functions fuzzy output

Parameters

kernel (Kernel) – the kernel object mapping the inference system to membership functions

Raises

TypeError – if object type != type(Kernel)

Returns

self

Return type

Engine

add_kernel(name: str, kernel: fuzz.kernel.Kernel)[source]

Adds a Kernel object, to map a particular variable of interest to its membership functions

Parameters
  • name (str) – The name of the kernel. Various methods will call the kernel using this name as the key to a dict of type {name: kernel}

  • kernel (Kernel) – Kernel object, mapping the variable to its many membership functions

Raises

TypeError – if the internal dictionary self.input_kernel_set gets corrupted by direct user manipulation

Returns

self

Return type

Engine

add_rule(name: str, rule: fuzz.rules.RuleBase)[source]

Add a declarative rule, mapping each input kernel membership values to the inference system membership functions.

Parameters
  • name (str) – must match the name of a KernelFuncMember registered at the Inference Kernel System.

  • rule (Union[RuleBase, Dict[str, str]]) – a rule object (AND, OR, NOT), or a dictionary to get the direct value of a specific membership function.

Examples

    1. If FOOD is GOOD then tip is High (direct access): fm.add_rule(‘High’, {‘food’: ‘good’})

    1. if SERVICE is BAD AND FOOD is RANCID then tip is Low (rule base access): fm.add_rule(‘Low’, AND({‘service’: ‘bad’}, {‘food’: ‘rancid’}))

See More:
  • RuleBase documentation

Returns

self

Return type

Engine

del_inference_kernel()[source]

Deletes the registered inference kernel, if there is one

del_kernel(name: str) None[source]

Deletes a registered input kernel

Parameters

name (str) – the name of the registered kernel

Raises

KeyError – if name is not found in self.input_kernel_set.keys()

delete_rule(name: str)[source]

Deletes a registered rule at self.ruleset

Parameters

name (str) – the rule name

Raises

KeyError – if name is not in self.ruleset.keys()

gen_surface(map_size: int, granularity: float)[source]

Very expensive operation, if used with Linguistic Fuzzy Systems.

Parameters

granularity (Union[float, Dict[str, float]]) – [description]

Returns

[description]

Return type

[type]

run_defuzz(measurements: Dict[str, Any], granularity: Optional[float] = None) numpy.ndarray[source]

Passing a dictionary of data and granularity (mandatory for Linguistic Defuzz) returns a crisp result of the inference system.

Parameters
  • measurements (Dict[str, Any]) – Dictionary mapping each input kernel to its data

  • granularity (float) – iteration granularity. Required for Linguistic Defuzz only.

Raises

NotImplementedError – If Defuzz method is not implemented.

Returns

crisp values of the inference system.

Return type

np.ndarray

run_fuzz(measurements: Dict[str, Any]) Dict[str, numpy.ndarray][source]

Passing a dictionary of data, runs the Engine and return a fuzzy output mapped to the inference system

Parameters

measurements (Dict[str, Any]) – Dictionary mapping each input kernel to its data

Returns

the fuzzy output of the inference system.

Return type

Dict[str, np.ndarray]