Skip to content

Commit

Permalink
adding pre-baked triangular and trapezoidal MF
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone Spolaor committed Sep 5, 2020
1 parent 7088f88 commit af7da7a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion simpful/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .simpful import FuzzySystem, LinguisticVariable, UndefinedUniverseOfDiscourseError
from .rule_parsing import Clause, Functional, OR, AND, AND_p, NOT, preparse, postparse, find_index_operator, curparse
from .fuzzy_sets import FuzzySet, MF_object, gaussian, Sigmoid_MF, InvSigmoid_MF, Gaussian_MF, InvGaussian_MF, DoubleGaussian_MF
from .fuzzy_sets import FuzzySet, MF_object, gaussian, Sigmoid_MF, InvSigmoid_MF, Gaussian_MF, InvGaussian_MF, DoubleGaussian_MF, Triangular_MF, Trapezoidal_MF
49 changes: 49 additions & 0 deletions simpful/fuzzy_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,55 @@ def __call__(self, x):
def gaussian(x, mu, sig):
return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))

class Triangular_MF(MF_object):

def __init__(self, a=0, b=0.5, c=1):
"""
Creates a triangular membership function.
Requires a <= b <= c.
"""
self._a = a
self._b = b
self._c = c

def _execute(self, x):
if x < self._b:
if self._a != self._b:
return (x-self._a) * (1/(self._b-self._a))
else:
return 1
else:
if self._b != self._c:
return 1 + (x-self._b) * (-1/(self._c-self._b))
else:
return 1

class Trapezoidal_MF(MF_object):

def __init__(self, a=0, b=0.25, c=0.75, d=1):
"""
Creates a trapezoidal membership function.
Requires a <= b <= c <= d.
"""
self._a = a
self._b = b
self._c = c
self._d = d

def _execute(self, x):
if x < self._b:
if self._a != self._b:
return (x-self._a) * (1/(self._b-self._a))
else:
return 1
elif x >= self._b and x <= self._c:
return 1
else:
if self._c != self._d:
return 1 + (x-self._c) * (-1/(self._d-self._c))
else:
return 1

class Sigmoid_MF(MF_object):

def __init__(self, c=0, a=1):
Expand Down
6 changes: 3 additions & 3 deletions simpful/simpful.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pylab import *
from .fuzzy_sets import FuzzySet, MF_object, Sigmoid_MF, InvSigmoid_MF, Gaussian_MF, InvGaussian_MF, DoubleGaussian_MF
from .fuzzy_sets import FuzzySet, MF_object, Sigmoid_MF, InvSigmoid_MF, Gaussian_MF, InvGaussian_MF, DoubleGaussian_MF, Triangular_MF, Trapezoidal_MF
from .rule_parsing import curparse, preparse, postparse
from numpy import array, linspace
from scipy.interpolate import interp1d
Expand Down Expand Up @@ -34,7 +34,7 @@ def __init__(self, FS_list=[], concept=None, universe_of_discourse=None):
Args:
FS_list: a list of FuzzySet instances.
concept: a brief description of the concept represented by the linguistic variable.
universe_of_discourse: A list of two elements, specifying min and max of universe of discourse.
universe_of_discourse: a list of two elements, specifying min and max of universe of discourse.
It must be specified to exploit plotting facilities.
"""

Expand Down Expand Up @@ -371,8 +371,8 @@ def Mamdani_inference(self, terms=None, ignore_errors=False, verbose=False, subd
Performs Mamdani fuzzy inference.
Args:
terms: list of the names of the variables on which inference must be performed.
subdivisions: the number of integration steps to be performed (default: 1000).
If empty, all variables appearing in the consequent of a fuzzy rule are inferred.
subdivisions: the number of integration steps to be performed (default: 1000).
ignore_errors: True/False, toggles the raising of errors during the inference.
verbose: True/False, toggles verbose mode.
Expand Down

0 comments on commit af7da7a

Please sign in to comment.