Introduction

In the following article, we briefly introduce Fuzzy Framework library which supports calculations based on fuzzy logic in .NET. In past, there have been a couple of similar projects like the one described in [3], but no one matched exactly my requirements:

Simplicity - everyone can understand the code, extend it, and make use of it throughout his systems.

Support both of continuous and discrete sets.

Support of arbitrary fuzzy sets as long as we can describe them by a group of polynomial functions.

No special parser required – once some basic objects representing predefined fuzzy sets are instantiated, standard overloaded C# operators can be used to build any fuzzy relations, including implication.

Defuzzification of fuzzy relation into the crisp value directly using a C# method

Build-in support for graphic representation – sets can be displayed as graphs, relations as a hierarchy in a tree view.

In the following text, we outline the basics of fuzzy logic and fuzzy set theory, focusing on how it differs from the standard, Boolean logic and from crisp sets. The text is more an illustrative summary with practical examples rather than formal description of the theory. Interlaced are notes on how is the particular feature covered by our Fuzzy Framework.

Fuzzy Sets

2.1 What’s the difference?

Fuzzy systems become handy when someone intends to work with vague, ambiguous, imprecise, noisy, or missing information [7]. We can use it to control non-linear systems that are too tricky to model them mathematically. In the standard, crisp set theory, every element strictly either does, or does not belong into specific set. Let’s take the following picture as an example. Here, some products like apple or pear do belong to set Fruits, whereas others like carrot or broccoli do not.

Figure 1 - Elements Apple and Pear belong to the set Fruits, whereas Carrot and Broccoli do not.

The membership for each element in the figure above can be described by one of the Boolean values {true, false} or if more appropriate by {0,1}. Relation dealing with this fact is called membership function:

Element x either is or is not a member of set Fruits. In case x = apple or x = carrot, y will hold the value of 1 or 0, respectively.

Fuzzy sets let us to extend the potential values of y to the whole continuous interval of <0, 1>. Hence it is possible to handle, besides the strict is member and not a member statements, a sort of partial membership, too.

Pomologists (those who seriously study fruits) will kindly condone our following example. Majority of people consider tomato or various berries as a sort of fruit, although they feel it is not a 100% fruit like an apple is, for instance. This is depicted in Figure 2 by means of a fuzzy set, where the blue gradient represents membership of particular products in set Fruits.

Figure 2 – With fuzzy sets, it is feasible to describe partial membership like the one of tomato or berries within set Fruits

Membership function can be defined either by a formula, or by an explicit list. Considering the picture above, the membership of respective products within set Fruits can be defined in the following way.

Product x

Its membership in the set

Apple

1

Pear

1

Blueberry

0.6

Tomato

0.4

Broccoli

0

Carrot

0

Table 1 – Membership of products in the Fruits set

2.2 Continuous Sets vs. Discrete Sets

Sets, as well as variables representing their elements, can be categorized in many ways. One of the attributes is continuity. Another distinction is based on variable’s nominality/ cardinality/ ordinality, but we will not go to such details. Speaking about continuity, there are two distinct groups each variable belongs to:

Discrete (countable) – this is the case of the product variable used in our above example. Each product is a discrete (non)member of the Fruits set. Yet, on a real number axis, these elements can be represented as isolated points: apple as 1, pear as 2 etc.

Continuous (quantitative, metric). As long as variable is represented by a real number, there can be an infinitive number of distinct values defined. Furthermore, various arithmetic operations like addition or multiplication can be performed with these variables. Typical example of quantity described by a continuous variable is temperature. There can be a temperature with the value of 10.00 °C, yet it differs from 10.02 °C. Furthermore, there is unlimited number of values between 10.00 and 10.02 °C

In our internal implementation of fuzzy sets, we first have to define dimensions we will work with. Examples of dimensions are product or temperature:

product = new DiscreteDimension("Product", "Product being offered");
price = new ContinuousDimension("Price", "Price we are about to pay for an offer",
"$", 0, 1000);
temperature = new ContinuousDimension("t", "Temperature detected by sensor",
"°C", -30, +40);
deltaTemperature = new ContinuousDimension("▵t",
"Change of temperature detected by temperature sensor in time period",
"°C/min", -5, +5);

Note that for continuous dimensions, part of the definition is a lowest and highest possible value. Once dimensions are specified, particular fuzzy sets can be defined for each specific dimension.

We consider discreet sets as a special case of continuous sets. First, an index is assigned to each element. E.g. apple ≡ 1, pear ≡ 2,… Next, we define single point intervals on a continuous axis for each element. E.g. apple ≡ <1, 1>; pear ≡ <2, 2>. Since the elements are not ordinal, the order of elements on the axis will not be taking into account. This allows us to handle discere sets the same way as continuous sets, sharing the same code.

2.3 Linguistic Terms

In fuzzy logic, sets describing a fuzzy value are often called Lingustic Terms. Set Fruits or set Vegetables can be also regarded as a linguistic term. Similar for temperature, there can exist terms like Cold, Warm or Hot. The Goal is to use the natural language in order to build fuzzy expressions:

Product x is a fruit.

Temperature t is cold.

This effort goes further with the introduction of alternations. Term “very” intensifies the statement, for example. Thus the membership function of set “very cold” must have a steeper course than that one of set “cold”. The similar way, function for set “strictly around 3” has a spikier course than that the one for set “somewhere around 3”.

2.4 Fuzzy Numbers

Fuzzy number is just a synonym for a convex, normalized continuous fuzzy set [1].

In general, membership of an element within a set is described by a formula called membership function. In the crisp set theory, one can have a set “number 3” defined as interval <3,3> on . That means only is a member of this set, with the following membership function:

Fuzzy set “fuzzy number 3”, on the other hand, will moreover contain a closed neighbourhood of number three:

Fuzzy sets can be further divided based on type of membership function which describes them. The most common types are singleton, triangular, trapezoidal, and bell-shaped numbers.

The numbers depicted above are all normalized, i.e. the domain of mapping for the membership function fully spans the interval [0, 1].

One speaks about fuzzy numbers with a peak (singleton, triangular, bell-shaped) or with a flat (trapezoidal in the picture above).

Left number and right number are special cases of asymmetric fuzzy numbers. They are used to define intervals open on the right and the left side, respectively. See Figure 4. Right or left fuzzy number is suitable for description of cold or hot temperature, respectively.

Figure 4 – a) Right and b) Left fuzzy number

In our Fuzzy Framework, there are the following classes available to define arbitrary fuzzy sets of the specific format:

Figure 5 – Predefined fuzzy set classes

Next, when making an instance, it is always required to specify the dimension:

Other parameters represent support boundary, kernel boundary etc. New instance of DiscreteSet is empty by default, and needs to be filled by an explicit collection of members.

Internally are all fuzzy sets represented by a group of polynomial functions for distinct intervals:

where i1 and im are lower and upper boundaries of the universe. Hence each set can be processed in a uniform way, and currently available FuzzySet classes are easily extendable. Library Polylib [4] is used to handle some basic operations on polynomials like calculation of roots via Weierstrass iteration, derivation, and integration.

2.5 Operations on Fuzzy Sets

The goal is to introduce some useful operations similar to those available in crisp sets. There are plenty of options how fuzzy operators can be designed Anyway, to comply with the crisp set theory, all the alternatives of AND, OR and NOT operators do behave exactly the same for the combinations on the membership borderline (i.e. within the kernel, outside the support). This behaviour is the same as it would be with standard Boolean operators.

We overloaded standard C# operators like “&” or “|” to support some basic operations on fuzzy sets. Besides this, methods like Includes(...) or Complement(...) are also available as an alternative.

Having fuzzy set A (and also B for binary operators) with elements of the same dimension, the following table enlists the supported operations. U stays for universe, i.e. the set with all possible values of the specific type.

Operation Name

Input Cardinality

Type of output

Definition

C# operator in use

Note

Equality

binary

Boolean

==, !=

Only implemented for single dimensional sets, i.e. class FuzzySet

Inclusion

binary

Boolean

<=, =>

Only implemented for single dimensional sets, i.e. class FuzzySet

Proper Inclusion

binary

Boolean

<, >

Complement

unary

fuzzy relation

!

Intersectionm

binary

fuzzy relation

&

By Zadeh, Gödel

Unionm

binary

fuzzy relation

|

Intersectiona

binary

fuzzy relation

/

By Goguen

Uniona

binary

fuzzy relation

*

Intersectionb

binary

fuzzy relation

-

By Lukasiewicz

Unionb

binary

fuzzy relation

+

“very” alteration

unary

fuzzy relation

~

One of most common alteration. Corresponds to linguistic variable “very”

Root-Sum-Square

n-nary

Fuzzy relation

%

See 7.1.3.2

Table 2 – Supported operations on Fuzzy Sets and Fuzzy Relations

Examples

There are three samples coming with Fuzzy Framework. ReallySimpleExample is just a console application with minimum code which evaluates the statement that if someone is lanky, then he is also good in basket ball.

Figure 6 – FruitExample

The other two examples are available in Windows application SampleProject. Here you can toggle between FruitExample and TemperatureExample. To illustrate the fuzzy principles in practise, we will use these two examples in the following text, first one providing buying decisions whereas the second one deals with regulation of physical quantity. Both the examples incorporate a rule-based approach IF antedescent THEN consequent. This means that the fuzzy model is built on empiric experience rather than modelled mathematically [7].

The user interface lets you dynamically change the input values, defuzzification method, and thanks the C# evaluator published in [5] even the expression. You don’t have to recompile the code each time the expression changes.

3.1 Yen for some fruit

This example is from the domain of reasoning systems. Imagine you have a watch-dog robot which continuously scans offers on eBay, because you are too busy or laizy to do that yourself. You are hungry, and want the robot to buy some product. There are two prerequisites an offer must fulfil in order to qualify: The product must be fruit and the price must be low.

and a single output. Eventually, this output will be defuzzified into a crisp value on a scale of <-10,+10>, where -10 means definitely don’t buy, 0 stays for indifferent attitude, and 10 leads to sure deal.

3.2 Temperature Regulation

With this example, Fuzzy logic finds its use in fuzzy regulation. Fuzzy regulators are systems that actively regulate a dynamic environment.

Figure 7 – General Schema of Fuzzy Regulator

In the related literature, the typical example is a simple temperature controller with two inputs:

temperature (t)

change of temperature in time (∆t),

and two outputs:

heating intensity of variable electric heating element,

cooling intensity of variable-speed cooling fan,

Since we do not consider heating and cooling action at the same time, the intensities can be expressed by a single value from continuous interval output for cooling, for heating, and 0 for no action. Control is achieved through proper balance and control of these two active devices [7].

Figure 8 – Sample fuzzy temperature controller [7]

If we think about it in broad context, various applications can be considered a “regulator”, as long as it fires an action based on input conditions. Even the Fruit example can be considered a regulator because it keeps your fridge full of fruit for reasonable price. The same way, the temperature regulator can be considered a reasoning system since it decides what output action should be taken.

Goal

In the end, we don’t need anything else than function which returns a crisp output based on crisp input(s). Something like the relationship depicted in Figure 9 for our Fruit example.

Figure 9 – Input(s) ⇾ Output relationship of fuzzy inference system

As already mentioned, the advantage of fuzzy systems is that we don’t have to model this function mathematically. As Lofti A. Zadeh, the founder of fuzzy logic, expressed in his principle of incompatibility:

As the complexity of a system increases, our ability to make precise and yet significant statements about its behaviour diminishes until a threshold is reached beyond which precision and significance (or relevance) become almost mutually exclusive characteristics.[2]

Anyway, the challenge is to implement the function, either mathematically, or by means of fuzzy logic.

Relations

First of all, let’s focus on the Boolean crisp expression:

product is Fruit and price is cheap

Presuming that crisp condition price is cheap equals to price ≤ $5, this expression represents a two-dimensional set CheapFruits with members like Apple for $5, whereas other elements like Pear for $50 or Broccoli for $5 remain outside.

/p>

Figure 10 –Two-dimensional set CheapFruits.

In general terms, relation between sets A and B is defined as a subset of Cartesian product A x B. Here we have set Product {Apple, Pear, Blueberry, Broccoli, Carrot} and set Price {$5, $50, $500}. Hence the relation called CheapFruits is in fact a subset of Cartesian product Product x Price, i.e. as a list of ordered pairs {Apple for $5, Pear for $5}. Graphical representation of such relation is depicted in Figure 11a.

Now we can move forward towards fuzzy relations. If standard relation specifies that certain products are/aren’t somehow related to certain prices, then fuzzy relations extend this statement in the way that products are related to prices at some degree. The membership in the two-dimensional set CheapFruits is now represented by value within the interval of <0, 1>:

Fuzzy relation between sets A and B is defined as a subset of Cartesian product A x B x m, m <0, 1>. It finely describes the interaction between sets A and B. In our example, fuzzy relation CheapFruits is defined as a subset of Cartesian product Product x Price x m, and it can be reperesented by a set of ordered triplets. See Figure 11b. Why triplets rather than doublets? Since one dimension is “internally” reserved to store the membership of sets A and B in the relation. [10]

You can notice that crisp relation is just a special case of fuzzy relation. And even the crisp relation can be described by a triplet Product x Price x m, m m{0,1}. By the way when speaking about specialization, please also note that a set is just a special case of relation in a single-dimensional space. This is actually why our class FuzzySetinherits from class FuzzyRelation.

Rules

In fuzzy regulators, fuzzy relation is typically defined by a group of inference (production) rules. Each such group can be alternatively formulated as a list of if→ then conditions, by a table, or an expression:

if t is low and ∆t is negative then Heat
if t is low and ∆t is zero then Heat
if t is low and ∆t is positive then Heat
if t is high and ∆t is negative is then Cool
if t is high and ∆t is zero then Cool
if t is high and ∆t is positive then Cool
if t is correct and ∆t is negative then Heat
if t is correct and ∆t is positive then Cool
if t is correct and ∆t is zero then DoNothing

Code Example 2 – Set of rules written as expression using Mandani implication and RSS operator

To sum up, rules are just offer another way to define fuzzy relation. The easiest way for humans to write them down is to create a list of if→ then conditions. But eventually, such a list is always transferrable into a single expression. This expression describes what part play input variables in the overall fuzzy relation.

Figure 12 illustrates how rules are employed in the overall inference process. According to Table 3, the total number of rules is n = 9. There is a question mark in the box representing inference and defuzzification. As we will explain in the next section, there are three tasks in order to complete this box.

Action

There are the following three steps which must be performed in order to decide what action should eventually be fired:

Implication - also referred to as inference – to derive the consequent for each rule in the list. There is always at least one rule which defines the relationship between antedescent and consequent.

Sum of Rules – In case that there are several rules like in the Temperature example, we have to calculate a single total vector from the particular consequents.

Defuzzification – In case the total vector of consequents is represented by a fuzzy set, we have to defuzzify it in order to obtain a crisp value. This crisp value, usually a Boolean or an integer, is used as a decision in reasoning systems, or to influence the dynamic system in regulators. See the “action” feedback in Figure 7.

But first, why should be the total vector represented by another fuzzy set?

7.1.1 Simple inference

Let’s put the following question: What is the easiest way to decide whether an action should be performed? For the fruit example, the answer will be like following:

µAction(product, price) = min(µFruits(x), µCheap(y) ),

where product represents an offered product and price the inquired price. We have defined a two-dimensional set that contains products which are suitable for buying. Now, it would be really easy to specify that we will buy just those products where membership degree within this set is, let’s say, higher than 0.5:

IF µAction(product, price) > 0.5 THEN BuyIt ELSE LetItBe

Well, this is the simplest but least useful inference method, because it only reflects a single rule with the highest result. To make it more sophisticated, we have to add another, output dimension, into the overall fuzzy relation.

7.1.2 Output Fuzzy Set

Having n input dimensions, the aim is to extend the n-dimensional set fuzzy with another, output dimension. Based on the predefined rules, this dimension represents the consequents. We presume that for both examples, this output set will have universe U ∈ <-10, +10>:

where action represents any potential action you can take, and the resulting membership degree specifies how this action matches the inputs based on the predefined rules. We will find out later on why we used the formula just with „&” operators. The important fact for now is that the output dimension represents a sort of suitability to perform specific action.

Considering product and price given for the offer being evaluated, we can obtain the following graph when iterating through the universe of action. By other words, when we project relation into a single-dimensional fuzzy set considering constant inputs product and price and various action.

