Documentation
Tools for embedded systems
|
A Fuzzy Inference System (FIS) is a key component of any fuzzy logic system. It uses fuzzy set theory, IF-THEN rules and fuzzy reasoning process to find the output corresponding to crisp inputs. Predicates in IF-THEN
rules are connected using AND
or OR
logical connectives.
Fuzzy inference works by sending any process's crisp input to the fuzzifier, which applies a fuzzy membership function and maps the actual readings into fuzzy values. The inference engine applies fuzzy rules from the knowledge base and produces the fuzzy output. This output can not be used directly in any process or system. It needs to be mapped into the original domain. For this, the defuzzifier is used, which is the inverse process of fuzzification. It converts the fuzzy output into crisp output, which can be fed to the process.
The qFIS Fuzzy Inference System engine provides an API for building and evaluation of type-1 fuzzy logic inference systems.
The types of inferences supported by qFIS are listed in the qFIS_Type_t and are detailed below:
In a Mamdani system, the output of each rule is a fuzzy set. Since Mamdani systems have more intuitive and easier to understand rule bases, they are well-suited to expert system applications where the rules are created from human expert knowledge.
The output of each rule is a fuzzy set derived from the output membership function and the implication method of the FIS. These output fuzzy sets are combined into a single fuzzy set using the aggregation method of the FIS. Then, to compute a final crisp output value, the combined output fuzzy set is defuzzified using one of the methods described in Defuzzification Methods. To specify a FIS of this type use the Mamdani enum definition when calling qFIS_Setup().
Sugeno fuzzy inference, also referred to as Takagi-Sugeno-Kang fuzzy inference, uses singleton output membership functions that are either constant or a linear function of the input values. The defuzzification process for a Sugeno system is more computationally efficient compared to that of a Mamdani system, since it uses a weighted average or weighted sum of a few data points rather than computing the centroid of a two-dimensional area.
Each rule generates two values:
\(u_i\) Rule output level, which is either a constant value or a linear function of the input values.
and \(w_i\), the rule firing strength, that is determined by the rule antecedent
The output of each rule is the weighted output level, which is the product of \(w_i\) and \(u_i\).
The final output of the system is the weighted average/sum over all rule outputs:
where \(N\) is the number of rules.
Because of the linear dependence of each rule on the input variables, the Sugeno method is ideal for acting as an interpolating supervisor of multiple linear controllers that are to be applied, respectively, to different operating conditions of a dynamic nonlinear system.
To specificy a FIS of this type, use the Sugeno enum definition when calling qFIS_Setup().
In the Tsukamoto inference system, the consequent of each fuzzy if-then rule is represented by a fuzzy set with a monotonical membership function, As a result, the inferred output of each rule is defined as a crisp value induced by the rule's firing strength.
The overall output is taken as the weighted average of each rule's output. Since each rule infers a crisp output, the Tsukamoto fuzzy model aggregates each rule's output by the method of weighted average and thus avoids the time-consuming process of defuzzification.
To specify a FIS of this type, use the Tsukamoto enum definition when calling qFIS_Setup().
qFIS supports five different methods, as listed in the qFIS_DeFuzz_Method_t type for computing a single crisp output value for such a fuzzy set.
To carry out the FIS building process in a more friendly manner, we will explain it using a specific example. The goal is to solve the tipping problem, which is described as follows:
Given a number from 0 through 10 that represents the quality of service at a restaurant, where 10 is excellent, and another number from 0 through 10 that represents the quality of the food, where 10 is delicious, what should the tip be? Tipping behavior varies depending on local traditions and personal preferences. In this example, the problem is based on tipping as it is typically practiced in the United States. An average tip for a meal in the US is 15%. A generous tip could be as high as 25% and a cheap tip could be 5%.
The actual amount of the tip can vary depending on the quality of the service and food. For this problem, tipping behavior is defined using the following three rules.
IF the service IS poor OR the food IS rancid, THEN the tip IS cheap.
IF the service IS good, THEN the tip IS average.
IF the service IS excellent OR the food IS delicious, THEN the tip IS generous.
This leads to a system with 2 inputs : service
and food
and 1 output: tip
5 membership functions for the inputs
1.5
and center on 0
1.5
and center on 5
1.5
and center on 10
[0 0 1 3]
[7 9 10 10]
and 3 membership functions for the output
[0 5 10]
[10 15 20]
[20 25 30]
To build the fuzzy system, you must first instantiate an abstract object of type qFIS_t that represents the fuzzy inference system, then the input and output vectors, fuzzy set vectors for inputs and outputs, and enumerations with the tags for all of them. Let's take a look :
Then, we will define the rules of the fuzzy system using the previously defined tags. Rules should be defined as an array of type qFIS_Rules_t and the contents should be rules constructed with the provided statements:
Let's apply some of these statements to build the rule set.
Additionally, we also need to define a vector where the firing strength of each rule will be stored. The size of the vector will be the number of rules. In this particular case, there will be only three.
Once all the necessary elements have been defined, we can proceed to the construction of the fuzzy system. First, we must configure the inputs and outputs by setting the ranges of each. For this, we will use the qFIS_InputSetup() and qFIS_OutputSetup() functions as follows:
Then, let's define the parameters of all the membership functions:
the next step is to configure the membership functions by relating I/O, tags, shape and parameters one by one by using the qFIS_SetMF() API as follows:
Finally, we only have to configure the instance that represents the fuzzy system using the qFIS_Setup() API:
If we already have a fuzzy system configured with qFIS_Setup(), we can evaluate it by using qFIS_Fuzzify(), qFIS_Inference() and qFIS_DeFuzzify(). Input values can be set with qFIS_SetInput() and output values can be obtained with qFIS_GetOutput()
To show its use, first we are going to put everything together in a single code snippet and we are going to create two functions, tipper_init()
and tipper_run()
that will be in charge of setting up the fuzzy inference system and evaluating it respectively.
Building a fuzzy system in code can become tedious, especially when you want to tune membership function parameters. It would be appropriate to have a graphical tool that reflects the changes made by each tweak.
Although the qFIS engine does not provide such a tool, a well-known tool, MATLAB's Fuzzy Logic Toolbox, can be used to build the fuzzy system and generate qFIS-compatible C code. qLibs provides a MATLAB command that can be used to take a FIS object generated by that tool and generate C code based on the qFIS engine.
Download the C-Code generator here: