Jfuzzylogic: A Robust and Flexible Fuzzy-Logic Inference System Language Implementation
Jfuzzylogic: A Robust and Flexible Fuzzy-Logic Inference System Language Implementation
Abstract—This work introduces jFuzzyLogic, an open source In this work, we introduce an open source Java library
library for fuzzy systems which allow us to design Fuzzy named jFuzzyLogic. This fuzzy systems library allows FLCs
Logic Controllers supporting the standard for Fuzzy Control design and implementation, following the standard for Fuzzy
Programming published by the International Electrotechnical
Commission. This library is written in Java and is available Control Language (FCL) published by the International Elec-
as open source from jfuzzylogic.sourceforge.net. The use of trotechnical Commission (IEC 61131-7) [15]. The IEC-61131
jFuzzyLogic is illustrated through the analysis of one case study. norm is well known for defining the Programmable Controller
Languages (PLC), commonly used in industrial applications.
In the part 7, this standard offers a well defined common
I. I NTRODUCTION understanding of the basic means to integrate fuzzy control
applications in control systems. It also defines a common
Fuzzy rule based systems (FRBSs) are one of the most language to exchange portable fuzzy control programs among
important areas for the application of the Fuzzy Set Theory different platforms.
[1]. Classical rule based systems deal with IF-THEN rules. The main goal of jFuzzyLogic is to bring the benefits of
FRBSs constitute an extension to classical systems, having open source software and standardization to the fuzzy systems
antecedents and consequents composed of fuzzy logic state- community. Our library offers several advantages:
ments. • Standardization, which reduces programming work and
A Fuzzy Logic Controller (FLC) [2]–[5] is a FRBS com- learning curve. This library contains the basic program-
posed of: i-) a Knowledge Base that comprises the information ming elements for the Standard IEC 61131-7, alleviating
used by the expert operator in the form of linguistic control developers from boiler plate programming tasks.
rules; ii-) a Fuzzification Interface, that transforms the crisp • Extensibility, the object model and API allows to create
values of the input variables into fuzzy sets; iii-) an Inference a wide range of applications. This is of special interest
System, that uses the fuzzy values from the Fuzzification for the research community.
Interface and the information from the Knowledge Base to • Platform independence, allows to develop and run on
perform the reasoning process and iv-) the Defuzzification any hardware and operating system configuration that
Interface, which takes the fuzzy action from the Inference supports Java.
System and translates it into crisp values for the control This work is arranged as follows. The next section presents
variables. a comparison on non-commercial fuzzy software and the main
FLCs are suitable for engineering applications in which benefits that the jFuzzyLogic offers with respect to other
classical control strategies do not achieve good results or when libraries. Section III describes jFuzzyLogic’s main features.
it is too difficult to obtain a mathematical model. FLCs usually Section IV, illustrates how jFuzzyLogic can be used in a
have two characteristics: the need for human operator experi- control application. Conclusions are presented in Section V.
ence, and a strong non linearity. Many real-world applications
use FLCs [6] such as mobile robot navigation [7], [8], air II. C OMPARISON OF FUZZY LOGIC SOFTWARE
conditioning controllers [9], [10], domotic control [11], [12], In this section we present a comparison on non-commercial
and industrial applications [13], [14]. fuzzy software (Table I). We center our interest on free
FLCs are powerful for solving a wide range of problems, software distributions because of its important role in the
but their implementation requires a certain programming ex- scientific research community [16]. Moreover, we do not want
pertise. In the last few years, many fuzzy logic software to establish a comparison among all software tools or to
tools have been developed to reduce this task. Some are emphasize the advantages of one over another. Our objective
commercially distributed, for example MATLAB Fuzzy logic is to detect the major differences in the software and then to
toolbox(www.mathworks.com), while a few are available as categorize jFuzzyLogic as an alternative to these suites when
open source software (see section II). other research requirements are needed.
We analyze twenty five packages (including jFuzzyLogic), The specification defines six programming languages: In-
mostly from SourceForge or Google-Code, which are consid- struction list (IL), Structured text (ST), Ladder diagram (LD),
ered to be amongst the most respectable software repositories. Function block diagram (FBD), Sequential function chart
The packages are analyzed in the following categories: (SFC), and Fuzzy Control Language (FCL). While IL, ST,
• FCL support. Only four packages (∼ 17%) claim to sup- and FCL are text based languages, LD, FBD and SFC are
port IEC 61131-7 specification. Notably two of them are graphic based languages.
based on jFuzzyLogic. Only two packages that support Instruction list is similar to assembly language: one in-
FCL are not based on our software. Unfortunately neither struction per line, low level and low expression commands.
of them seem to be maintained by their developers any Structured text, as the name suggests, intends to be more
more. Furthermore, one of them has some code from structured and it is very easy to learn and understand for
jFuzzyLogic. anyone with a modest experience in programming. The focus
• Programming language. This is an indicator of code of this work is FCL, which is oriented to fuzzy logic based
portability. There languages of choice were mainly Java control systems and its syntax is similar to ST.
and C++/C (column Lang.). Java being platform inde-
pendent has the advantage of portability. C++ has an
A. IEC Language concepts
advantage in speed, but also allows easier integration in
industrial controllers. All IEC-61131 languages are modular. The basic module is
• Functionality. Seven packages (∼ 29%) were made for called Programmable Organization Unit (POU) and includes
specific purposes, marked as ‘specific’ (column Notes, Programs, Functions or Function Blocks. A system is usually
Table I). Specific code usually has limited functionality, composed of many POUs, and each of these POUs can
but it is simpler and has a faster learning curve for the be programmed in a different language. For instance, in a
user. system consisting of two functions and one function block
• Membership functions. This is an indicator of how com- (three POUs), one function may be programed in LD, another
prehensive and flexible the package is. Specific packages function in IL and the function block may be programmed in
include only one membership function (typically trape- ST. The norm defines all common data types (e.g. BOOL,
zoid) and/or one defuzzification method (data not shown). REAL, INT, ARRAY, STRUCT, etc.) as well as ways to
In some cases, arbitrary combinations of membership interconnect POUs, assign process execution priorities, process
functions are possible. These packages are marked with timers, CPU resource assignment, etc.
asterisk. For example, ‘M + N ∗ ’ means that the software The concepts of a Program and Functions are quite intuitive.
supports M membership functions plus another N which Programs are simple set of statements and variables. Functions
can be arbitrarily combined. are calculations that can return only one value and are not
• Latest release. In eight cases (∼ 33%) there were no supposed to have state variables.
released files for the last three years or more (see Rel.
A Function Block resembles a very primitive object. It
column in the Table I). This may indicate that the package
can have multiple input and multiple output variables, can be
is no longer maintained, and in some cases the web site
enabled by an external signal, and can have local variables.
explicitly mentions this.
Unlike an object, a function block only has one execution
• Code availability and usability. Five of the packages (∼
block (i.e. there are no methods). The underlying idea for these
21%) had no files available, either because the project
limitations is that you should be able to implement programs
was no longer maintained or because the project never
using either text-based or graphic-based languages. Having
released any files at all. Whenever the original sites were
only one execution block, allows to easily control execution
down, we tried to retrieve the projects from alternative
when using graphic-based language to interconnect POUs.
mirrors. In three cases (∼ 13%) the packages did not
compile. We performed minimal testing by just following At first glance FCL is similar to ST. However, there are
the instructions, if available, and make no effort to correct some very important differences. FCL uses exclusively a new
any compilation problems. POU type: Fuzzy Inference System (FIS) which is a special
case of a Function Block. All fuzzy language definitions
In summary, only eight of the software packages (∼ 33%) should be within a FIS. Since a fuzzy system is inherently
seemed to be maintained, compiled correctly, and had exten- parallel, there is no concept of execution order, therefore there
sive functionality. Only two of them are capable of parsing are no statements. For instance, there is no way to create
FCL (IEC-61131-7) files and both are based on jFuzzyLogic. the typical “Hello world” example since there is no print
statement. A simple example of a FIS using FCL is shown
III. JF UZZY L OGIC in Table II, this FCL code calculates the tip in a restaurant
Fuzzy Control Language is an industry standard specifica- (the equivalent of a “Hello world” program in fuzzy systems).
tion released by the International Electrotechnical Commission Fig. 1 shows the membership functions.
(IEC) as part of the Programmable Controller Languages Table III shows the corresponding Java code to run the FCL
(PLC) defined in the IEC-61131 specification. code shown in Table II.
TABLE I
C OMPARISSON ON OPEN FUZZY LOGIC SOFTWARE PACKAGES . C OLUMNS DESCRIBE : P ROJECT NAME (NAME ), IEC 61131-7 LANGUAGE SUPPORT (IEC),
LATEST RELEASE YEAR (R EL .), MAIN PROGRAMMING LANGUAGE (L ANG .), SHORT DESCRIPTION FORM WEBSITE (D ESCRIPTION ), NUMBER OF
MEMBERSHIP FUNCTIONS SUPPORTED (MF) AND F UNCTIONALITY ( NOTES ). NAME ∗ : PACKAGE IS MAINTAINED , COMPILES CORRECTLY, AND HAS
EXTENSIVE FUNCTIONALITY.
FUNCTION_BLOCK tipper
VAR_INPUT
service, food : REAL;
END_VAR
VAR_OUTPUT
tip : REAL;
END_VAR
FUZZIFY service
TERM poor := (0, 1) (4, 0) ;
TERM good := (1, 0) (4,1) (6,1) (9,0);
TERM excellent := (6, 0) (9, 1);
END_FUZZIFY
FUZZIFY food
TERM rancid := (0, 1) (1, 1) (3,0);
TERM delicious := (7,0) (9,1);
END_FUZZIFY
DEFUZZIFY tip
TERM cheap := (0,0) (5,1) (10,0);
TERM average := (10,0) (15,1) (20,0);
TERM generous := (20,0) (25,1) (30,0);
METHOD : COG; // Center of Gravity
END_DEFUZZIFY
RULEBLOCK tipRules
Rule1: IF service IS poor OR food IS rancid THEN tip IS cheap;
Rule2: IF service IS good THEN tip IS average;
Rule3: IF service IS excellent AND food IS delicious THEN tip IS generous;
END_RULEBLOCK
END_FUNCTION_BLOCK
TABLE III
E XAMPLE OF JAVA API TO EXECUTE FCL CODE .
Sum.
Only two membership functions are defined in the IEC
standard: singleton and piece-wise linear. jFuzzyLogic also im-
plements other commonly used membership functions: trape-
zoidal, sigmoidal, gaussian, generalized bell, difference of sig-
Fig. 2. Activation methods: Min (left) and Prod (right).
moidal, and cosine. Furthermore, jFuzzyLogic allows to build
arbitrary membership functions by combining mathematical
functions.
Finally, accumulation method defines how the consequents
from multiple rules are combined within a Rule Block (see Because of the flexibility in defining membership functions,
Fig. 3). Accumulation methods defined in the norm include: we discretize them at a number of points. The number of
Maximum, Bounded sum, Normed sum, Probabilistic OR, and points, by default one thousand, can be adjusted according
Fig. 3. Accumulation method: Combining consequents from multiple rules
using Max accumulation method.
VAR_INPUT
rd : REAL; // Right distance
dq : REAL; // Distance quotient
o : REAL; // Orientation. Note: ’or’ is a reserved word
v : REAL; // Velocity
END_VAR
VAR_OUTPUT
la : REAL; // Linear acceleration
av : REAL; // Angular velocity
END_VAR
FUZZIFY rd
TERM L := trian 0 0 1;
TERM M := trian 0 1 2;
TERM H := trian 1 2 3;
TERM VH := trian 2 3 3;
END_FUZZIFY
FUZZIFY dq
TERM L := trian 0 0 2;
TERM H := trian 0 2 2;
END_FUZZIFY
FUZZIFY o
TERM HL := trian -450 -450 -225;
TERM LL := trian -450 -225 0;
TERM Z := trian -225 0 225;
TERM LR := trian 0 225 450;
TERM HR := trian 225 450 450;
END_FUZZIFY
FUZZIFY v
TERM L := trian 0 0 1;
TERM H := trian 0 1 1;
END_FUZZIFY
DEFUZZIFY la
TERM VHB := trian -1 -1 -0.75;
TERM HB := trian -1 -0.75 -0.5;
TERM MB := trian -0.75 -0.5 -0.25;
TERM SB := trian -0.5 -0.25 0;
TERM Z := trian -0.25 0 0.25;
TERM SA := trian 0 0.25 0.5;
TERM MA := trian 0.25 0.5 0.75;
TERM HA := trian 0.5 0.75 1;
TERM VHA := trian 0.75 1 1;
METHOD : COG; // Center of Gravity
DEFAULT := 0;
END_DEFUZZIFY
DEFUZZIFY av
TERM VHR := trian -1 -1 -0.75;
TERM HR := trian -1 -0.75 -0.5;
TERM MR := trian -0.75 -0.5 -0.25;
TERM SR := trian -0.5 -0.25 0;
TERM Z := trian -0.25 0 0.25;
TERM SL := trian 0 0.25 0.5;
TERM ML := trian 0.25 0.5 0.75;
TERM HL := trian 0.5 0.75 1;
TERM VHL := trian 0.75 1 1;
METHOD : COG;
DEFAULT := 0;
END_DEFUZZIFY
TABLE V
WALL FOLLOWING ROBOT. F UZZY CONTROLLER IN FCL LANGUAGE : RULE BLOCK .
RULEBLOCK rules
AND : MIN; // Use ’min’ for ’and’ (also implicit use ’max’ for ’or’ to fulfill DeMorgan’s Law)
ACT : MIN; // Use ’min’ activation method
ACCU : MAX; // Use ’max’ accumulation method
RULE 01: IF rd is L and dq is L and o is LL and v is L THEN la is VHB , av is VHR with 0.4610;
RULE 02: IF rd is L and dq is L and o is LL and v is H THEN la is VHB , av is VHR with 0.4896;
RULE 03: IF rd is L and dq is L and o is Z and v is L THEN la is Z , av is MR with 0.6664;
RULE 04: IF rd is L and dq is L and o is Z and v is H THEN la is HB , av is SR with 0.5435;
RULE 05: IF rd is L and dq is H and o is LL and v is L THEN la is MA , av is HR with 0.7276;
RULE 06: IF rd is L and dq is H and o is Z and v is L THEN la is MA , av is HL with 0.4845;
RULE 07: IF rd is L and dq is H and o is Z and v is H THEN la is HB , av is ML with 0.5023;
RULE 08: IF rd is L and dq is H and o is LR and v is H THEN la is VHB , av is VHL with 0.7363;
RULE 09: IF rd is L and dq is H and o is HR and v is L THEN la is VHB , av is VHL with 0.9441;
RULE 10: IF rd is M and dq is L and o is Z and v is H THEN la is SA , av is HR with 0.3402;
RULE 11: IF rd is M and dq is L and o is LR and v is H THEN la is Z , av is VHL with 0.4244;
RULE 12: IF rd is M and dq is L and o is HR and v is L THEN la is SA , av is HL with 0.5472;
RULE 13: IF rd is M and dq is L and o is HR and v is H THEN la is MB , av is VHL with 0.4369;
RULE 14: IF rd is M and dq is H and o is HL and v is L THEN la is Z , av is VHR with 0.1770;
RULE 15: IF rd is M and dq is H and o is HL and v is H THEN la is VHB , av is VHR with 0.4526;
RULE 16: IF rd is M and dq is H and o is LL and v is H THEN la is SA , av is VHR with 0.2548;
RULE 17: IF rd is M and dq is H and o is Z and v is L THEN la is HA , av is Z with 0.2084;
RULE 18: IF rd is M and dq is H and o is LR and v is L THEN la is HA , av is VHL with 0.6242;
RULE 19: IF rd is M and dq is H and o is LR and v is H THEN la is SA , av is VHL with 0.3779;
RULE 20: IF rd is M and dq is H and o is HR and v is L THEN la is Z , av is VHL with 0.6931;
RULE 21: IF rd is M and dq is H and o is HR and v is H THEN la is VHB , av is VHL with 0.7580;
RULE 22: IF rd is H and dq is L and o is Z and v is L THEN la is HA , av is VHR with 0.5758;
RULE 23: IF rd is H and dq is L and o is LR and v is H THEN la is SA , av is MR with 0.2513;
RULE 24: IF rd is H and dq is L and o is HR and v is L THEN la is HA , av is VHL with 0.5471;
RULE 25: IF rd is H and dq is L and o is HR and v is H THEN la is SA , av is HL with 0.5595;
RULE 26: IF rd is H and dq is H and o is HL and v is L THEN la is VHB , av is VHR with 0.9999;
RULE 27: IF rd is H and dq is H and o is HL and v is H THEN la is VHB , av is VHR with 0.9563;
RULE 28: IF rd is H and dq is H and o is LL and v is L THEN la is HA , av is VHR with 0.9506;
RULE 29: IF rd is H and dq is H and o is Z and v is L THEN la is HA , av is VHR with 0.4529;
RULE 30: IF rd is H and dq is H and o is Z and v is H THEN la is SA , av is VHR with 0.2210;
RULE 31: IF rd is H and dq is H and o is LR and v is L THEN la is HA , av is MR with 0.3612;
RULE 32: IF rd is H and dq is H and o is LR and v is H THEN la is SA , av is MR with 0.2122;
RULE 33: IF rd is H and dq is H and o is HR and v is L THEN la is HA , av is HL with 0.7878;
RULE 34: IF rd is H and dq is H and o is HR and v is H THEN la is SA , av is VHL with 0.3859;
RULE 35: IF rd is VH and dq is L and o is LR and v is L THEN la is HA , av is VHR with 0.5530;
RULE 36: IF rd is VH and dq is L and o is HR and v is L THEN la is HA , av is HR with 0.4223;
RULE 37: IF rd is VH and dq is L and o is HR and v is H THEN la is SA , av is HR with 0.3854;
RULE 38: IF rd is VH and dq is H and o is LL and v is L THEN la is HA , av is VHR with 0.0936;
RULE 39: IF rd is VH and dq is H and o is LR and v is L THEN la is HA , av is VHR with 0.7325;
RULE 40: IF rd is VH and dq is H and o is LR and v is H THEN la is SA , av is VHR with 0.5631;
RULE 41: IF rd is VH and dq is H and o is HR and v is L THEN la is HA , av is HR with 0.5146;
END_RULEBLOCK