Fuzzy Inference module for behaviour-based control
June 6, 2014
In previous blog I talked about the subsumption architecture of Hurby toy, now it is time to talk about the fuzzy inference module which controls its happiness state.
Hurby's happiness depends on two main variables.
- Elapsed time since last play (in hours).
- Playing time trend (mean measured playing time), in minutes.
According with these inputs, Hurby will get happier if elapsed time since last play is reduced and playing time trend is incremented. On the other hand, it will get unhappier if that elapsed time increases and/or playing time trend decreases.
As both inputs are relative and vague concepts, it is a good situation to think about the use of a fuzzy inference reasoning module. Within this fuzzy module I can control Hurby's behaviour according with vague input specifications, for example: "If last playing time was a long time ago and playing time is decreasing then Hurby will be very unhappy".
These kind of rules are very easy to understand by humans, and what fuzzy systems permits is to program this kind of reasoning.
Furthermore, I am an enthusiast of KISS principle. If you don't know what means KISS, you can googled it. But basically is an acronym for Keep It Simple, Stupid. And it states that most systems works best if they are simple rather than complicated.
In this case, applying KISS principle to the design of the Fuzzy Inference System, I've got these key points:
- Inpust only will can be catalogued into 3 fuzzy sets (LOW, MIN, HIGH) or similar and these sets are triangles.
- Output will be the percentage of happiness. 0% means completely unhappy and 100% means extremely happy.
- Defuzzification method for the percentage of happiness is done according with the "center of mass". And hence, output fuzzy sets are singleton points in stead of triangles.
All of these premises, give me this situation:
- Elapsed time since last play will be expressed in hours and is fuzzified according with this figure:
- Playing time trend will be expressed in minutes and fuzzified as shown below:
- Percent of happiness (output of the fuzzy inference module) is defuzzified to get a crisp happiness % as shown below:
As inputs are catalogued into 3 fuzzy sets, and there are only 2 inputs, this raises the number of fuzzy rules to 9 (3*3). So graphically, the knowledge base of this fuzzy module will be this (see below). Now I have to fill it:
Filling this knowledge base implies that I have to decide how Hurby will react depending on the inputs. In the case of the first cell which corresponds to rule: "If time since last play is short AND playing time trend is also short, then Hurby will keep NEUTRAL. And filling the rest of rules the result is this:
As you could se above, during the explanation of the first rule, there is a logical operator (AND). Also, can be other operator (OR). In these cases, and based on fuzzy logic algebra, I will translate AND operation into MINimum values, while OR operations will be MAXimum values. I will explain with an example:
Suppose that in a certain moment inputs are: TSLP=12hours and PTT=3min, in that case the fuzzification of both inputs will activate 3 rules (MED-SHORT, MED-MED, MED-LARGE):
As shown, those 3 activated rules have the minimum value of each input membership (due to the AND operator). In this case all of them has a value of 0.5.
Looking at the knowledge base, the first and second activated rules have the same effect, that is, Hurby will be NEUTRAL. This can be translated into a single rule with OR operator:
If (TSLP is medium AND PTT is short) OR (TSLP is medium AND PTT is medium) then happiness will be neutral.
And as told before, OR operator is translated into MAX value, in this case MAX(0.5, 0.5) = 0.5. In conclussion the fuzzy inference has a result of 2 rules:
- Hurby will be NEUTRAL with a membership of 0.5 (1st and 2nd rules)
- Hurby will be HAPPY with a membership of 0.5 (3rd rule)
Applying the Center Of Mass equation as defuzzification method, we have that Hurby's happiness will be 75%.
Attached to the blog, you can find a C++ class named FuzzyIO8 (both files cpp an h in a zip compressed one). This is the fuzzy inference module explained. I've given that name because I format both inputs and output as <uint8_t> data types. This simplifies fuzzy calculations and improves its execution time in real time applications like this one. Even, simplifying this module as I've done, it only requires a dozes of RAM bytes, which is very important in resource-constrained MCUs, where RAM is very appreciated for other purposes.
In next blog entries I will talk about how Hurby will move its eyes, ears, feet and mouth depending on its happiness state and which is the function of the module I've named as Motor Cortex.