A fuzzy logic C++ library
|
NAVIGATION
The MEMBFUNC class holds a membership function, that defines what is called a "fuzzy set". In this library, membership functions can be defined only by line segments, aka piecewise linear function.
A function has a name, and always has a maximum and minimum value. It can be empty (0 points), but that does not mean it has a null value.
A copy-constructor is provided, as well as an assignement operator and comparison operators ("==" and "!=" ).
Several constructors are provided to build membership functions, but you can afterwards redefine any function using one of the "Set()" functions. Actually, this is what the constructors do. In the following example, we'll use one or another of these.
For example, you can create an empty function, then assigning to it a name, then assigning to it a predefined shape:
MEMBFUNC mf; mf.SetName( "cold" ); mf.Set( MF_HL, 10, 20 );
which is strictly identical to:
MEMBFUNC mf( "cold" ); mf.Set( MF_HL, 10, 20 );
which in turn is strictly identical to:
MEMBFUNC mf( MF_HL, "cold", 10, 20 );
You can also assign a constant fuzzy value to a function, with the code:
MEMBFUNC mf; mf.Clear( 0.3 );
which is equivalent to:
MEMBFUNC mf( 0.3 );
(Here, the function will be assigned a default name).
The MEMBFUNC::Clear() function removes all the points, and assigns the constant value to the function (0 is the default value).
Five predefined functions are available, through constructors and the enum slifis::EN_MF_TYPE :
These will produce classical "0 to 1" functions:
MEMBFUNC mf1( MF_HL, "cold", 10, 20 ); MEMBFUNC mf2( MF_LH, "hot", 40, 70 ); MEMBFUNC mf3( MF_LL, "warm", 20, 40, 50, 60 ); MEMBFUNC mf4( MF_TRI, "Just good", 26, 30, 38 );
Several symmetric functions can be automatically defined:
The triangular function can be defined by only two values, the center value, and its half-width at bottom. For example, the following code:
mf.Set( MF_TRI, 30.0, 4.0 );
will assign to mf
a triangular function centered at 30 with a total width of 8.
A symmetric trapezoidal function can be defined by giving three values:
For example:
mf.Set( MF_LL, 30.0, 1.0, 5.0 );
Of course, the second and third value must be positive, and the third value must be higher than the second value.
symmetric MF_TRI function | symmetric MF_LL function |
---|---|
The Gaussian membership function is an approximation of the real function using 6 line segments. It has a ("sigma") argument, but contrarly to a real Gaussian, it always has a maximum value of 1.
MEMBFUNC mf( MF_GAUSS, "Gaussian", x0, sigma );
Although the function may seem a really rough approximation, it is actually not that far from the real Gaussian function, see Gaussian function approximation.
This set of membership function has been generated with this code, from file src/demo/gui/generate-doc-plots.cpp:
MEMBFUNC fa( MF_GAUSS, "s=0.5", 10.0, 0.5 ); MEMBFUNC fb( MF_GAUSS, "s=1.0", 12.0, 1.0 ); MEMBFUNC fc( MF_GAUSS, "s=2.0", 15.0, 2.0 ); MEMBFUNC fd( MF_GAUSS, "s=5.0", 25.0, 5.0 ); FUZZY_IN set( "Gaussian functions example" ); set.AddMf( fa ); set.AddMf( fb ); set.AddMf( fc ); set.AddMf( fd );
Several functions are provided to give the user some information about the function:
Once you have set up a MF, its goal is to give you the fuzzy value corresponding to a real value. This is done with the MEMBFUNC::Fuzzify() function.
REALVAL x; ... FUZZYVAL y = mf.Fuzzify( x );
You can create an empty function and add points one by one, by giving x and y values with MEMBFUNC::AddPoint() :
MEMBFUNC mf1("warm");
mf1.AddPoint( 20, 0 );
mf1.AddPoint( 30, 1 );
mf1.AddPoint( 40, 1 );
mf1.AddPoint( 60, 0 );
mf1.AddPoint( 20, 2 );
Editing a membership function can be done by modifying its points, assuming you know their indexes, with the function MEMBFUNC::ModifyPoint() :
mf.ModifyPoint( idx, 30, 0.5 );
or
FPOINT new_point( 30, 0.5 ); mf.ModifyPoint( idx, new_point );
Most of the time, you only need to change the fuzzy value of the point, you can do that directly:
mf.ModifyPoint( idx, 0.4 );
Or you can also remove it, with MEMBFUNC::DeletePoint(), and add the new point:
mf.DeletePoint( idx ); mf.AddPoint( FPOINT( 30, 0.5 ) );
Don't worry about the order of points inside the membership function, they will be correctly reordered inside automatically.
You can also shift the whole function left or right, using MEMBFUNC::Shift() :
mf.Shift( 2.0 ); // shifts all the points to right (+2)
If you want to clear a function and make it empty, just use the MEMBFUNC::Clear() :
mf.Clear();
This function can also be used to assign some constant value to a function (indeed, a function can have no points without having a null value).
mf.Clear( 0.3 );
Two types of operations can be done with membership functions (aka "fuzzy sets"), unary operations and binary operations. Unary operations are implemented as self-transforming (non-const) member functions, i.e., they are in the form:
MEMBFUNC mf;
... // add content
mf.DoThis();
while binary operation are implemented as free functions, in the form:
DoThis( mf1, mf2, mf_result );
The following unary operations are available:
For a quick example, see the following code and the generated functions:
MEMBFUNC mf( MF_LL, "test", 10, 20, 50, 60 ); mf.ModifyPoint( 1, 0.7 ); MEMBFUNC mf_T(mf); MEMBFUNC mf_A(mf); MEMBFUNC mf_C(mf); mf_T.Truncate(0.4); mf_A.AlphaCut(0.4); mf_A.Scale(0.4); mf_C.Complement();
Original function | Complement | |
---|---|---|
Alphacut (0.4) | Truncate (0.4) | Scale (0.4) |
Binary operations are called in the litterature t-norms and t-conorms. See the following links for details:
These norms map to slifis::EN_TNORM and slifis::EN_SNORM used for the inference process (see FIS usage and Defuzzification), and slifis::EN_MERGE_OP holds both. At present, the following are supported:
Either you call the function FuzzyMerge(), with the appropriate slifis::EN_MERGE_OP value as argument, or you can directly call one of the following:
These functions are declared in global_functions.hpp and implemented in global_functions.cpp.
See below examples of these binary operations:
The OR operation is achieved with the following code:
FuzzyMax( mf0, mf1, mf_out );
or
FuzzyMerge( OPM_MAX, mf0, mf1, mf_out );
Example 1:
Source functions | Corresponding output function |
---|---|
Example 2:
Source functions | Corresponding output function |
---|---|
The MIN operation is achieved with the following code:
FuzzyMin( mf0, mf1, mf_out );
or
FuzzyMerge( OPM_MIN, mf0, mf1, mf_out );
Example 1:
Source functions | Corresponding output function |
---|---|
Example 2:
Source functions | Corresponding output function |
---|---|
The product operation is achieved with the following code:
FuzzyProduct( mf0, mf1, mf_out );
or
FuzzyMerge( OPM_PRODUCT, mf0, mf1, mf_out );
Example 1:
Source functions | Corresponding output function |
---|---|
Example 2:
Source functions | Corresponding output function |
---|---|
The probalistic sum operation is achieved with the following code:
FuzzyPSum( mf0, mf1, mf_out );
or
FuzzyMerge( OPM_PSUM, mf0, mf1, mf_out );
Example 1:
Source functions | Corresponding output function |
---|---|
Example 2:
Source functions | Corresponding output function |
---|---|
The bounded sum operation is achieved with the following code:
FuzzyBSum( mf0, mf1, mf_out );
or
FuzzyMerge( OPM_BSUM, mf0, mf1, mf_out );
Example 1:
Source functions | Corresponding output function |
---|---|
Example 2:
Source functions | Corresponding output function |
---|---|
The Łukasiewicz t-norm operation is achieved with the following code:
FuzzyLukas( mf0, mf1, mf_out );
or
FuzzyMerge( OPM_LUKAS, mf0, mf1, mf_out );
Example 1:
Source functions | Corresponding output function |
---|---|
Example 2:
Source functions | Corresponding output function |
---|---|