In [1]:
import sys
sys.path.append('../src')
import Pyfuzzy as fuzz
import numpy as np
import pylab as plt
from scipy.integrate import odeint 
%matplotlib inline
This is an example of a dynamic fuzzy model: A dynamic model with fuzzy models at the right hand side model: d(alge)/dt=alge_phosphor(alge,pf)+alge_daphnie(alge,daphnie)+licht_alge(alge,cyano)+flux_alge(alge,flux) d(daphnie)/dt=daphnie_alge(daphnie,alge) d(cyano)/dt=cyano_phosphor(cyano,pf)+cyano_flux(cyano,flux)+licht_cyano(cyano,alge) see: http://www.rroij.com/open-access/the-use-of-the-dynamic-fuzzy-method-in-ecosystemmodeling.pdf
load the fuzzy models and visualize it

In [2]:
alge_phosphor=fuzz.read_model('../dyn_fuzzy/alge_phosphor.fis') # describes the growth of the alga according to phosphorus
alge_phosphor.show_model()


Out[2]:
True

In [3]:
alge_daphnie=fuzz.read_model('../dyn_fuzzy/alge_daphnie.fis') # includes the growth of the daphnie using the alge
alge_daphnie.show_model()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-5ebe9c1d9c70> in <module>()
      1 alge_daphnie=fuzz.read_model('../dyn_fuzzy/alge_daphnie.fis') # includes the growth of the daphnie using the alge
----> 2 alge_daphnie.show_model()

/home/ralf/master/samt2/fuzzy/src/Pyfuzzy.so in Pyfuzzy.fuzzy.show_model (Pyfuzzy.c:32758)()

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in contour(*args, **kwargs)
   2764         ax.hold(hold)
   2765     try:
-> 2766         ret = ax.contour(*args, **kwargs)
   2767     finally:
   2768         ax.hold(washold)

/usr/lib/python2.7/dist-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1812                     warnings.warn(msg % (label_namer, func.__name__),
   1813                                   RuntimeWarning, stacklevel=2)
-> 1814             return func(ax, *args, **kwargs)
   1815         pre_doc = inner.__doc__
   1816         if pre_doc is None:

/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.pyc in contour(self, *args, **kwargs)
   5642             self.cla()
   5643         kwargs['filled'] = False
-> 5644         return mcontour.QuadContourSet(self, *args, **kwargs)
   5645     contour.__doc__ = mcontour.QuadContourSet.contour_doc
   5646 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
   1422         are described in QuadContourSet.contour_doc.
   1423         """
-> 1424         ContourSet.__init__(self, ax, *args, **kwargs)
   1425 
   1426     def _process_args(self, *args, **kwargs):

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
    861         self._transform = kwargs.get('transform', None)
    862 
--> 863         self._process_args(*args, **kwargs)
    864         self._process_levels()
    865 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _process_args(self, *args, **kwargs)
   1443                 self._corner_mask = mpl.rcParams['contour.corner_mask']
   1444 
-> 1445             x, y, z = self._contour_args(args, kwargs)
   1446 
   1447             _mask = ma.getmask(z)

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _contour_args(self, args, kwargs)
   1538             warnings.warn('Log scale: values of z <= 0 have been masked')
   1539             self.zmin = z.min()
-> 1540         self._contour_level_args(z, args)
   1541         return (x, y, z)
   1542 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _contour_level_args(self, z, args)
   1187                 warnings.warn("Contour levels are not increasing")
   1188             else:
-> 1189                 raise ValueError("Contour levels must be increasing")
   1190 
   1191     def _process_levels(self):

ValueError: Contour levels must be increasing

In [4]:
licht_alge=fuzz.read_model('../dyn_fuzzy/licht_alge.fis') # includes the shade from cyano
licht_alge.show_model()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-8c163d0ab2a4> in <module>()
      1 licht_alge=fuzz.read_model('../dyn_fuzzy/licht_alge.fis') # includes the shade from cyano
----> 2 licht_alge.show_model()

/home/ralf/master/samt2/fuzzy/src/Pyfuzzy.so in Pyfuzzy.fuzzy.show_model (Pyfuzzy.c:32758)()

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in contour(*args, **kwargs)
   2764         ax.hold(hold)
   2765     try:
-> 2766         ret = ax.contour(*args, **kwargs)
   2767     finally:
   2768         ax.hold(washold)

/usr/lib/python2.7/dist-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1812                     warnings.warn(msg % (label_namer, func.__name__),
   1813                                   RuntimeWarning, stacklevel=2)
-> 1814             return func(ax, *args, **kwargs)
   1815         pre_doc = inner.__doc__
   1816         if pre_doc is None:

/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.pyc in contour(self, *args, **kwargs)
   5642             self.cla()
   5643         kwargs['filled'] = False
-> 5644         return mcontour.QuadContourSet(self, *args, **kwargs)
   5645     contour.__doc__ = mcontour.QuadContourSet.contour_doc
   5646 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
   1422         are described in QuadContourSet.contour_doc.
   1423         """
-> 1424         ContourSet.__init__(self, ax, *args, **kwargs)
   1425 
   1426     def _process_args(self, *args, **kwargs):

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
    861         self._transform = kwargs.get('transform', None)
    862 
--> 863         self._process_args(*args, **kwargs)
    864         self._process_levels()
    865 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _process_args(self, *args, **kwargs)
   1443                 self._corner_mask = mpl.rcParams['contour.corner_mask']
   1444 
-> 1445             x, y, z = self._contour_args(args, kwargs)
   1446 
   1447             _mask = ma.getmask(z)

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _contour_args(self, args, kwargs)
   1538             warnings.warn('Log scale: values of z <= 0 have been masked')
   1539             self.zmin = z.min()
-> 1540         self._contour_level_args(z, args)
   1541         return (x, y, z)
   1542 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _contour_level_args(self, z, args)
   1187                 warnings.warn("Contour levels are not increasing")
   1188             else:
-> 1189                 raise ValueError("Contour levels must be increasing")
   1190 
   1191     def _process_levels(self):

ValueError: Contour levels must be increasing

In [5]:
flux_alge=fuzz.read_model('../dyn_fuzzy/flux_alge.fis')  # includes the dilution
flux_alge.show_model()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-d270eabe960e> in <module>()
      1 flux_alge=fuzz.read_model('../dyn_fuzzy/flux_alge.fis')  # includes the dilution
----> 2 flux_alge.show_model()

/home/ralf/master/samt2/fuzzy/src/Pyfuzzy.so in Pyfuzzy.fuzzy.show_model (Pyfuzzy.c:32758)()

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in contour(*args, **kwargs)
   2764         ax.hold(hold)
   2765     try:
-> 2766         ret = ax.contour(*args, **kwargs)
   2767     finally:
   2768         ax.hold(washold)

/usr/lib/python2.7/dist-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1812                     warnings.warn(msg % (label_namer, func.__name__),
   1813                                   RuntimeWarning, stacklevel=2)
-> 1814             return func(ax, *args, **kwargs)
   1815         pre_doc = inner.__doc__
   1816         if pre_doc is None:

/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.pyc in contour(self, *args, **kwargs)
   5642             self.cla()
   5643         kwargs['filled'] = False
-> 5644         return mcontour.QuadContourSet(self, *args, **kwargs)
   5645     contour.__doc__ = mcontour.QuadContourSet.contour_doc
   5646 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
   1422         are described in QuadContourSet.contour_doc.
   1423         """
-> 1424         ContourSet.__init__(self, ax, *args, **kwargs)
   1425 
   1426     def _process_args(self, *args, **kwargs):

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
    861         self._transform = kwargs.get('transform', None)
    862 
--> 863         self._process_args(*args, **kwargs)
    864         self._process_levels()
    865 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _process_args(self, *args, **kwargs)
   1443                 self._corner_mask = mpl.rcParams['contour.corner_mask']
   1444 
-> 1445             x, y, z = self._contour_args(args, kwargs)
   1446 
   1447             _mask = ma.getmask(z)

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _contour_args(self, args, kwargs)
   1538             warnings.warn('Log scale: values of z <= 0 have been masked')
   1539             self.zmin = z.min()
-> 1540         self._contour_level_args(z, args)
   1541         return (x, y, z)
   1542 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _contour_level_args(self, z, args)
   1187                 warnings.warn("Contour levels are not increasing")
   1188             else:
-> 1189                 raise ValueError("Contour levels must be increasing")
   1190 
   1191     def _process_levels(self):

ValueError: Contour levels must be increasing

In [6]:
daphnie_alge=fuzz.read_model('../dyn_fuzzy/daphnie_alge.fis') # daphnie alge to model the growth of the daphnie
daphnie_alge.show_model()


Out[6]:
True

In [7]:
cyano_phosphor=fuzz.read_model('../dyn_fuzzy/cyano_phosphor.fis') # show the influence of phosphorus to the cyano
cyano_phosphor.show_model()


Out[7]:
True

In [8]:
cyano_flux=fuzz.read_model('../dyn_fuzzy/flux_cyano.fis') # influence of the flux to cyano
cyano_flux.show_model()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-d174e39a0b40> in <module>()
      1 cyano_flux=fuzz.read_model('../dyn_fuzzy/flux_cyano.fis') # influence of the flux to cyano
----> 2 cyano_flux.show_model()

/home/ralf/master/samt2/fuzzy/src/Pyfuzzy.so in Pyfuzzy.fuzzy.show_model (Pyfuzzy.c:32758)()

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in contour(*args, **kwargs)
   2764         ax.hold(hold)
   2765     try:
-> 2766         ret = ax.contour(*args, **kwargs)
   2767     finally:
   2768         ax.hold(washold)

/usr/lib/python2.7/dist-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1812                     warnings.warn(msg % (label_namer, func.__name__),
   1813                                   RuntimeWarning, stacklevel=2)
-> 1814             return func(ax, *args, **kwargs)
   1815         pre_doc = inner.__doc__
   1816         if pre_doc is None:

/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.pyc in contour(self, *args, **kwargs)
   5642             self.cla()
   5643         kwargs['filled'] = False
-> 5644         return mcontour.QuadContourSet(self, *args, **kwargs)
   5645     contour.__doc__ = mcontour.QuadContourSet.contour_doc
   5646 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
   1422         are described in QuadContourSet.contour_doc.
   1423         """
-> 1424         ContourSet.__init__(self, ax, *args, **kwargs)
   1425 
   1426     def _process_args(self, *args, **kwargs):

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
    861         self._transform = kwargs.get('transform', None)
    862 
--> 863         self._process_args(*args, **kwargs)
    864         self._process_levels()
    865 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _process_args(self, *args, **kwargs)
   1443                 self._corner_mask = mpl.rcParams['contour.corner_mask']
   1444 
-> 1445             x, y, z = self._contour_args(args, kwargs)
   1446 
   1447             _mask = ma.getmask(z)

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _contour_args(self, args, kwargs)
   1538             warnings.warn('Log scale: values of z <= 0 have been masked')
   1539             self.zmin = z.min()
-> 1540         self._contour_level_args(z, args)
   1541         return (x, y, z)
   1542 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _contour_level_args(self, z, args)
   1187                 warnings.warn("Contour levels are not increasing")
   1188             else:
-> 1189                 raise ValueError("Contour levels must be increasing")
   1190 
   1191     def _process_levels(self):

ValueError: Contour levels must be increasing

In [9]:
licht_cyano=fuzz.read_model('../dyn_fuzzy/licht_cyano.fis') # the influence of light to the cyano
licht_cyano.show_model()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-958e06ef4637> in <module>()
      1 licht_cyano=fuzz.read_model('../dyn_fuzzy/licht_cyano.fis') # the influence of light to the cyano
----> 2 licht_cyano.show_model()

/home/ralf/master/samt2/fuzzy/src/Pyfuzzy.so in Pyfuzzy.fuzzy.show_model (Pyfuzzy.c:32758)()

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in contour(*args, **kwargs)
   2764         ax.hold(hold)
   2765     try:
-> 2766         ret = ax.contour(*args, **kwargs)
   2767     finally:
   2768         ax.hold(washold)

/usr/lib/python2.7/dist-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1812                     warnings.warn(msg % (label_namer, func.__name__),
   1813                                   RuntimeWarning, stacklevel=2)
-> 1814             return func(ax, *args, **kwargs)
   1815         pre_doc = inner.__doc__
   1816         if pre_doc is None:

/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.pyc in contour(self, *args, **kwargs)
   5642             self.cla()
   5643         kwargs['filled'] = False
-> 5644         return mcontour.QuadContourSet(self, *args, **kwargs)
   5645     contour.__doc__ = mcontour.QuadContourSet.contour_doc
   5646 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
   1422         are described in QuadContourSet.contour_doc.
   1423         """
-> 1424         ContourSet.__init__(self, ax, *args, **kwargs)
   1425 
   1426     def _process_args(self, *args, **kwargs):

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
    861         self._transform = kwargs.get('transform', None)
    862 
--> 863         self._process_args(*args, **kwargs)
    864         self._process_levels()
    865 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _process_args(self, *args, **kwargs)
   1443                 self._corner_mask = mpl.rcParams['contour.corner_mask']
   1444 
-> 1445             x, y, z = self._contour_args(args, kwargs)
   1446 
   1447             _mask = ma.getmask(z)

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _contour_args(self, args, kwargs)
   1538             warnings.warn('Log scale: values of z <= 0 have been masked')
   1539             self.zmin = z.min()
-> 1540         self._contour_level_args(z, args)
   1541         return (x, y, z)
   1542 

/usr/lib/python2.7/dist-packages/matplotlib/contour.pyc in _contour_level_args(self, z, args)
   1187                 warnings.warn("Contour levels are not increasing")
   1188             else:
-> 1189                 raise ValueError("Contour levels must be increasing")
   1190 
   1191     def _process_levels(self):

ValueError: Contour levels must be increasing
Define the dynamic fuzzy model

In [11]:
pfx=[]
def rhs(y, t, P,flux):
    global daphnie_alge,alge_p,cyano_p,cyano_f,pfx
    Alge=y[0]
    Daphnie=y[1]
    Cyano=y[2]
    pgA=0.1   # 0.01 g/g Phosphatgehalt Alge
    pgC=0.02  # 0.002 g/g Phosphatgehalt Cyano
    a=2.0
    pf=P-(pgA*Alge+pgC*Cyano)
    Alge1=a*alge_phosphor.calc2(Alge,pf)+a*alge_daphnie.calc2(Alge,Daphnie)+\
        a*flux_alge.calc2(Alge,flux)+a*licht_alge.calc2(Alge,Cyano)
    Daphnie1=a*daphnie_alge.calc2(Daphnie,Alge)
    Cyano1=a*cyano_phosphor.calc2(Cyano,pf)+a*cyano_flux.calc2(Cyano,flux)+\
        a*licht_cyano.calc2(Cyano,Alge)
    pfx.append(pf)
    return [Alge1,Daphnie1,Cyano1]

In [12]:
P=0.18      # change this
flux=0.0    # change this
dt=1.0
niter=300
# init for iteration
# init for state variables
Alge0=0.03      # 0.3 mg/l
Cyano0=0.01    # 00.1 mg/l
Daphnie0=0.01  # 0.01 mg/l
y = [Alge0,Daphnie0,Cyano0]       # initial condition vector
t  = np.arange(0,niter,dt)   # time grid
# solve the DEs
soln = odeint(rhs, y, t, args=(P,flux))
plt.plot(t,soln[:,0],label='alge')
plt.plot(t,soln[:,1],label='daphnie')
plt.plot(t,soln[:,2],label='cyano')
plt.legend()
s='P: ' + str(P) + ' flux: ' + str(flux)
plt.title(s)
plt.xlabel('t')
plt.ylabel('Y')
plt.grid(True)
plt.show()



In [11]: