Some basic demonstration

Import helper modules and instantiate objects

Instance to create documentation and plots

In [1]:
from fuzzy.doc.plot.matplotlib.doc import Doc; doc = Doc()

Instance to read an FCL file

In [2]:
from fuzzy.storage.fcl.Reader import Reader; reader = Reader()

Read in example file

In [3]:
system = reader.load_from_file(".../examples/inverted_pendulum/src/inverted_pendulum.fcl")

List the contained input/output variables

In [4]:
{name:variable.__class__.__name__ for name,variable in system.variables.items()}
Out[4]:
{u'Phi': 'InputVariable',
 u'X': 'InputVariable',
 u'a': 'OutputVariable',
 u'dPhi_dT': 'InputVariable',
 u'dX_dT': 'InputVariable'}

Show their definitions

In [5]:
_len=len(system.variables.keys())
gcf().set_size_inches(gcf().get_size_inches()*[1,_len])
for i,name in enumerate(system.variables.keys()):
    doc.buildDocVariable(subplot(_len,1,i+1),system.variables[name], name)

Show the relation of Phi to a (while all other inputs are 0)

In [6]:
doc.build2DPlot(gca(),system, 'Phi', 'a')
/usr/lib/pymodules/python2.7/matplotlib/axes.py:4486: UserWarning: No labeled objects found. Use label='...' kwarg on individual plots.
  warnings.warn("No labeled objects found. "

Show the relation of Phi and dPhi_dT to a (while all other inputs are 0)

In [7]:
doc.build3DPlot(gca(projection="3d"),system, 'Phi', 'dPhi_dT', 'a', input_dict = { name:0.0 for name in system.variables.keys()})
In [8]:
#doc.build3DPlot_adjective(gca(projection="3d"),system, 'Phi', 'dPhi_dT', 'a', 'left_slow', input_dict = { name:0.0 for name in system.variables.keys()})

Define some dicts for following operations to use for inputs and outputs (and set some default values)

In [9]:
inputs = { name:0.0 for name in system.variables.keys()}; outputs = {'a':None}

Set input Phi to 33°.

In [10]:
inputs['Phi'] = 33

Calculate the result of the fuzzy system

In [11]:
system.calculate(inputs, outputs)
Out[11]:
{'a': 25.70673076923077}

The output dict contains now the results.

In [12]:
outputs
Out[12]:
{'a': 25.70673076923077}

Inspect the components of a which lead after defuzzyfication to the previous result.

In [13]:
{ name:adj.getMembership() for name,adj in system.variables["a"].adjectives.items() }
Out[13]:
{u'left_fast': 0.0,
 u'left_slow': 0.0,
 u'right_fast': 0.9,
 u'right_slow': 0.1,
 u'stop': 0.0}

Show how the previous values modified the sets of a

In [14]:
system.variables["a"].defuzzify.activated_sets
Out[14]:
{u'left_fast': fuzzy.set.Polygon.Polygon(points=[(-50.0, 0.0), (-20.0, 0.0), (-10.0, 0.0)]),
 u'left_slow': fuzzy.set.Polygon.Polygon(points=[(-20.0, 0.0), (-10.0, 0.0), (0.0, 0.0)]),
 u'right_fast': fuzzy.set.Polygon.Polygon(points=[(10.0, 0.0), (19.0, 0.9), (20.0, 0.9), (23.0, 0.9), (50.0, 0.0)]),
 u'right_slow': fuzzy.set.Polygon.Polygon(points=[(0.0, 0.0), (1.0, 0.1), (10.0, 0.1), (19.0, 0.09999999999999998), (20.0, 0.0)]),
 u'stop': fuzzy.set.Polygon.Polygon(points=[(-10.0, 0.0), (0.0, 0.0), (10.0, 0.0)])}

and here the grapical representation

In [15]:
doc.buildDocSets(gca(),system.variables["a"].defuzzify.activated_sets, "activated sets")

Now all these sets accumulated

In [16]:
system.variables["a"].defuzzify.accumulated_set
Out[16]:
fuzzy.set.Polygon.Polygon(points=[(-50.0, 0.0), (-20.0, 0.0), (-10.0, 0.0), (0.0, 0.0), (1.0, 0.1), (10.0, 0.1), (11.0, 0.1), (19.0, 0.9), (20.0, 0.9), (23.0, 0.9), (50.0, 0.0)])

and here the raphical representation

In [17]:
doc.buildDocSets(gca(),{"accumulated":system.variables["a"].defuzzify.accumulated_set}, "accumulated set")
In [17]: