MyFuzzy.pm - The Fuzzy Library

with FuzzyWeb demo here!

by Roland Stelzer, 2003

MyFuzzy.pm is an open source fuzzy logic library developed in order to facilitate the implementation of fuzzy control systems. First of all, an appropriate programming language needed to be found for the development of fuzzy control applications. Perl was the chosen programming language for the following reasons:

 

www.perl.org (2003)

 

Before developing the library, the Internet was searched for existing solutions. The only Fuzzy Logic Library written in Perl and freely was “AI::Fuzzy” from CPAN (Comprehensive Perl Archive Network). Using this Perl module, it was possible to design triangular fuzzy sets and carry out the fuzzification of values. However, there are no inference mechanisms or defuzzification methods included in this module. Therefore, a new library needed to be developed, with all the functions necessary for a fuzzy control application.

 

The central element used in the library is the fuzzy set. In MyFuzzy.pm the fuzzy sets are discrete sets. This means, that they consist of a finite number of value pairs (x,m(x)) with x Î {0,1,2,…,n}. The value n is called the resolution of the fuzzy term.

 

All fuzzy operations in MyFuzzy.pl round every crisp input – which has to be fuzzified – to a natural number in {0,1,…,n}. The membership values are only defined for this set of natural numbers.

 


A fuzzy set A~ with a resolution n = 4 is shown in the figure 1.

Figure 1: Discrete Fuzzy Sets in MyFuzzy.pm

 

The crisp input value 1.6 will be rounded to 2, because membership values are only defined for natural numbers. The result of the fuzzification is m(round(1.6)) = m(2) = 1.

 

When a resolution for a fuzzy term is to be found, it needs to be considered whether maximum precision or maximum performance is required. If the resolution is low, all fuzzy operations work very fast, but the level of precision for the membership values is not very high. If precision is more important than performance, a high resolution value should be taken.

 

Definition of a Fuzzy Set

To define the fuzzy set A~ from the example above using MyFuzzy.pm the following code is needed.

$A->{0} = 0;

$A->{1} = 0.5;

$A->{2} = 1;

$A->{3} = 0.5;

$A->{4} = 0;

 

For larger fuzzy terms, especially with a higher resolution, defining all the values would be a lot of work. With the function interpolate this work can be reduced to a minimum. Only the prominent values of the fuzzy set need to be defined. The in-between values will be filled out automatically by linear interpolation. The result of the following code is exactly the same as that of the last example.

 


The number of value pairs is unlimited. For this reason you cannot only define standard fuzzy sets like triangles or trapezoids.

$A->{0} = 0;

$A->{2} = 1;

$A->{4} = 0;

$A = interpolate($A);

 

The examples above always employ a universe of discourse of [0,n]. In real life the input values will not always be of this form. A good example of this is a thermometer that measures the temperature outside in °C. The temperature values for this will be in the interval [-50,50]. The MyFuzzy.pm library always works with x-values from 0 to n. Therefore transformation is needed. The following example shows the usage of the function transform_fuzzyterm.

 

The fuzzy set B~ represents the linguistic term “hot” for outside temperature.

Assumption: Resolution n = 100

$B->{20} = 0;

$B->{35} = 1;

$B->{50} = 1;

$B = transform_fuzzyterm(-50,50,$B);

$B = interpolate($B);

 

The function transform_fuzzyterm transforms the x-values from the interval [-50,50] into the interval [0,n]: in this case [0,100].

Figure 2: Interpolation in MyFuzzy.pm


Applying the Fuzzy Inference System

When all fuzzy sets are defined and transformed, the fuzzy rules have to be applied. A fuzzy rule contains a condition and a conclusion. The condition is a single fuzzy term or a combination of fuzzy terms, such as union (OR) or intersection (AND).

 

The following example demonstrates how a simple fuzzy rule is applied. The procedure has to be done for every rule of the fuzzy system.

 

Facts:                Room temperature = 25 °C

                          Outside temperature = 30 °C

Rule:                  IF room_temperature IS hot

            AND outside_temperature IS hot

                 THEN aircondition IS full (degree of support = 1)

Fuzzification

In this example we use the fuzzy set of the previous example again. To keep the example simple, the same fuzzy set is used for both the inside and outside temperature. Assumed that it is still transformed into x-values of the interval [0,n] with a resolution of n=100 again. The name of the fuzzy sets in the Perl code example is: $room_temperature_hot and $outside_temperature_hot. It is also necessary to transform the input values. The function transform_value is used to transform the temperature values (inside: 25°C, outside: 30°C) to the internal representation - a natural number in the interval [0,n].

$room_temperature = transform_value(-50,50,25);

$outside_temperature = transform_value(-50,50,30);

 

The new “internal” room temperature value is now 75, outside temperature is 80. The following command returns the corresponding membership values.

$mv_hot_room_temperature =

$room_temperature_hot->{$room_temperature};

$mv_hot_outside_temperature =

$outside_temperature_hot->{$outside_temperature};

 

The variables now contain the fuzzified temperature:

mroom_hot(75) = 0.33           => $mv_hot_room_temperature = 0.33

moutside_hot(80) = 0.66        => $mv_hot_outside_temperature = 0.66


Aggregation

Because the antecedent of the rule combines two statements using the AND operator, aggregation is necessary. This is done with the function fuzzy_aggregation. The specified parameters are the logical operator and the membership values taken from the fuzzification statements.

 

For the antecedent “room_temperature IS hot AND outside_temperature IS hot” the following Perl statement is needed to get the activation level of the rule:

$activation = fuzzy_aggregation( "AND",

   $mv_hot_room_temperature,

   $mv_hot_outside_temperature);

 

From this we get the following degree of activation:

actRule = min(0.33,0.66) = 0.33 => $activation = 0.33

 

Implication

The implication is done with the function fuzzy_implication. The first parameter of the function is the used t-norm. In the example, the minimum operator is used. The second parameter is the fuzzy set is given by the conclusion; in this case: “aircondition IS full”. The last parameter is the degree of rule activation, multiplied by the degree of support.

 

$rule_result_fuzzyset =

         fuzzy_implication( "min",

                           $aircond_full,

                           1*$activation));

Figure 3: Implication in MyFuzzy.pm

 

Accumulation

If there are multiple rules, a result fuzzy set exists for each of them after their implication. These fuzzy sets now need to be accumulated to a single result fuzzy set for the whole fuzzy inference system.

 

The output accumulation is done with the function fuzzy_accumulation, which combines two fuzzy sets using an s-norm. We start with an output fuzzy set following the membership function m(x) = 0. The initialisation is done with the function init_fuzzyterm.

$aircond_result_fuzzyset = init_fuzzyterm(0);

 

Following this, the accumulation can take place. The following statement has to be done for every rule with a conclusion concerning the linguistic variable “aircondition”. The parameters describe the used s-norm, the result fuzzy set, and the fuzzy set to be accumulated (the result of the implication).

$aircond_result_fuzzyset =

           fuzzy_accumulation( "max",

                          $aircond_result_fuzzyset,

                              $rule_result_fuzzyset);

 

After applying all the rules of the fuzzy system in this way, the variable $aircond_result_fuzzyset contains the result fuzzy set.

 

Defuzzification

The last step is to defuzzify the output fuzzy set $aircond_result_fuzzyset. This can be done with the function defuzzify. The first parameter needed, specifies the defuzzification method to use. The second parameter is the fuzzy set to be defuzzified.

$aircond_crisp =

defuzzify(“CoG”, $aircond_result_fuzzyset);

 

The variable $aircond_crisp now contains a natural number within the interval [0,n], where n is the resolution of the fuzzy set. To get the real value required to regulate the air condition, a retransformation is needed.

 


The function retransform_value is the inversion of the function transform_value that was used at the beginning of the inference process. As a result a given value from the internal representation with the universe of discourse [0,n], is retransformed into the user-defined interval for the fuzzy set [min,max].

 

$aircond_crisp =

retransform_value(0, 100, $aircond_crisp);

 

In the previous example, the retransformation will not have an effect because the resolution n is 100, and the possible values for the air condition are also between 0 and 100. Therefore, both intervals are [0,100].


MyFuzzy.pm - Complete Function Reference

round (number)

Description:              Rounds a floating-point number to an integer

Parameters:             number ... numeric value to round mathematically

Return value:            Rounded value

Example:                  round (12.54);

                                 returns 13

 

min (val_1,..,val_n)

Description:              Determines the minimum of given values

Parameters:             val_x ... numeric values

Return value:            smallest of the parameter values

Example:                  max (12,54,100.3,-23);

                                 returns –23

 

max (val_1,..,val_n)

Description:              Determines the maximum of given values

Parameters:             val_x ... numeric values

Return value:            largest of the parameter values

Example:                  max (12,54,100.3,-23);

                                 returns 100.3

 

fuzzy_aggregation (operator, val_1,..,val_n)

Description:              Aggregates the given values using the specified logic operator.

Parameters:             operator ... ‘AND‘, ‘OR‘

                                 val_x ... numeric values of the interval [0,1] (results of fuzzification)

Return value:            for operator ‘AND‘: Minimum of the values

                                 for operator ‘OR‘: Maximum of the values

Example:                  fuzzy_aggregation (‘AND‘, 0.54, 0.8);

                                 returns 0.54

 

transform_value (min, max, val)

Description:              Transforms a given value from a given universe of discourse [min,max] into the internal representation of the system with a universe of discourse defined by the resolution of the variables of the fuzzy system [0,resolution].

Parameters:             min ... lower limit of the source universe of discourse

                                 max ... upper limit of the source universe of discourse

                                 val ... numeric values to transform into the internal representation [0,resolution]

Return value:            transformed value (internal representation of the given value)

Example:                  A value of 30°C for the outside temperature in the universe of discourse [-50,50] should be transformed into the interval [0,100] (The resolution of the fuzzy sets is assumed to be 100). transform_value (-50,50,30);

                                 returns 80

 

retransform_value (min, max, val)

Description:              This is the inversion of the function transform_value. So a given value from the internal representation with the universe of discourse [0,resolution] is retransformed into the user defined interval for the fuzzy set [min,max].

Parameters:             min ... lower limit of the source universe of discourse

                                 max ... upper limit of the source universe of discourse

                                 val ... numeric values to retransform into [min,max]

Return value:            retransformed value

Example:                  We assume a resolution of 100. The value 80 will be retransformed into the universe of discourse [-50,50] for the outside temperature.

                                 retransform_value (-50,50,80);

                                 returns 30(°C)

 

init_fuzzyterm (val)

Description:              Creates a new fuzzy term, with  m(x) = val for every x in {0,1,..n}.

Parameters:             val ... membership value out of [0,1]

Return value:            Pointer to the initialised fuzzy term.

Example:                  We assume a resolution of 4.

                                 $newterm = init_fuzzyterm(0.5);

                                 The initialised fuzzy set consists of {(0,0.5),(1,0.5),(2,0.5),(3,0.5),(4,0.5)}.

 

transform_fuzzyterm (min,max,fuzzyterm)

Description:              Transforms a previously defined fuzzyterm into its internal representation, depending on the resolution.

Parameters:             min ... lower limit of the source universe of discourse

                                 max ... upper limit of the source universe of discourse

                                 fuzzytem ... pointer to the previously defined fuzzy term

Return value:            Pointer to the transformed fuzzy term.

Example:                  $B = transform_fuzzyterm(-50,50,$A);

 

interpolate (fuzzyterm)

Description:              If only some membership values are defined, this function interpolates between them.

Parameters:             fuzzytem ... pointer to a fuzzy set, with some defined members

Return value:            Fully defined fuzzy term.

Example:                  $A = interpolate($A);

 

fuzzy_and (fuzzyterm_1, …, fuzzyterm_n)

Description:              Combines the given fuzzy sets using the minimum operator.

Parameters:             fuzzyterm_x ... pointers to fuzzy terms

Return value:            Pointer to the result fuzzy term.

Example:                  $C = fuzzy_and($A,$B);

 

fuzzy_or (fuzzyterm_1, …, fuzzyterm_n)

Description:              Combines the given fuzzy sets using the maximum operator.

Parameters:             fuzzyterm_x ... pointers to fuzzy terms

Return value:            Pointer to the result fuzzy term.

Example:                  $C = fuzzy_or($A,$B);

 

fuzzy_accumulation (s-norm, fuzzyterm_1, …, fuzzyterm_n)

Description:              Combines the given fuzzy sets using the specified s-norm.

Parameters:             s-norm … ‘max’ for maximum or ‘bdsum’ for bounded sum.

                                 fuzzyterm_x ... pointers to fuzzy terms

Return value:            Pointer to the result fuzzy term.

Example:                  $C = fuzzy_accumulation(‘bsum’,$A,$B);

 

fuzzy_implication (t-norm, fuzzyterm, val)

Description:              Combines the given value with every membership value of the given fuzzy set using the specified t-norm.

Parameters:             t-norm … ‘min’ for minimum or ‘prod’ for algebraic product.

                                 fuzzytem ... pointer to a fuzzy term

                                 val … value between 0 and 1

Return value:            Pointer to the result fuzzy term.

Example:                  $C = fuzzy_implication(‘min’,$A,0.7);

 

defuzzify (method, fuzzyterm)

Description:              Defuzzifies the given fuzzy set using the specified defuzzification method.

Parameters:             method … ‘CoG’ for centre of gravity, ‘CoA’ for centre of area, ‘MoM’ for mean of maxima.

                                 fuzzytem ... pointers to fuzzy set to defuzzify

Return value:            crisp value (natural number within the interval [0,n], where n is the resolution of the fuzzy set).

Example:                  $crisp = defuzzify(‘CoG’,$A);

 

read_dev_file (filename)

Description:              Reads the value out of the specified device file.

Parameters:             filename … name of the device file.

Return value:            value stored in the device file.

Example:                  $value = read_dev_file(’25.dev’);

 

write_dev_file (filename, $value)

Description:              Writes the specified value into a device file.

Parameters:             filename … name of the device file.

                                 $value … value to store into the device file.

Return value:            none

Example:                  write_dev_file(’25.dev’, 17.5);

calc_next_input (varname, vartype)

Description:              Fetches the simulation input for the current simulation cycle out of the database.

Parameters:             varname … name of the linguistic variable

                                 vartype … ‘input’ or ‘output’ (usually it will be ‘input’).

Return value:            simulation input value.

Example:                  $value = calc_next_input(’temperature’, ‘input’);

 

store_value (varname, vartype)

Description:              Stores the input/output value of a running fuzzy system into the database.

Parameters:             varname … name of the linguistic variable

                                 vartype … ‘input’ or ‘output’.

Return value:            none

Example:                  store_value(’heating’, ‘output’);

 

delete_output ()

Description:              Deletes the output values from previous runs in the database.

Parameters:             none

Return value:            none

Example:                  delete_output(’heating’, ‘output’);

 

FuzzyWeb is a web application used to design fuzzy systems graphically. The source code of a designed fuzzy inference system can be generated automatically; it uses MyFuzzy.pm. Click the logo to try FuzzyWeb.

SourceForge.net Logo