## 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.

Figure 3 – a) Singleton b) Triangular fuzzy number c) Trapezoidal fuzzy number d) Bell-shaped, piecewise quadratic fuzzy number

The following is a summary of essential facts about fuzzy numbers:

- The interval(s) where µ=1 are called kernel.
- The interval(s) where µ>0 are called support.
- 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:

FuzzySet fruits = new DiscreteSet(product, "Fruits");
FuzzySet cheap = new RightLinearSet(price, "Cheap", 5, 50);
FuzzySet lowTemperature = new RightQuadraticSet(temperature, "Low temperature",
10, 15, 20);

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 i_{1} and i_{m} 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 | | ! | |

Intersection_{m} | binary | fuzzy relation | | & | By Zadeh, Gödel |

Union_{m} | binary | fuzzy relation | | | |

Intersection_{a} | binary | fuzzy relation | | / | By Goguen |

Union_{a} | binary | fuzzy relation | | * |

Intersection_{b} | binary | fuzzy relation | | - | By Lukasiewicz |

Union_{b} | 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.

Normally, you would use a rule like

**IF** (product ∈ Fruit) and (price < $10) **THEN** BuyIt

Since we work with fuzzy sets, the rule can be i bit more relaxed:

**IF** (product is Fruit) and (price is cheap) **THEN** BuyIt

There are two inputs:

- Product – a nominal type of product.
*Product ∈ {Apple, Pear,…Broccoli},* - Price – offered price in $.
*Price ∈ <$0, $500>,*

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.

Figure 11 –

a) Crisp relation CheapFruits between sets Product and Price

b) Fuzzy relation between the same sets.

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 *FuzzySet*** **inherits 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 1 – Set of rules expressed by list of conditions

t ∆t | t is low | t is correct | t is high |

∆t is negative (t drops) | Heat | Heat | Cool |

∆t is zero (t is constant) | Heat | Do nothing | Cool |

∆t is positive (t raises) | Heat | Cool | Cool |

Table 3 – Set of rules expressed in table

((lowTemperature & fallingTemperature) & heat) %
((lowTemperature & constantTemperature)& heat) %
((lowTemperature & risingTemperature)& heat) %
((correctTemperature & fallingTemperature)& heat) %
((correctTemperature & constantTemperature) & doNothing) %
((correctTemperature & risingTemperature)& cool) %
((highTemperature & fallingTemperature)& cool) %
((highTemperature & constantTemperature)& cool) %
((highTemperature & risingTemperature)& cool)

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.

Figure 12 – Inference process in the example with temperature regulation.

## 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>:

Dimension action = new ContinuousDimension("Action", "", "", -10, +10);

Based on the rule used in the fruit example, we will build the overall relation in the following way:

µ<sub>Relation</sub>(product, price, action) = min(µ<sub>Fruits</sub>(product), µ<sub>Cheap</sub>(price), µ<sub>BuyIt</sub>(action) )

where* *set *BuyIt* is a fuzzy set defined on the output dimension:

FuzzySet buyIt = new LeftQuadraticSet(action, "Buy it!", 3, 5, 10);

Using our Fuzzy Framework, the expression will look like:

FuzzyRelation relation = cheap & fruits & buyIt;

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 *produc*t 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*.

Figure 13 – Two-dimensional projection of relation *µ*_{Relation}(product, price, action) for constant *product* and *price*.

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:

double y = relation.IsMember(
new Dictionary<IDimension, decimal>{
{ product, inputProduct },
{ weight, inputPrice },
{ action, x}
}
);

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 *Union*_{m} operation to infer the output set from the consequents incurred from particular rules. If the consequents are defined like:

**ToBeCooled** = (t is high) or (t is correct and ∆t is positive), Cool ∈ <0,1>
**ToBeHeated** = (t is low) or (t is correct and ∆t is negative), Heat ∈ <0,1>
**ToDoNothing** = (t is correct and ∆t is zero), DoNothing ∈ <0,1>

Then the sum can be built by means of *Union*_{m} in the following way:

µ<sub>Relation</sub>(t, ∆t, action) = **max(**
min( ToBeCooled(t, ∆t), Cool(action) ),
min( ToBeHeated(t, ∆t), Heat(action) ),
min( ToDoNothing(t, ∆t), DoNothing(action) )
)

The maximum function in green represents the *Union*_{m} 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> = (t is high and ∆t is positive)
ToBeCooled<sub>2</sub> = (t is high and ∆t is negative)
ToBeCooled<sub>3</sub> = (t is high and ∆t is zero)
ToBeCooled<sub>4</sub> = (t is correct and ∆t is positive)
ToBeHeated<sub>1</sub> = (t is low and ∆t is positive)
ToBeHeated<sub>2</sub> = (t is low and ∆t is negative)
ToBeHeated<sub>3</sub> = (t is low and ∆t is zero)
ToBeHeated<sub>4</sub> = (t is zero and ∆t is negative)
ToDoNothing<sub>1</sub> = (t is correct and ∆t is zero)

Next, instead of *Union*_{m}, we will apply a sum of square powers to calculate the resulting consequents:

ToBeCooled<sub>t</sub> = ToBeCooled<sub>1</sub>^2 + ToBeCooled<sub>2</sub>^2 + ToBeCooled<sub>3</sub>^2 + ToBeCooled<sub>4</sub>^2
ToBeHeated<sub>t</sub> = ToBeHeated<sub>1</sub>^2 + ToBeHeated<sub>2</sub>^2 + ToBeHeated<sub>3</sub>^2 + ToBeHeated<sub>4</sub>^2
ToDoNothing<sub>t</sub> = ToDoNothing<sub>1</sub>^2

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:

lowTemperature = new RightQuadraticSet(temperature, "Low t", 10, 15, 20);
highTemperature = new LeftQuadraticSet(temperature, "High t", 20, 25, 30);
correctTemperature = new BellSet(temperature, "Correct t", 20, 5, 10);
fallingTemperature = new RightQuadraticSet(deltaTemperature, "∆t<0", -3, -1, 0);
risingTemperature = new LeftQuadraticSet(deltaTemperature, "∆t>0", 0, 1, 3);
constantTemperature = new BellSet(deltaTemperature, "∆t=0", 0, 1, 3);
heat = new SingletonSet(action, "Heat", 10);
cool = new SingletonSet(action, "Cool", -10);
doNothing = new SingletonSet(action, "Do nothing", 0);

the overall relation can be specified in the following way:

FuzzyRelation relation =
((lowTemperature & fallingTemperature) & heat) %
((lowTemperature & constantTemperature)& heat) %
((lowTemperature & risingTemperature)& heat) %
((correctTemperature & fallingTemperature)& heat) %
((correctTemperature & constantTemperature) & doNothing) %
((correctTemperature & risingTemperature)& cool) %
((highTemperature & fallingTemperature)& cool) %
((highTemperature & constantTemperature)& cool) %
((highTemperature & risingTemperature)& cool);

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:

µ<sub>Relation</sub>(product, price, action) = min(µ<sub>Fruits</sub>(product), µ<sub>Cheap</sub>(price), µ<sub>BuyIt</sub>(action) )

As we could already see in Section 7.1.2., Mandani implication also was one alternative used in the fuzzy temperature regulator:

µ<sub>Relation</sub>(t, ∆t, action) = max(
**min(**µ<sub>ToBeCooled</sub>(t, ∆t), µ<sub>Cool</sub>(action) **)**,
**min(**µ<sub>ToBeHeated</sub>(t, ∆t), µ<sub>Heat</sub>(action) **)**,
**min(**µ<sub>ToDoNothing</sub>(t, ∆t), µ<sub>DoNothing</sub>(action) **)**
)

The reader can note that we consider the implication a standard part of fuzzy relation:

((lowTemperature & fallingTemperature) & heat) %
((lowTemperature & constantTemperature) & heat) %
…

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 *Union*_{m}, *Union*_{a} -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:

((lowTemperature & fallingTemperature) * heat) %
((lowTemperature & constantTemperature) * heat) %
…

#### 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:

Defuzzification result = new MeanOfMaximum(
relation,
new Dictionary<IDimension, decimal>{
{ product, inputProduct },
{ price, inputPrice }
}
);
double crispValue = result.CrispValue;
double crispValue = result.CrispValue;

Where *MeanOfMaximum *is the name of defuzzification method. Another alternative to obtain this class is to invoke a factory:

DefuzzificationFactory.GetDefuzzification(DefuzzificationMethod.MeanOfMaximum, ...)

#### 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:

System.Drawing.Graphics graphics;
RelationImage img = new RelationImage(relation, inputs, variableDimension);
img.DrawImage(graphics);

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:

System.Windows.Forms.TreeView treeViewRelation;
System.Windows.Forms.PictureBox pictureBoxGraph;
System.Windows.Forms.Label lblGraphCaption;
TreeSource ts = new TreeSource(relation);
ts.BuildTree(treeViewRelation, pictureBoxGraph, lblGraphCaption);

Once a tree node is selected, specified picture box and label are filled in with detailed information about the selected subrelation.

## License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

## Resources

- Bojadziev, G., Bojadziev, M.: Fuzzy Logic for Business, Finance and Management. 2nd edition. Singapore: World Scientific Publishin, 2007. ISBN:13 978-981-270-649-2
- Cirstea, M. et al: Neural and Fuzzy Logic Control of Drives and Power Systems. Newnes, 2002.1
^{st} Edition. ISBN: 9780080497365 - Gafa, C.: FuzzScript - A Fuzzy Logic Control Language, The Code Project, 2009. Online: http://www.codeproject.com/KB/recipes/FuzzScript.aspx
- Hanzzoid: A C# Class for Complex Polynomials, The Code Project, 2007, Online: http://www.codeproject.com/KB/cs/PolyLib.aspx
- Hauser, K. D.: Evaluate C# Code (Eval Function), The Code Project, 2005. Online: http://www.codeproject.com/KB/cs/evalcscode.aspx
- Jantzen, J.: Tutorial On Fuzzy Logic. Online: http://www.iau.dtu.dk/~jj/pubs/logic.pdf
- Kaehler, S. D.: Fuzzy Logic Tutorial. Encoder: The Newsletter of the Seattle Robotic Society. Online: http://www.seattlerobotics.org/encoder/mar98/fuz/flindex.html
- Leekwijck, L., Kerre, K.: Defuzzification: Criteria and Classification. In Fuzzy Sets and Systems. Vol.108 i. 2, Dec 2008. ISSN:0165-0114
- Modrlák, O.: Fuzzy řízení a regulace. Technical University in Liberec, Faculty of Mechatronics, Informatics and Interdisciplinary Studies. 2002. Online: http://www.fm.tul.cz/~krtsub/fm/tr2/tar2_fuz.pdf
- Rydval, S. Základy Fuzzy Logiky. NaWEBKa. 2005. Online: http://www.rydval.cz/phprs/view.php?cisloclanku=2005061701

This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.