In [9]:
%matplotlib inline
from fuzzle import lvars, mfs, operators, rules, controller, defuzz
from fuzzle.plot_utils import plot_lvar
from fuzzle.utils import frange
import matplotlib.pyplot as plt

We create two lingüistic variables, temperature and humidity. Those variables will contain five fuzzy set each.


In [2]:
# Temperature sensor (range 0 to 40)
temperature = lvars.InputLVar('temperature', (10, 40))
temperature['MB'] = mfs.LineDescMF(10, 15)
temperature['B'] = mfs.TriMF(10, 18, 20)
temperature['N'] = mfs.TriMF(18, 20, 25)
temperature['A'] = mfs.TriMF(20, 25, 30)
temperature['MA'] = mfs.LineAscMF(25, 30)

# Humidity sensor (range 0% to 100%)
humidity = lvars.InputLVar('humidity', (0, 100))
humidity['MB'] = mfs.LineDescMF(10, 20)
humidity['B'] = mfs.TriMF(10, 25, 40)
humidity['N'] = mfs.TriMF(30, 40, 50)
humidity['A'] = mfs.TriMF(40, 55, 70)
humidity['MA'] = mfs.LineAscMF(60, 70)

In [3]:
plot_lvar([temperature, humidity])


Creamos dos variables lingüísticas de salida (output1, output2, cada una con sus dos conjuntos difusos, fa y fb.


In [4]:
boiler = lvars.OutputLVar('boiler', domain=(-20, 20), defuzz=defuzz.CoG(resolution=5))
boiler['BG'] = mfs.TriMF(-15, -10, -7.5)
boiler['BN'] = mfs.TriMF(-10, -5, -2.5)
boiler['BP'] = mfs.TriMF(-7.5, -2.5, 0)
boiler['M'] = mfs.TriMF(-1, 0, 1)
boiler['SP'] = mfs.TriMF(0, 2.5, 7.5)
boiler['SN'] = mfs.TriMF(2.5, 5, 10)
boiler['SG'] = mfs.TriMF(7.5, 10, 15)

In [5]:
plot_lvar(boiler)


Por último, añadimos un bloque de reglas que relaciona las entradas con las salidas.


In [6]:
rb = rules.RuleBlock(
    and_op=operators.Minimum(),
    or_op=operators.Maximum(),
    not_op=operators.Zadeh(),
    agg_op=operators.Minimum(),
    acc_op=operators.Maximum()
)
rb[1] = 'if temperature is MB and humidity is MB then boiler is SN'
rb[2] = 'if temperature is MB and humidity is B then boiler is SN'
rb[3] = 'if temperature is MB and humidity is N then boiler is SG'
rb[4] = 'if temperature is MB and humidity is A then boiler is SG'
rb[5] = 'if temperature is MB and humidity is MA then boiler is SG'
rb[6] = 'if temperature is B and humidity is MB then boiler is M'
rb[7] = 'if temperature is B and humidity is B then boiler is M'
rb[8] = 'if temperature is B and humidity is N then boiler is SP'
rb[9] = 'if temperature is B and humidity is A then boiler is SP'
rb[10] = 'if temperature is B and humidity is MA then boiler is SN'
rb[11] = 'if temperature is N and humidity is MB then boiler is M'
rb[12] = 'if temperature is N and humidity is B then boiler is M'
rb[13] = 'if temperature is N and humidity is N then boiler is M'
rb[14] = 'if temperature is N and humidity is A then boiler is M'
rb[15] = 'if temperature is N and humidity is MA then boiler is BP'
rb[16] = 'if temperature is A and humidity is MB then boiler is M'
rb[17] = 'if temperature is A and humidity is B then boiler is M'
rb[18] = 'if temperature is A and humidity is N then boiler is BP'
rb[19] = 'if temperature is A and humidity is A then boiler is BP'
rb[20] = 'if temperature is A and humidity is MA then boiler is BN'
rb[21] = 'if temperature is MA and humidity is MB then boiler is BP'
rb[22] = 'if temperature is MA and humidity is B then boiler is BN'
rb[23] = 'if temperature is MA and humidity is N then boiler is BN'
rb[24] = 'if temperature is MA and humidity is A then boiler is BG'
rb[25] = 'if temperature is MA and humidity is MA then boiler is BG'

In [7]:
fc = controller.FuzzyController([temperature, humidity], [boiler], rb)

In [23]:
from mpl_toolkits.mplot3d import Axes3D

points = []
for x in frange(*reversed(temperature.domain)):
    for y in frange(*reversed(humidity.domain)):
        points.append((x, y, fc.eval({
            'temperature': x,
            'humidity': y,
        })['boiler']))

fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
ax.invert_yaxis()
ax.plot_trisurf(
    [p[0] for p in points],
    [p[1] for p in points],
    [p[2] for p in points],
    cmap=plt.cm.jet,
    linewidth=0.1
)


Out[23]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x7fc44f084b00>

In [ ]: