Sample application (sources)  46K
Sample application (binaries)  27K
(Note: The application's sources may be obtained also as part of AForge.NET Framework)
Introduction: Fuzzy Computing
Fuzzy Logic, the core of the Fuzzy Computing, was introduced by professor Lofti A. Zadeh in 1965, as an alternative
approach to solve problems when the classical set theory and discrete mathematics, therefore the classical algorithms,
are unappropriate or too complex to use. To address a certain class of problems which a human being can solve easly, an approach that do not relies on mathematical
rigor and precision, but an approach fault tolerant that can handle partial truths.
The Fuzzy Computing can handle qualitative values instead of quantitative values. It can define the so called linguistic variables, instead of the classical numeric
variables, and can perform computing with theses variables, using fuzzy rules, simulating in a certain way the human reasoning processes. Incomplete or uncertain information
can be used for computing, like:
 "This man is tall";
 "That object is heavy";
 "Warm this food a little";
 "Increase the speed a lot".
In all those cases, the meaning of tall, heavy, a little and a lot are relevant to solve the problem, and not the precise
numerical value. To represent and compute with that kind of statements, daily used in human communication, we can use Fuzzy Sets.
Fuzzy Sets
The classical logic defines the classical sets. A set is a collection of objects of any kind. In a classical set, a given object belongs or not to a set.
There are operations that can be applied to sets: union, intersection and complementation. Defining a set is defining a membership function F(x), which
returns {0,1} for a x meaning that x belongs (1) or do not belongs (0) to the set.
Fuzzy approach extends the classical sets by letting the F(x) function returns a value in the [0,1] range. A given x value can belong to the set in some certain degree.
The classical behaviour can still be achieved by returning 0 or 1, but a membership function can return 0.6, for example, meaning there is a possibility of 0.6/1.0 that the value
belongs to the set. The below figure illustrates examples of a classical (a) and a fuzzy set (b).
Fuzzy sets are represented by their membership function. The next figure shows a fuzzy set A with its membership function. In this example, we can say that 10 does not belong to the set A,
20 belongs with a membership degree 0.5, 25 belongs with a membership degree of 0.75 and 40 belongs with a membership degree 1.
Fuzzy operators are defined to perform union, intersection and complementation. Those operators match the linguistic meaning of AND, OR and NOT, respectively.
The AND operation can be calculated with a function of the class TNorm. A commonly used AND operator can be the minimum among two membership functions. Let's consider
we have a 0.4 membership degree of a X value belonging to a fuzzy set A, and 0.6 membership degree of a Y value belonging to a fuzzy set B. The operation "(X is A) AND (Y is B)"
evaluates 0.4 using the minimum as TNorm operator.
The OR operation can be calculated with a function of the class TConorm, commonly represented by a maximum among the two operands. Considering the same values of the AND example,
the operation "(X is A) OR (Y is B)" evaluates 0.6 using the maximum as TConorm operator. Finally, the NOT unary operation can be calculated by using 1  the membership degree. So the statement "X is NOT A"
evaluates to 0.6.
Linguistic Variables
Linguistic variables represent the data used in fuzzy systems. They can store linguistic values instead of numerical values. A linguistic variable Distance, for example, can store the value near, which
has a meaning, not a numerical value as usual. The linguistic variable can be viewed as a set of fuzzy sets. Each fuzzy set is a value that the variable can be assigned to, and inside a variable the fuzzy sets are usually
called fuzzy labels. Below figure shows a linguistic variable Distance and its linguistic labels.
In this example, if X has a numerical value of 45, we can say the distance is far (with a membership 0.25) and is medium (with a membership of 0.75).
Fuzzy systems often have a set of linguistic variables to represent the meaning of the data manipulated, usually called fuzzy database.
Fuzzy Rules
To perform linguistic computations with the fuzzy database, fuzzy rules can be declared. They are very similar to natural language communication, like some
instructions from person to person:
 "IF distance IS far THEN speed IS high";
 "IF temperature IS hot AND pressure IS medium THEN potency IS low".
In a general form, fuzzy rules have an antecedent and a consequent, separated by a "THEN" statement. The antecedent is a conjunction of several fuzzy clauses (variable IS label)
with proper operators (AND, OR, NOT) among them. The consequent represent the action the system must execute if the antecedent is true (in any degree of membership).
 "IF (Var1 IS label11 OR Var1 IS label12) AND Var2 is label 21 ... THEN Var3 IS label31."
The computation of the membership of the antecedent is performed using the fuzzy operators AND, OR and NOT (TNorm and TConorm). The membership of the antecedent is usually called
firing strength of the rule to a given input value. Fuzzy systems often have a set of fuzzy rules that represents the behaviour of the system, known as the fuzzy rulebase.
Fuzzy Inference Systems
Fuzzy systems are becoming very popular nowadays, being used in control, expert systems, prediction and decision making, for example.
The main applications can be classified as Fuzzy Inference Systems (FIS). A FIS has two major components:
 Database  a repository of the linguistic variables;
 Rulebase  a repository of the knowledge the system represents, in the form of Fuzzy Rules.
The operation of a FIS can be simply described as:
 Setting input (numeric values) to all input linguistic variables.
 Those values will be represented internally by the system as membership values to each fuzzy set of each linguistic variable.
 Computing the firing strength of each rule. This is accomplished by computing the membership of each fuzzy clause and combining those memberships with fuzzy operators.
 For each fuzzy rule with non null firing strength, the rule output is to be calculated. There are several methods, like the Mamdani, which reflects the membership degree of the antecedent to the consequent.
 All outputs (fuzzy sets) are aggregated with a fuzzy operator of OR.
 Finally, the numeric output is calculated using a defuzzification method. The defuzzification is a technique to evaluate a numerical value from a fuzzy output.
The firing strength of each rule can be evaluated using the fuzzy operators AND, OR and NOT as described in Fuzzy Rules section.
To obtain the fuzzy output of each rule, the Mamdani method can be applied. This method propagates the degree of membership of
the antecedent of the rule (the firing strength) to the consequent of the rule. The next figure shows how a rule with a firing
strength of 0.7 constrains the fuzzy output.
Once all the rules' outputs are evaluated, the fuzzy output of the system is obtained by using an OR operator, aggregating all outputs in one single piecewise fuzzy set, as shown in the figure below.
The fuzzy output is the meaning of the system output, but, in many situations, a numerical output equivalent to the fuzzy one is required. In this case, a defuzzification method can be applied to the output.
A common method is the centroid, where the center of mass of the X axis is calculated. The equation used to calculate the center of mass is:
Where n is the number of steps, since this is a numerical approximation for the center of area. The higher n is, the more precise the center of mass will be.
The AForge.Fuzzy namespace
of AForge.NET Framework has a set of classes, which can perform all the tasks of a typical FIS. The base classes can be used separately or derived as well, to build Fuzzy Models that do not use the typical FIS architecture.
The Library
Since the AForge.NET 2.0 there is a Fuzzy namespace, with several
classes that can be used in fuzzy computing. The below class diagram shows all the classes and their relations.
 InferenceSystem  represents a complete fuzzy inference system,
with a data base, a rule base, a fuzzy output and a defuzzification method to calculate the numeric output.
 Database  a list with all the linguistic variables of the system.
 LinguisticVariable  variables used in fuzzy systems that can store a linguistic value (fuzzy set). They are set of labelled fuzzy sets.
 FuzzySet  the base of all fuzzy theory, represents a set where members have a degree of membership, usually between 0 and 1.
 IMembershipFunction  interface for the possible membership functions of the fuzzy sets.
 PicewiseLinearFunction  membership function composed by several linear functions.
 TrapezoidalFunction  typical membership function with a trapezoid's shape.
 Rulebase  a list with all the linguistic rules of the system.
 Rule  represents a fuzzy rule, generally formatted as ""if X is A and Y is B then Z is C". It contains fuzzy clauses and operators to combine these clauses.
 Clause  a fuzzy clause of the type "X is A", where "X" is a linguistic variable and "A" is a linguistic value to which the variable can be set.
 INorm  interface for the fuzzy norms, methods used to perform the "And" operations among fuzzy sets.
 ICoNorm  interface for the fuzzy conorms, methods used to perform the "Or" operations among fuzzy sets.
 MaximumCoNorm  conorm calculated using the maximum among two values.
 MiniumNorm  norm calculated using the minimum among two values.
 ProductNorm  norm calculated multiplying the two values.
 FuzzyOutput  the fuzzy output of a system, that can be used in its linguistic form or defuzzyfied.
 IDefuzzyfier  interface for the defuzyfication methods used to extract the numeric output from a fuzzy output.
 CentroidDefuzzifier  defuzyfication which calculates the centroid of the fuzzy output.
The Auto Guided Vehicle Example
To demonstrate the utilization of the AForge.Fuzzy namespace,
we can address a part of a classical robotics application: the navigation of an auto guided vehicle.
This problem has many faces, so we will be focusing in the avoid obstacles phase. Our goal is to keep a virtual robot away from walls, navigating in corridors. We will consider
the distance sensors in the below figure: "F" to frontal distance, "L" to left distance and "R" to right distance. The robot will keep constant speed, and our only form of control will be
the direction angle of the robot. Lets consider the angle zero will go forward, negative values will turn left and positive values will turn right.
We will use three linguistic variables to represent the inputs (distances) and one linguistic variable to represent output (angle). Then seven fuzzy rules were designed to
avoid the walls and keep the robot in the middle of the corridors. The code to create and initialize the inference system is presented.
// initialization of the Fuzzy Inference System
void InitFuzzyEngine( )
{
// linguistic labels (fuzzy sets) that compose the distances
FuzzySet fsNear = new FuzzySet( "Near", new TrapezoidalFunction(
15, 50, TrapezoidalFunction.EdgeType.Right ) );
FuzzySet fsMedium = new FuzzySet( "Medium", new TrapezoidalFunction(
15, 50, 60, 100 ) );
FuzzySet fsFar = new FuzzySet( "Far", new TrapezoidalFunction(
60, 100, TrapezoidalFunction.EdgeType.Left ) );
// right Distance (Input)
LinguisticVariable lvRight = new LinguisticVariable( "RightDistance", 0, 120 );
lvRight.AddLabel( fsNear );
lvRight.AddLabel( fsMedium );
lvRight.AddLabel( fsFar );
// left Distance (Input)
LinguisticVariable lvLeft = new LinguisticVariable( "LeftDistance", 0, 120 );
lvLeft.AddLabel( fsNear );
lvLeft.AddLabel( fsMedium );
lvLeft.AddLabel( fsFar );
// front Distance (Input)
LinguisticVariable lvFront = new LinguisticVariable( "FrontalDistance", 0, 120 );
lvFront.AddLabel( fsNear );
lvFront.AddLabel( fsMedium );
lvFront.AddLabel( fsFar );
// linguistic labels (fuzzy sets) that compose the angle
FuzzySet fsVN = new FuzzySet( "VeryNegative", new TrapezoidalFunction(
40, 35, TrapezoidalFunction.EdgeType.Right ) );
FuzzySet fsN = new FuzzySet( "Negative", new TrapezoidalFunction(
40, 35, 25, 20 ) );
FuzzySet fsLN = new FuzzySet( "LittleNegative", new TrapezoidalFunction(
25, 20, 10, 5 ) );
FuzzySet fsZero = new FuzzySet( "Zero", new TrapezoidalFunction(
10, 5, 5, 10 ) );
FuzzySet fsLP = new FuzzySet( "LittlePositive", new TrapezoidalFunction(
5, 10, 20, 25 ) );
FuzzySet fsP = new FuzzySet( "Positive", new TrapezoidalFunction(
20, 25, 35, 40 ) );
FuzzySet fsVP = new FuzzySet( "VeryPositive", new TrapezoidalFunction(
35, 40, TrapezoidalFunction.EdgeType.Left ) );
// angle
LinguisticVariable lvAngle = new LinguisticVariable( "Angle", 50, 50 );
lvAngle.AddLabel( fsVN );
lvAngle.AddLabel( fsN );
lvAngle.AddLabel( fsLN );
lvAngle.AddLabel( fsZero );
lvAngle.AddLabel( fsLP );
lvAngle.AddLabel( fsP );
lvAngle.AddLabel( fsVP );
// the database
Database fuzzyDB = new Database( );
fuzzyDB.AddVariable( lvFront );
fuzzyDB.AddVariable( lvLeft );
fuzzyDB.AddVariable( lvRight );
fuzzyDB.AddVariable( lvAngle );
// creating the inference system
IS = new InferenceSystem( fuzzyDB, new CentroidDefuzzifier( 1000 ) );
// going Straight
IS.NewRule( "Rule 1", "IF FrontalDistance IS Far THEN Angle IS Zero" );
// going Straight (if can go anywhere)
IS.NewRule( "Rule 2", "IF FrontalDistance IS Far AND RightDistance IS Far AND " +
"LeftDistance IS Far THEN Angle IS Zero" );
// near right wall
IS.NewRule( "Rule 3", "IF RightDistance IS Near AND LeftDistance IS Medium " +
"THEN Angle IS LittleNegative" );
// near left wall
IS.NewRule( "Rule 4", "IF RightDistance IS Medium AND LeftDistance IS Near " +
"THEN Angle IS LittlePositive" );
// near front wall  room at right
IS.NewRule( "Rule 5", "IF RightDistance IS Far AND FrontalDistance IS Near " +
"THEN Angle IS Positive" );
// near front wall  room at left
IS.NewRule( "Rule 6", "IF LeftDistance IS Far AND FrontalDistance IS Near " +
"THEN Angle IS Negative" );
// near front wall  room at both sides  go right
IS.NewRule( "Rule 7", "IF RightDistance IS Far AND LeftDistance IS Far AND " +
"FrontalDistance IS Near THEN Angle IS Positive" );
}
After the system is initialized, we can realize the inference setting inputs and querying for outputs:
...
// setting inputs
IS.SetInput( "RightDistance", Convert.ToDouble( txtRight.Text ) );
IS.SetInput( "LeftDistance", Convert.ToDouble( txtLeft.Text ) );
IS.SetInput( "FrontalDistance", Convert.ToDouble( txtFront.Text ) );
// evaluating outputs
try
{
double NewAngle = IS.Evaluate( "Angle" );
...
}
catch ( Exception )
{
...
}
To test the navigation, a simple simulation environment was created, where the user can draw several corridors and simulate the robot responses. The below picture shows this simulation environment working.
Conclusion
Fuzzy computing can be used to easily solve problems that seems very complex to the traditional mathematical modelling. They are inspired in the human way to reason with
partial truths and imprecision. They can perform computations with words, with meaning. AForge.NET Framework has a namespace with classes that can be used to build fuzzy models and systems. This article introduced
the fuzzy theory, the AForge.Fuzzy namespace and showed
that it can be used to easily solve a part of an auto guided vehicle navigation, the avoiding of collisions.


