jFuzzyLogic is an open source fuzzy logic library implementing industry standards to simplify fuzzy systems developments.
</p>
jFuzzyLogic is a fuzzy logic package.
As you might have guessed, it is written in Java.
jFuzzyLogic implements Fuzzy control language (FCL) specification IEC 61131 part 7, as well as a complete library that will greatly simplify your fuzzy logic development or research work.
<br>
<br>
<b>Fuzzy control language:</b> For an introduction to FCL, you can read a pre-release specification <ahref="pdf/iec_1131_7_cd1.pdf">here</a>.
<br>
<br>
<b>jFuzzyLogic:</b> For a thorough introduction to jFuzzyLogic, I highly recommend that you read our <ahref="pdf/Cingolani_Alcala-Fdez_jFuzzyLogic_2013_IJCIS.pdf">paper</a>.
</p>
<br>
<pclass="lead"> Downloading: Why two versions? </p>
<p>
For most applications, you should download the full version.
<br>
The <b>core</b> version is used mostly on mobile applications or applications where resources are limited and graphics capabilities are not required.
To build, just run <code>ant</code> from the project's folder.
</p>
<br>
<pclass="lead"> Publications & Citing </p>
<p>
If you are using jFuzzyLogic in academic environments, please cite our publications:
<br>
<br>
<ul>
<li> Cingolani, Pablo, and Jesús Alcalá-Fdez. <ahref="pdf/Cingolani_Alcala-Fdez_jFuzzyLogic_2013_IJCIS.pdf"><i>"jFuzzyLogic: a Java Library to Design Fuzzy Logic Controllers According to the Standard for Fuzzy Control Programming"</i></a>
<pre>
@article{cingolanijfuzzylogic,
title={jFuzzyLogic: a Java Library to Design Fuzzy Logic Controllers According to the Standard for Fuzzy Control Programming},
author={Cingolani, Pablo and Alcal{\'a}-Fdez, Jes{\'u}s}
booktitle={International Journal of Computational Intelligence Systems},
pages={61--75},
year={2013}
}
</pre>
<li> Cingolani, Pablo, and Jesus Alcala-Fdez. <ahref="pdf/jFuzzyLogic.pdf"><i>"jFuzzyLogic: a robust and flexible Fuzzy-Logic inference system language implementation."</i></a> Fuzzy Systems (FUZZ-IEEE), 2012 IEEE International Conference on. IEEE, 2012.
<pre>
@inproceedings{cingolani2012jfuzzylogic,
title={jFuzzyLogic: a robust and flexible Fuzzy-Logic inference system language implementation},
author={Cingolani, Pablo and Alcala-Fdez, Jesus},
booktitle={Fuzzy Systems (FUZZ-IEEE), 2012 IEEE International Conference on},
A simple demo is available using the command line option <code>demo</code>.
It will show an animation of the <code>tipper.fcl</code> example:
<pre>
java -jar jFuzzyLogic.jar demo
</pre>
<br>
Using an FCL file, jFuzzyLogic will show all memebership functions for all input and output variables.
<pre>
java -jar jFuzzyLogic.jar myfile.fcl
</pre>
<br>
You can also specify input variables using the command line option <code>-e</code>, jFuzzyLogic will calculate and show the defuzzified output variables as well as the degree of support for each rule.
The <code>-e</code> command line option must be followed by the input variables values.
<pre>
java -jar jFuzzyLogic.jar -e tipper.fcl 8.5 9
FUNCITON_BLOCK tipper
VAR_INPUT food = 8.500000
VAR_INPUT service = 9.000000
VAR_OUTPUT tip = 24.999984
RULE_BLOCK No1
Support Rule name Rule
0.000000 1 IF (service IS poor) OR (food IS rancid) THEN tip IS cheap;
0.000000 2 IF service IS good THEN tip IS average;
0.750000 3 IF (service IS excellent) AND (food IS delicious) THEN tip IS generous;
The obvious advantages of having a standard language for fuzzy systems.
</p>
<p>
Fuzzy control language (FCL) is a language defined in the IEC 61131 part 7 specification.
<br>
<br>
<b>Note:</b> Unfortunately the FCL specification is not freely available and you have to buy the document from IEC.
But you can access a free copy of the pre-release version of the spec. <ahref="pdf/iec_1131_7_cd1.pdf">here</a>.
<br>
<br>
Obviously, having a standard language to program fuzzy systems has many benefits.
Before FCL, each fuzzy logic library implementation had different ways to define membership functions, rules, etc.
Most of the time you had to learn APIs which were incompatible from one system to another.
Furthermore, using APIs to create systems is cumbersome and error prone.
<br>
FCL simply removes all these problems.
Furthermore, FCL is quite simple to learn and most people learn the basics by taking a looks at a couple of examples.
<br>
<br>
<b>Fuzzy control language:</b> For an introduction to FCL, please read the pre-release specification <ahref="pdf/iec_1131_7_cd1.pdf">here</a>.
<br>
<br>
The following example is FCL code for the classic "tipper" problem.
The problem is how to calculate the tip in a restaurant (obviously, this is a toy example of a trivial problem. equivalent to a "hello world!" example in other programming languages):
<br>
<br>
You can download this file <ahref="fcl/tipper.fcl">here</a>.
<pre>
/*
Example: A tip calculation FIS (fuzzy inference system)
Calculates tip based on 'service' and 'food'
Pablo Cingolani
*/
FUNCTION_BLOCK tipper // Block definition (there may be more than one block per file)
METHOD : COG; // Use 'Center Of Gravity' defuzzification method
DEFAULT := 0; // Default value is 0 (if no rule activates defuzzifier)
END_DEFUZZIFY
// Inference rules
RULEBLOCK No1
AND : MIN; // Use 'min' for 'and'
ACT : MIN; // Use 'min' activation method
ACCU : MAX; // Use 'max' accumulation method
RULE 1 : IF service IS poor OR food IS rancid THEN tip IS cheap;
RULE 2 : IF service IS good THEN tip IS average;
RULE 3 : IF service IS excellent AND food IS delicious THEN tip IS generous;
END_RULEBLOCK
END_FUNCTION_BLOCK
</pre>
<br>
<br>
In order to run this example, you can download the FCL file <ahref="fcl/tipper.fcl">tipper.fcl</a> and then run the following command:
<br>
<br>
<pre>
java -jar jFuzzyLogic.jar tipper.fcl
</pre>
<br>
<br>
This command will parse and load the FCL code and show the membership functions (continue reading the next sub-section for more details).
<br>
<imgsrc="images/tipper_MF.png">
<br>
<br>
<pclass="lead">
Step by step explanation
</p>
Keep in mind that FCL is defined as a 'Control language', so the main concept is a 'control block' which has some input and output variables.
You cannot make programs in the usual way, for instance, there is no "print" statement in FCL.
Furthermore, there is no implicit execution order.
In theory, the whole block is executed in parallel.
<br>
<br>
We now analyze the previously shown FCL code (<ahref="fcl/tipper.fcl">tipper.fcl</a>):
<br>
<br>
<ul>
<li>First you define each <code>FUNCTION_BLOCK</code> (there may be more than one in each file)
<br>
<pre> FUNCTION_BLOCK tipper </pre>
<br>
<li> Then input and output variables are defined.
<br>
<b>Note:</b> Even though IEC-61131 defines several data types, the only variable type supported is <code>REAL</code>, which is the only one needed for fuzzy logic applications.
<br>
<pre>
VAR_INPUT
service : REAL;
food : REAL;
END_VAR
VAR_OUTPUT
tip : REAL;
END_VAR
</pre>
<br>
<li> Next we define how each input variable is fuzzified is defined in <code>FUZZIFY</code> block. In each block we define one or more <code>TERMs</code> (also called LinguisticTerms). Each term is composed by a name and a membership function. E.g.:
<br>
<pre>
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
</pre>
<br>
In this lines we define how variable <code>service</code> will be fuzzified. Three terms are used, for instance term <code>poor</code> uses a piece-wise linear membership function defined by points (x0, y0) = (0, 1) and (x1, y1) = (4, 0)
<br>
<br>
<imgsrc=images/service.png>
<br>
<br>
Similarly, we define <code>food</code> membership functions:
<br>
<pre>
FUZZIFY food
TERM rancid := (0, 1) (1, 1) (3,0) ;
TERM delicious := (7,0) (9,1);
END_FUZZIFY
</pre>
<imgsrc=images/food.png>
<br>
<br>
<li> Output variables are defuzzified to get a 'real' output number.
Defuzzifiers are defined in <code>DEFUZZIFY</code> blocks.
Linguistic terms (or simply terms) are defined just like in <code>FUZZIFY</code> blocks.
<pre>
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);
</pre>
<br>
<imgsrc=images/tip.png>
<br>
<br>
<br>
Next, we state that 'Center of gravity' as defuzzifier method.
<pre> METHOD : COG; </pre>
We also use '0' as default value, i.e. the value use when no rule activates this variable.
<pre> DEFAULT := 0; </pre>
<li> At this point we are ready to define the rules.
This is done using a <code>RULEBLOCK</code>.
First we must define some parameters.
Use 'min' for 'and' (implicitly uses 'max' for 'or' to fulfill DeMorgan's Law).
<pre>
RULEBLOCK No1
AND : MIN;
</pre>
Use 'min' activation method
<pre> ACT : MIN; </pre>
Use 'maximum' as accumulation method.
<pre> ACCU : MAX; </pre>
And now define some fuzzy rules
<pre>
RULE 1 : IF service IS poor OR food IS rancid THEN tip IS cheap;
RULE 2 : IF service IS good THEN tip IS average;
RULE 3 : IF service IS excellent AND food IS delicious THEN tip is generous;
Here we show some details on jFuzzyLogic's structure as well as some basic FCL definitions.
</p>
<p>
How this package works and how classes are organized is briefly explained here.<p>
<ul>
<li> A fuzzy inference system (<code>FIS</code>) is composed by one or more <code>FuncionBlock</code> class, like in FCL. E.g.:
<pre>
FUNCTION_BLOCK functionBlock1
...
END_FUNCTION_BLOCK
FUNCTION_BLOCK functionBlock2
...
END_FUNCTION_BLOCK
</pre><p>
<li> Each <code>FuncionBlock</code> is composed by one or more <code>RuleBlock</code> class and some <code>Variables</code>, as well as <code>Fuzzyfiers</code> and <code>Defuzzifiers</code>. Again, like in FCL, e.g.:
<pre>
FUNCTION_BLOCK functionBlockName
VAR_INPUT
...
END_VAR
VAR_OUTPUT
...
END_VAR
FUZZIFY inputVariable
...
END_FUZZIFY
DEFUZZIFY outputVariable
...
END_DEFUZZIFY
RULEBLOCK No1
...
END_RULEBLOCK
END_FUNCTION_BLOCK
</pre><p>
<li> Each <code>Rule</code> class is composed by an antecedent (<b>IF</b> part) and a consequent (<b>THEN</b> part), e.g.:<br>
<pre>
RULE 1 : IF service IS poor OR food IS rancid THEN tip IS cheap;
</pre>
<ul>
<li> Antecedent: <i>"service IS poor OR food IS rancid"</i>
<li> Consequent: <i>"tip IS cheap"</i>. Note that there may be more than one consequent.
<li> A rule implication (or activation) method can be defined (althought FCL does not allow different implication method for each rule, it can be defined at RULEBLOCK level).
e.g.: ACT : MIN; // Use 'min' activation method
</ul>
<p>
<li> Consequents are a 'collection' of <code>RuleTerms</code> classes (e.g. <i>"tip IS cheap"</i> is a RuleTerm)
<p>
<li> An Antecedent is represented by a <code>RuleExpression</code> class. A <code>RuleExpression</code> is just two terms connected by one <code>RuleConnectionMethod</code> (rule conectors are 'AND', 'OR' and 'NOT')<br>
e.g.: service IS poor OR food IS rancid <br>
<ul>
<li> First term: <i>"service IS poor"</i>
<li> Second term: <i>"food IS rancid"</i>
<li> RuleConnectionMethod is <i>'OR'</i>
</ul>
<br>
<b>Note:</b> Each term can be either a <code>RuleTerm</code> or a <code>RuleExpression</code>, this definition is recursive, so arbitrarily complex expressions can be created.
<p>
<li> Each <code>RuleTerm</code> is defined by a <code>Variable</code> and a <code>LinguisticTermName</code>.
e.g.: service IS poor
<ul>
<li><code>Variable</code>: service
<li><code>LinguisticTermName</code>: 'poor'
<li> Connector: 'IS'
</ul>
It can also be 'negated' e.g.: service IS NOT excellent<br>
<p>
<li> Each <code>Variable</code> has a <code>name</code> and some <code>LinguisticTerms</code>
This section shows how to add jFuzzyLogic as a library in your Java projects.
</p>
<p>
In order to use jFuzzyLogic in your projects, you must add <code>jFuzzyLogic.jar</code> as one of your JAR files (libraries).
This is done like you'd do it with any other JAR file.
Since this is a basic Java programming skill, so I won't get into many details.
<br>
<br>
<b>Full example project:</b><ahref="http://sourceforge.net/projects/jfuzzylogic/files/jfuzzylogic/jFuzzyLogicProjectExample.zip">Here</a>, you can download a ZIP file with a sample Eclipse project that uses jFuzzyLogic.
<br>
<br>
This video shows how to create a Java project that uses jFuzzyLogic using the Eclipse IDE.
<br>
<b>Note:</b> The Java code is copied from <code>TestTipper.java</code> and the FCL code is from <code>tipper.fcl</code> file.<p>
jFuzzyLogic provides a parameter optimization framework, allowing to learn or refine fuzzy parameters using machine learning algorithms.
</p>
The parameter optimization framework requires to define a learning method which can be applied to a dataset in order to refine fuzzy membership parameters.
<br>
<br>
In this Java code example, we optimize fuzzy sets' parameters and fuzzy rule's weights: