Rules

class floulib.Rules(*args)

Bases: Plot

This class contains methods to define and use a set of rules.

Note

Rules is a subclass of Plot, therefore all methods in Plot may be used.

__init__(*args)

Constructor

Parameters:

*args (Rule) – Arguments are instances of Rule.

Raises:

TypeError – If arguments are not instances of Rule.

Return type:

None.

Example

>>> from floulib import Term, Triangle, Rule, Rules, Variable
>>> import numpy as np
>>> A1 = Term('A1', Triangle(0, 5, 10, label = '$A_1$'))
>>> A2 = Term('A2', Triangle(5, 10, 15, label = '$A_2$'))
>>> B1 = Term('B1', Triangle(0, 2, 4, label = '$B_1$'))
>>> B2 = Term('B2', Triangle(2, 4, 6, label = '$B_2$'))
>>> v1 = Variable(np.linspace(0,15, 1000))
>>> v2 = Variable(np.linspace(0, 10, 1000))
>>> R = Rules(
    Rule().If(v1.Is(A1)).Then(v2.Is(B1)),
    Rule().If(v1.Is(A2)).Then(v2.Is(B2))
    )

Support of the relation generated by the rules (conjunctive representation).

>>> R.plot(xlim = [-1, 16], ylim = [-1, 7])
_images/Rules.__init___1.png

Support of the relation generated by the rules (implicative representation).

>>> R.plot(xlim = [-1, 16], ylim = [-1, 7], implication = True)
_images/Rules.__init___2.png
inference(*args, **kwargs)

Computes the image of a fuzzy subsets by a set of rules.

Parameters:
Returns:

The result.

Return type:

Discrete

Example

>>> from floulib import Term, Triangle, Rule, Rules, Variable
>>> import numpy as np
>>> A1 = Term('A1', Triangle(0, 5, 10, label = '$A_1$'))
>>> A2 = Term('A2', Triangle(5, 10, 15, label = '$A_2$'))
>>> B1 = Term('B1', Triangle(0, 2, 4, label = '$B_1$'))
>>> B2 = Term('B2', Triangle(2, 4, 6, label = '$B_2$'))
>>> v1 = Variable(np.linspace(0,15, 1000))
>>> v2 = Variable(np.linspace(0, 10, 1000))
>>> R = Rules(
    Rule().If(v1.Is(A1)).Then(v2.Is(B1)),
    Rule().If(v1.Is(A2)).Then(v2.Is(B2))
    )
>>> A = Triangle(6, 8, 9)
>>> B = R.inference(v1.Is(A)).label('$B$')
>>> B.plot().add_plot(B1, alpha = 0.3).add_plot(B2, alpha = 0.3)
_images/Rules.inference.png
sugeno_controller(*args)

Computes the output of a Sugeno controller.

Parameters:

*args (Variable) – Arguments are instances of Variable.

Raises:
  • Exception – If the length of If parts of the rules and number of arguments are not the same.

  • Exception – If the If parts of the rules are instances of Multilinear but do not have universes associated with.

  • TypeError – If the If part of the rules are not instances of Variable.

  • TypeError – Raised if the Then part of the rules is not instance of Variable.

Returns:

The ouput of the Sugeno controller.

Return type:

float

Example

>>> from floulib import Term, Triangle, Rule, Rules, Variable
>>> import numpy as np
>>> A1 = Term('A1', Triangle(0, 5, 10, label = '$A_1$'))
>>> A2 = Term('A2', Triangle(5, 10, 15, label = '$A_2$'))
>>> v1 = Variable(np.linspace(0,15, 1000))
>>> v2 = Variable(np.linspace(0, 50, 1000))
>>> R = Rules(
    Rule().If(v1.Is(A1)).Then(v2.Is(lambda x: x-3)),
    Rule().If(v1.Is(A2)).Then(v2.Is(lambda x: 3*x + 5))
    )
>>> y = R.sugeno_controller(v1.Is(8))
>>> print(y)
19.4