We can apply the following code in Fuzzy Framework to obtain the functional specification above, having x is the input variable from x-axis, inputProduct and inputPrice are the constant inputs, and y is the output variable:

The remaining task is to convert the shape you see in Figure 13 into a crisp value on the horizontal axis. Nice thing is that we can consider the graph in all its complexity - we can derive different conclusion from a singleton and from a flat trapezoidal number spanning half of universe, for example.

7.1.3 Sum of Rules

Now back to those three steps we introduced at the beginning of Section 7. Let’s turn our attention once more to Figure 12. As we can see, multiple rules are joint together in a single box denoted Inference/ Defuzzification. There are many ways how to combine these rules into a single output set which can be further defuzzified. Union and Root-Sum-Square are just the two of them.

7.1.3.1 Union

Since our Fruits example only has a single rule, let’s use Union to combine rules in Temperature example. First, we need three fuzzy sets representing three distinct consequent values. Similar to set BuyIt which was used in section 7.1.2 for Fruits, sets Cool, Heat and DoNothing will be defined with the following membership functions.

Figure 14 – Membership functions of sets Cool, DoNothing, and Heat.

Next, we can apply the Unionm operation to infer the output set from the consequents incurred from particular rules. If the consequents are defined like:

The maximum function in green represents the Unionm operation. And it will be explained latter why min is used for the implication.

Term min( ToBeCooled(t, ∆t), Cool(action) ) represents “cooling suitability”, much like the letter two terms represent “heating suitability” and “suitability to do nothing”, whereas the overall relation represents the overall “some action suitability”.

Union is the simplest summary operation where only the most intensive consequent value is taken into the consideration. To be more specific, three “cooling” rules fired will have the same strength as a single “cooling” rule, provided that the single rule has been fired intensively enough.

7.1.3.2 Root-Sum-Square (RSS)

RSS method is more complicated mathematically, yet much more suitable for use in fuzzy regulators. This method combines the effects of all rules [7].

First of all, unlike in the expression-based definition (see Code Example 2), we have to consider all nine rules separately, so that the final strength can be calculated more precisely:

ToBeCooled<sub>1</sub>

Next, instead of Unionm, we will apply a sum of square powers to calculate the resulting consequents:

Further operation we can apply on the resulting consequents is a sort of normalization, to be sure that their sum won’t exceed the range of <0,1>. Since the Centre-of-gravity defuzz method is typically used for defuzzification (see Section 7.1.5.3), and because we only care of the relative values, the easiest way is to divide all three results by the total number of rules. In Fuzzy Framework, RSS is implemented by means of C# operator „%“. The framework is able to make the final division automatically. Having the following predefined fuzzy sets:

Note that since we expect Centre-of-gravity being applied for defuzzification, singleton sets suffice for the output set.

7.1.4 Implication

It is obvious that the truth table of implication differs from that one of equivalence. But in inference systems, the letter is typically implemented in fact. This is because other circumstances firing the action besides the circumstances considered in the system are out of scope of the system. By other words, we can buy some fruit not only when there is a cheap fruit offered on the auction. We can pretty much buy it on our way from work as well, when coming across an attractive offer in a shop. But our system does not consider it. It only consider the equivalence cheap fruit ↔ buy it as well as not a cheap fruit ↔ no deal.

The typical implication operator s is considered counter-intuitive and unsuitable for the use in fuzzy systems. However, there is about 70 other alternatives to choose from [6]. We introduce the most frequent below.

7.1.4.1 Mandani Implication

This popular implication method is based on the assumption that maximum value of the consequent membership function cannot be higher than the membership degree of antedescent. This means that the graph representing membership degree of the consequent is “cut”, so that it does not overlap the antedescent [9].

Figure 15 – Example of inference based on Mandani implication

Common Gödel intersection can be employed to define the relationship between antedescent and consequent in a fuzzy relation:

Using the “&” intersection, we can be sure that µ will be only high if t is cold and action is heating, or when t is warm and action is cooling... In a nutshell, when there is some proper action responding to the given inputs. And this is, after all, equivalence. The equivalence we have written about at the beginning of section 7.1.4.

7.1.4.2 Larsen Implication

Larsen implication is very similar to Mandani implication. The only difference is that instead of Unionm, Uniona -algebraic AND by Goguen - is used.

Figure 16 – Example of inference based on Larsen implication

To employ Larsen implication instead of Mandani implication, we simply replace the “&” operator with “*” and that’s it:

7.1.5 Defuzzification

Before any inferred action can be performed, the output fuzzy set needs to be converted into a crisp value from the acceptable range. The process of approximation of fuzzy terms to the crisp value is called defuzzification [9].

Plenty of defuzzification methods have been invented in past. These methods can be divided into two basic groups: maxima methods and distribution methods. The first group suits better to reasoning systems like the one used in the fruit example, whereas the second one is intended for fuzzy regulators [8].

The following subsections contain summary of the most popular methods. The content of output set for given input values is considered as the source information for defuzzification. In Fuzzy Framework is defuzzification implmeneted in the following way:

7.1.5.1 Mean of Maximum

If the functional maximum is a peak, then the result is just the x-coordinate of the place where the peak resides. Otherwise it is the mean of flat maximum area.

Figure 17 – Mean of Maximum

The disadvantage is that only the maximum part of the membership function is considered. If there is another, lower rise on the other side of scale, it will be totally neglected.

7.1.5.2 Center of Maximum

Figure 18 – Center of Maximum

Unlike the previous method which works with the fuctional specification of µRelation and does not deal with particular rules, this method needs to access them one by one. The algoritm first calculates MeanOfMaximum for each consequent. Next, center of gravity (i.e. weighted average) is calculated.

where n is total number of consequents.

If fact, we are only interested in the value and the intensity of each consequent. This is why singletons are typically used as consequents. This method is often used with RSS (see Section 7.1.3.2) to calculate the resulting consequent from the particular rules. Otherwise, the algorithm would fail to give increased weighting to more rule votes per resulting consequent [7].

7.1.5.3 Center of Gravity

Figure 19 - Center of Gravity

This method employs integral calculus to obtain centre of gravity for the output fuzzy set.

Graphical Representation

Namespace FuzzyFramework.Graphics contains the following features which can help users to clearly see the internal structure of fuzzy relations they deal with. Both features accept object of type FuzzyRelation as well as type Defuzzification. In fact, Defuzzicication is nothing more than a wrapper for FuzzyRelation.

Membership Function Graph

The following code renders an image representing the graph into the specified graphics object:

where relation is an object of type FuzzyRelation, inputs are constant inputs specified in collection Dictionary<IDimension, decimal> and variableDimension is object type IDimension indicating which dimension has to be projected on the x-axis. Since it is only possible to draw 2D graphs, all other dimensions except variableDimension need to be specified in the inputs collection as constants.

Relation Tree

The following code fills the specified TreeView component with a hierarchy representing the specified relation:

I want to write a small code in c# using Fuzzy Logic framework where user will give input number between 0 to 10 and that numbers needs to be classified as Low, Moderate and High. Low is 0 to 3, Moderate is 3 to 7 and High is between 6 to 10. Using triangular membership function. Any guess how to do it I read some tutorials on Fuzzy Framework but couldn't get my problem fit in those. Following is code I wrote,

How should I proceed further now. I have assigned manually some score to opinion words(about 7000) on scale of 0 to 10 and I want to classify intensity of this opinion words as low, moderate or high using triangular function. Using fuzzy category of each word I have to further implement rules like if word1 is low and word2 is high then orientation is moderate .

Hi, excellent work in articel and code!Should not there be a sigmoid funtion in addition to the linear shoulder?It would give a more smooth start and end.Just take the the function and shift to up and to the right for a left "shoulder".

I am assuming you won't mind my citing your project in a paper I am writing. I have added Fuzzy Type 2 extensions to a different open-source framework but I hope to incorporate some of your excellent work in the overall design.

Hello, the class library cannot be started directly, indeed. You have to create your own project which will be either a window form application or a command line application. This project should be marked as the startup project in the Visual Studio solution. This main project should reference to the FuzzyFramework library. Similar to the demo applications attached at the top of the article.

The lack of simple to use AI libraries that have clear and concise examples is a real blocking point for their adoption into mainstream application development. This is may in part be due to the domain space, language and concepts being partially owned by Neuroscience.

This is an excellent article for addressing that issue alone and thank you very much for sharing.