Simpfu
Simpfu
Research Article
Simpful: A User-Friendly Python Library for Fuzzy Logic
Simone Spolaor1, , Caro Fuchs2, , Paolo Cazzaniga3,4,5, , Uzay Kaymak2, , Daniela Besozzi1,4,5, , Marco S. Nobile2,4,5,*,
1
Department of Informatics, Systems and Communication, University of Milano-Bicocca, Milan, Italy
2
School of Industrial Engineering, Eindhoven University of Technology, Eindhoven, The Netherlands
3
Department of Human and Social Sciences, University of Bergamo, Bergamo, Italy
4
SYSBIO/ISBE.IT Centre of Systems Biology, Milan, Italy
5
Bicocca Bioinformatics, Biostatistics and Bioimaging Centre (B4), Milan, Italy
Table 2 Features supported by the available software for the design of FISs.
3. SOFTWARE DESCRIPTION
Simpful is implemented in the Python 3 programming language Figure 1 Example of derivation tree produced by Simpful
[38]. Its dependencies are numpy [39] and scipy [33]. The latest ver- while parsing a fuzzy rule.
sion of Simpful currently supports the following features:
• In the second case, the user creates a FuzzySet object by passing • 0-order functions (i.e., constant functions) are defined as
a function pointer, using the argument function. This custom “output crisp values.”
function should be in the form f: 𝕌 → [0, 1], 𝕌 ⊆ ℝ, mapping
• For higher order Takagi–Sugeno systems, the user can define
every element of the universe of discourse to a valid
“output functions” as strings of text involving the linguistic
membership value. In case the codomain of the custom
variables. These functions are evaluated at runtime by
function f provided by the user is not equal to the expected
exploiting the eval() function of Python, which parses the
interval, Simpful automatically clamps it to [0, 1]. Simpful also
expression given as a string argument and executes it as a code
provides a set of objects with preimplemented general-purpose
within the program.
parametric functions, namely: Gaussian (Gaussian_MF),
inverted Gaussian (InvGaussian_MF), double Gaussian
Output crisp values, output functions, and output fuzzy sets must
(DoubleGaussian_MF), sigmoid (Sigmoid_MF), inverted
have an associated meaningful and unique string to identify them,
sigmoid (InvSigmoid_MF), triangular (Triangular_MF) and
which will be exploited in the definition of the fuzzy rules.
trapezoidal (Trapezoidal_MF). It is worth noting that all these
objects are derived from the same abstract class, named Linguistic variables, fuzzy rules, output crisp values, output func-
MF_object: by providing an implementation of the virtual tions, and output fuzzy sets are added to a fuzzy system object,
method _execute(), users can straightforwardly create which implements the whole FIS. Given the input values for the
arbitrarily complex fuzzy sets exploiting any custom function. variables appearing in the antecedents of the fuzzy rules, the meth-
In this case, the new method must accept an argument x ∈ 𝕌 ods implementing the Mamdani or Takagi–Sugeno inference can be
and must return a valid membership value. called for one or more of the variables appearing in the consequent
of fuzzy rules, in order to obtain their final output. As an alterna-
The user is also required to associate a meaningful linguistic term tive, the user can invoke the inference() method of the FuzzySys-
with each fuzzy set. The defined fuzzy sets are then employed in the tem class to let Simpful choose and use the most appropriate
creation of specific LinguisticVariable objects, provided with their inference method (i.e., Mamdani or Takagi–Sugeno). As a matter
own names, as given by the user. If the fuzzy sets are specified by of fact, during the initialization phase, Simpful analyzes the outputs
means of a sequence of points only, Simpful automatically identifies of the model and automatically determines the class of the defined
the boundaries of the universe of discourse by exploiting the min- FIS. The results of the inference are returned to the user as key-value
imum and the maximum value among the first coordinates of all pairs inside a dictionary, where keys represent the names of the vari-
the points that define all the fuzzy sets. On the contrary, if a fuzzy ables. Figure 2 shows a schematic overview of how to construct a
set is defined by means of custom functions, the user can specify fuzzy system object and how to perform inference in Simpful.
a valid interval of values for the LinguisticVariable object, by using
The source code of Simpful is available, under GPL license, on
the optional argument universe_of_discourse. The definition of this
GitHub at the following URL: https://github.com/aresio/simpful.
interval is required in order to use the draw() method of the Lin-
Simpful can be installed by using the PyPI facility: pip install simp-
guisticVariable class, which can be exploited to plot the fuzzy sets,
ful. The example code described in this work can be found on
thus allowing for a rapid inspection and debugging of their imple-
Code Ocean at the following URL: https://codeocean.com/capsule/
mentation. Worthy of note, both point- and function-based fuzzy
2230971/tree.
sets can be exploited simultaneously in the definition of a single lin-
guistic variable. In order to facilitate its usage, Simpful also provides
a AutoTriangle() class, which returns a LinguisticVariable object 4. ILLUSTRATIVE EXAMPLES
whose universe of discourse is subdivided in a user-defined num-
ber of normalized triangular fuzzy sets. In this section we provide three examples, together with their cor-
responding Python code, to show the potential and the usage of
The fuzzy rules used for the inference must be defined by means Simpful.
of well-formed strings, written in natural language (Figure 1).
The current version of Simpful supports the most common fuzzy
4.1. Tipping Problem
operators AND, OR, NOT, defined as
The tipping problem consists in computing a fair tip (in terms of
• NOT x = 1 − x. percentage of the overall bill), taking into account a restaurant’s ser-
• x OR y = max(x, y). vices. Listings 1 and 2 show two examples of Simpful code to define
a FIS that calculates the tipping amount on the basis of two input
• x AND y = min(x, y). variables, describing food and serving staff quality.
In Listing 1 the tipping problem is modeled as a Mamdani FIS. In
Each rule must use the variables’ names and linguistic terms that
line 5 a fuzzy system object is created. The fuzzy sets and the linguis-
were defined in the LinguisticVariable objects.
tic variable “Service” are defined in lines 8 to 11; this variable con-
In the case of Takagi–Sugeno systems, the consequent of rules tains three fuzzy sets, “poor,” “good,” and “excellent,” ranging from
must also use strings that are associated with the output crisp val- 0 to 10. From line 13 to 15 the linguistic variable for food quality is
ues or with the output functions defined by the user. In this case, defined, exploiting two fuzzy sets, “rancid” and “delicious.” The out-
the user has to define the functions exploited in the inference as put variable “Tip” and its fuzzy sets are defined from line 18 to 21.
follows: All fuzzy sets used in this example are triangular (hence the use of
S. Spolaor et al. / International Journal of Computational Intelligence Systems 13(1) 1687–1698 1691
the preimplemented function Triangular_MF), except for the fuzzy “Food” quality scored 4 and 8 points, the tipping percentage should
set that denotes a “Generous” tip, which is an example of a trape- be 14.77% according to this example.
zoidal set (Trapezoidal_MF). The resulting membership functions
Finally, Figure 4 shows a comparison of the output surfaces pro-
are visualized in Figure 3. The fuzzy rules are defined in lines 24 to
duced by simpful and Scikit-Fuzzy. The response of the two libraries
27. Once the input values are set (lines 30 and 31, where “Service”
is identical, confirming the correctness of Simpful’s fuzzy inference.
and “Food” quality scored 4 and 8 points, respectively), Mamdani
fuzzy inference is performed (line 34) to obtain the final tipping
percentage, which is equal to 14.17% in this example. 4.2. Clinical Decision Support for Sepsis
Listing 2 shows the definition of another FIS to solve the tipping
When a patient enters the intensive care unit (ICU) with symp-
problem, this time using a Takagi–Sugeno model. Again, the fuzzy
toms of sepsis, clinicians must diagnose quickly and start treatment
system object is created in line 5. From line 8 to 15 the fuzzy sets
within an hour of admission. Clinical decision support systems aim
for the input variables are defined. In this example, the fuzzy sets
at helping clinicians with these decisions by processing patient data,
are defined as polygons, using an ordered list of points, instead of
such as blood levels and symptoms, and giving suggestions for diag-
exploiting a pre-implemented membership function as in the pre-
nosis or treatment plans.
vious example. However, the resulting fuzzy sets are the same as the
sets used in the Mamdani FIS. The output crisp values for a “small” In Listing 3, we provide a simplified clinical decision support model
and “average” tip are set to 5% and 15% respectively in line 18 and that calculates how likely it is the patient suffers from sepsis. First,
19. The output value for a “generous” tip is a function (defined in the fuzzy system object is created in line 5. The fuzzy sets and their
line 22) depending on the scores for service and food quality. In linguistic terms for the input variables are specified in lines 8 to 31.
this example this is a linear function, but any arbitrary function can In this example, the fuzzy sets are defined by using sigmoidal and
be handled by Simpful. The fuzzy rules are then defined in lines Gaussian functions. Since these functions have 𝕌 = ℝ as domain,
25 to 28. The input values are set in line 31 and 32, and Takagi– it is not possible to automatically estimate the limits of the universe
Sugeno inference is performed in line 35. Given that “Service” and of discourse for plotting the membership functions with the draw()
1692 S. Spolaor et al. / International Journal of Computational Intelligence Systems 13(1) 1687–1698
Figure 4 Comparison of output surfaces obtained on the Mamdani fuzzy inference system (FIS) for the tipping problem, implemented in
Simpful (left) and Scikit-Fuzzy (right): the output of the two libraries is the same.
method. Therefore, the user has to explicitly set the universe of dis- of a set of linguistic variables describing the components of the sys-
course by specifying the argument universe_of_discourse (lines 10, tem, and a set of fuzzy rules providing a qualitative description of
16, 23 and 30). Please note that this (explicitly limited) universe of their interactions. A DFM can be considered as a FN [27], i.e., a net-
discourse is only used to plot the membership functions: the system work of interacting FISs. Thus, a FN can be depicted as a directed
still supports input values outside this range when inferring the out- graph (as in Figure 6), where nodes represent linguistic variables,
put. In line 12, the plots for the membership functions of the first and arcs the presence of some fuzzy rules governing them.
variable are generated (Figure 5).
The example code of the repressilator is given in Listing 4. In line 6 a
In lines 15 to 17, the variable “base excess” is created by first speci- fuzzy system object is created. From line 9 to 12, the three linguistic
fying the fuzzy set describing physiological values (using the preim- variables related to the three species constituting the repressilator
plemented Gaussian_MF function). This is the only fuzzy set for are defined. All three species are characterized by a universe of dis-
this linguistic variable, meaning that the fuzzy set for the abnor- course ranging from 0 to 1 (the default universe of discourse), and
mal values for the base excess is not explicitly modeled, but corre- by the presence of two fuzzy sets, “low” and “high,” representing the
sponds to the complement of the fuzzy set for normal values (note quantity of each protein. Note that here the AutoTriangle() class is
the NOT operator in second fuzzy rule, line 40). The same results used (line 9) in order to define a general linguistic variable charac-
could be achieved by modeling the set of non-normal values explic- terized by 2 symmetrical fuzzy sets covering the whole universe of
itly, either using the InvGaussian_MF function, or splitting the uni- discourse. In this example, a value of 1 in the universe of discourse
verse of discourse in a low, medium, and high fuzzy set, and later corresponds to the maximum quantity, while 0 to the absence of the
connecting the low and high fuzzy set in the fuzzy rule through an protein. Analogously, the output crisp values are defined in lines 15
OR operator. However, using the NOT operator simplifies the FIS and 16, by setting “low” to 0 and “high” to 1. Lines 19 to 26 contain
and preserves its high levels of interpretability. The output crisp val- the definition of the fuzzy rules, representing the negative feedbacks
ues “low_probability” and “high_probability,” referring to the prob- existing between the three genes.
ability that the patient is suffering from sepsis, are defined in lines
The initial state of the DFM is set in lines 29 to 31, the number
35 and 36. The fuzzy rules are then defined and added to the fuzzy
of simulation steps is defined in line 34, while the data structure
system object in lines 39 to 43. Lines 46 to 49 provide example input
containing the results of the simulation is initialized in lines 35 and
values to the model, and fuzzy inference is performed in line 52 to
36. Lines 39 to 42 contain the for loop in which the simulation is
obtain the probability that the patient is suffering from sepsis which,
performed. In particular, the new state of the system is inferred in
in this example, is equal to 69.3%.
line 40, updated in line 41, and then stored in the previously defined
data structure. Note that, in this example, we exploit the inference()
4.3. Repressilator method provided by the FuzzySystem object (see line 40).
The repressilator is a synthetic regulatory network consisting of The final output, i.e., the simulation of the system’s dynamics, can
three genes placed in a feedback loop, where the genetic product of be plotted as shown in Figure 7. Despite its simplicity and the lack
each gene inhibits the expression of the next gene in the network of a precise kinetic parameterization, this model can reproduce the
(Figure 6). This simple system was designed to exhibit a stable oscil- typical oscillatory dynamics of the three species, as shown in the
latory regime, studied by means of mechanistic modeling, and then original model [42].
implemented in vivo in the bacterium E. coli [42]. Here, we provide
a simple redefinition of the repressilator in terms of a DFM, to show
how Simpful can also be applied for the fuzzy modeling of complex 5. CONCLUSIONS
systems.
Simpful is a novel library that addresses the need of having a
DFM is a formalism useful to analyze the emergent behavior of lightweight, open-source, Python API to support the creation of
complex systems characterized by uncertainty [25]. A DFM consists readable FISs, based on either Mamdani or Takagi–Sugeno fuzzy
S. Spolaor et al. / International Journal of Computational Intelligence Systems 13(1) 1687–1698 1695
Figure 5 The membership functions for the clinical decision support system to diagnose sepsis at the intensive care unit
(ICU).
reasoning. Users can define fuzzy sets as polygons (formalized as automatically from data [44–47]. Moreover, Simpful can be read-
sequences of vertices), parametric functions (e.g., Gaussian and sig- ily integrated in computational intelligence methods that use Mam-
moid), or even arbitrary custom functions in the universe of dis- dani or Takagi–Sugeno inference, such as the class of global opti-
course/degree of membership space. Fuzzy rules are encoded as mization meta-heuristics exploiting fuzzy reasoning for dynamic
strings of text written in natural language, making FISs created parameter adaptation [48,49].
in Simpful easier to read and to inspect compared to competitor
In future releases, we plan to extend Simpful with support for addi-
libraries. To show its usage, we provided three examples: the defi-
tional fuzzy logic operators and other fuzzy inference methods (e.g.,
nition of a FIS for the tipping problem, the definition of a clinical
Tsukamoto [50], and AnYa [51] methods). In particular, we will
decision support system to diagnose sepsis, and the modeling and
add support for weighted fuzzy rules [52], type-2 FISs [53], and
simulation of a complex biochemical system by means of a DFM.
probabilistic fuzzy reasoning [54], the latter providing a means to
Thanks to its features, Simpful is a valuable addition to the open-
combine the interpretability of FIS with the statistical properties of
source software that support fuzzy reasoning, and it is expected to
probabilistic systems. Finally, Simpful will support the FML for-
highly facilitate the definition, analysis and interpretation of FISs in
mat defined in the IEEE Std 1855-2016 [35], possibly by leveraging
a wide variety of data- and knowledge-driven applications.
existing software (i.e., JFML and Py4JFML [17,24]), to facilitate the
Simpful was employed to implement the FuzzX framework for import, export, and sharing of the FISs defined within this library.
the modeling and simulation of hybrid (qualitative and quanti-
tative) systems [26]. The porting of FUMOSO [25] to Simpful,
currently in progress, will also promote the use of DFMs for the CONFLICTS OF INTEREST
investigation of complex systems. Simpful is also employed within
pyFUME [43], a novel Python package developed to estimate FISs The authors declare no conflicts of interest.
1696 S. Spolaor et al. / International Journal of Computational Intelligence Systems 13(1) 1687–1698
AUTHORS’ CONTRIBUTIONS
MSN conceived the idea of the library; SS and MSN designed and
implemented the library; SS and CF conceived the usage examples,
analyzed the results and performed comparisons with the other
methods; SS, CF and MSN prepared and created the figures and
wrote the first draft of the manuscript; PC, UK and DB critically
reviewed and edited the manuscript; All authors read and approved
its final version.
Funding Statement
This work was partially funded by the SYSBIO/ISBE.IT Research
Centre of Systems Biology.
Figure 7 Dynamics of the variables of the dynamic fuzzy
model (DFM) representing the repressilator.
S. Spolaor et al. / International Journal of Computational Intelligence Systems 13(1) 1687–1698 1697
[42] M.B. Elowitz, S. Leibler, A synthetic oscillatory network of tran- [49] F. Valdez, P. Melin, O. Castillo, A survey on nature-inspired
scriptional regulators, Nature. 403 (2000), 335. optimization algorithms with fuzzy logic for dynamic parameter
[43] C. Fuchs, S. Spolaor, M.S. Nobile, U. Kaymak, pyFUME: a Python adaptation, Expert Syst. Appl. 41 (2014), 6459–6466.
package for fuzzy model estimation, in 2020 IEEE International [50] Y. Tsukamoto, An approach to fuzzy reasoning method, in: M.M.
Conference on Fuzzy Systems (FUZZ-IEEE), IEEE, Glasgow, UK, Gupta, R.K. Ragade, R.R. Yager (Eds.), Advances in Fuzzy Set The-
2020, pp. 1–8. ory and Applications, North-Holland Publishing Company, Ams-
[44] C. Fuchs, A. Wilbik, U. Kaymak, Towards more specific estimation terdam, Netherlands, 1979.
of membership functions for data-driven fuzzy inference systems, [51] P. Angelov, R. Yager, Simplified fuzzy rule-based systems using
in 2018 IEEE International Conference on Fuzzy Systems (FUZZ- non-parametric antecedents and relative data density, in 2011
IEEE), IEEE, Rio de Janeiro, Brazil, 2018, pp. 1–8. IEEE Workshop on Evolving and Adaptive Intelligent Systems
[45] M. Setnes, R. Babuska, U. Kaymak, H.R. van Nauta Lemke, Simi- (EAIS), IEEE, Paris, France, 2011, pp. 62–69.
larity measures in fuzzy rule base simplification, IEEE Trans. Syst. [52] X. He, Weighted fuzzy logic and its applications, in Proceed-
Man Cybern. Part B. 28 (1998), 376–386. ings COMPSAC 88: the Twelfth Annual International Computer
[46] U. Kaymak, R. Babuska, Compatible cluster merging for fuzzy Software & Applications Conference, IEEE Computer Society,
modelling, in Proceedings of 1995 IEEE International Confer- Chicago, IL, USA, 1988, pp. 485–486.
ence on Fuzzy Systems, IEEE, Yokohama, Japan, 1995, vol. 2, [53] N.N. Karnik, J.M. Mendel, Q. Liang, Type-2 fuzzy logic systems,
pp. 897–904. IEEE Trans. Fuzzy Syst. 7 (1999), 643–658.
[47] C. Fuchs, S. Spolaor, M.S. Nobile, U. Kaymak, A graph the- [54] J. van den Berg, U. Kaymak, W.-M. van den Bergh, Probabilistic
ory approach to fuzzy rule base simplification, in: M.J. Lesot et reasoning in fuzzy rule-based systems, in: P. Grzegorzewski, O.
al. (Eds.), International Conference on Information Processing Hryniewicz, M.Á. Gil (Eds.), Soft Methods in Probability, Statis-
and Management of Uncertainty in Knowledge-Based Systems, tics and Data Analysis, Springer, Heidelberg, Germany, 2002, pp.
Springer, Cham, Switzerland, 2020, pp. 387–401. 189–196.
[48] M.S. Nobile, P. Cazzaniga, D. Besozzi, R. Colombo, G. Mauri, G.
Pasi, Fuzzy self-tuning PSO: a settings-free algorithm for global
optimization, Swarm Evol. Comput. 39 (2018), 70–85.