Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Fuzzy - Library for developing fuzzy models - Library for MetaTrader 5 - MT4/MT5 Resources

author EAcpu | 2 reads | 0 comments |

Fuzzy - library for developing fuzzy models - library for MetaTrader 5

Fuzzy - library for developing fuzzy models - library for MetaTrader 5

Fuzzy - library for developing fuzzy models - library for MetaTrader 5

Fuzzy - library for developing fuzzy models - library for MetaTrader 5

Dmitry Kalyuzhny. FuzzyNet project website - http://sourceforge.net/projects/fuzzynet/

Extract the archive to the terminal data folder .
The library code is located at \MQL5\Include\Math\FuzzyNet\
Sample test scripts can be found at Found in \MQL5\Scripts\FuzzyNet\

FuzzyNet is one of the most popular mathematical libraries for creating fuzzy models

The Microsoft.Net Fuzzy Logic Library (FuzzyNet) is an easy-to-use component that implements the Mamdani and Sugeno fuzzy inference systems.

Fuzzy networks include:

The following content was added when converting the library to MQL5:

Use the library

Notes: Mamdani-type inference systems can be configured at any stage after creation but before calling system calculation functions. If the system settings have not been changed since they were created, the system uses the default settings:

The transformation for the FuzzyNet library (v.1.2.0) is shown below.

To use this library, include the MamdaniFuzzySystem.mqh or SugenoFuzzySystem.mqh file, depending on the system you want to create.

The package also contains helper classes for implementing fuzzy rules.

Sugeno-type fuzzy variables are used when developing rules for Sugeno-type systems.

Using the FuzzyNet library in MQL5

Before writing a fuzzy system, you should have a clear understanding of its elements, including:

System development and calculation:

For Mamdani type systems:

 MamdaniFuzzySystem *fuzzy_system=new MamdaniFuzzySystem();
For Sugeno type systems:
 SugenoFuzzySystem *fuzzy_system=new SugenoFuzzySystem();
  • Create all fuzzy input variables independently of the system by assigning them names and max/min values ​​as parameters:
     Fuzzy variable *fuzzy_variable = new fuzzy variable ( const string name, constant double minute, constant double maximum);
  • A function that creates fuzzy terms, creating the fuzzy terms themselves and passing them appropriate names and membership functions. After that, add the items to the corresponding variables. In order to reduce program code, the process can be written as follows:
     fuzzy_variable.Terms().Add( new fuzzy term( const string name, new IMembershipFunction());
  • Enter the input variables into the system:
     fuzzy_system.Input().Add(FuzzyVariable fuzzy_variable);
  • Create input variables, taking note of your system type. For a Mamdani type system, create something similar to steps 2 and 3. For Sugeno-type models, create special fuzzy variables that accept only variable names as arguments:
     Sugeno variable *sugeno_variable= new Sugeno variable ( constant string name);
    A linear function that explains the linear combination of the input values ​​is added to the Sugeno-type fuzzy variable instead of the fuzzy term. Arrays of names and coefficients are used as linear function arguments. The linear equation is formed based on this array, so it is important to respect the order of the elements in the array. The coefficient array length should be equal to the number of input values ​​or one more than the input values. If the lengths are equal, the absolute term of the equation is equal to zero. If the array length exceeds this amount, the absolute term is equal to the last element value. All other array elements starting from the first array element are assigned to fuzzy input variables in the order in which they are entered into the system.
     sugeno_varriable.Functions().Add(fuzzy_sytem.CreateSugenoFunction( const string name, const double &coeffs[]));
  • Similar to step 4, the output variables should also be added to the system:

    For Mamdani type systems:

     fuzzy_system.Output().Add(FuzzyVariable fuzzy_variable);

    For Sugeno type systems:

     fuzzy_system.Output().Add(FuzzyVariable fuzzy_variable);
  • Arrange a set of rules according to the system. Rules are defined as public strings and automatically analyzed based on keywords. The keywords are "if", "then", "is", "and", "or", "not", "(", ")", "slightly", "somewhat", "very" and "extremely", as well as all variable names, terms and functions available in the system.

    For Mamdani type systems:

     MamdaniFuzzyRule *fuzzy_rule = fuzzy_system.ParseRule(const string Rule_text);

    For Sugeno type systems:

     SugenoFuzzyRule *fuzzy_rule = fuzzy_system.ParseRule(const string Rule_text);
  • Enter all rules into the system:

    For Mamdani type systems:

    fuzzy_system.Rules().Add(MamdaniFuzzyRule fuzzy_rule);

    For Sugeno type systems:

     fuzzy_system.Rules().Add(SugenoFuzzyRule fuzzy_rule);
  • Pass the input value of the variable to the system for calculation. To do this, they should be defined. At its input, the system accepts a list of values ​​consisting of Dictionary_Obj_Double class objects. This class is described in the Dictionary.mqh file.
     Dictionary_Obj_Double *p_od_in=new Dictionary_Obj_Double;
    This class implements the SetAll(CObject *key, const double value) method, which accepts two parameters - a fuzzy variable and a numeric value. This element is an input variable to the system.
     p_od_in.SetAll(FuzzyVariable fuzzy_variable, constant double value );
    All other input values ​​are filled in the same way. Create a list and add all values ​​to it:
     Directory * in = new CList; in . add (p_od_in);
  • The output value should also be specified:
     Dictionary_Obj_Double *p_od_out= new Dictionary_Obj_Double;  
     Directory * out = new C list;
  • Call the Calculate(CList *&list) function of our system to return the system calculation result list:
     Output=fuzzy_system.Calculate(in);
    After that, go out to list and store all calculated output values ​​in the order they were entered into the system. We just need to receive them:
     p_od_out=out.GetNodeAtIndex(int ​​index);
    Double result=p_od_out.Value();
    Now, the result variable stores the system's calculated result exponent for the output value of the specified number in the input system.
  • Sample script

    Prompt example (Mamdani)

    Tips_Sample_Mamdani.mq5 Calculate the percentage you need to tip based on the quality of service and food.

    Enter the input parameters:

    Fuzzy - library for developing fuzzy models - library for MetaTrader 5

    Calculation result:

    Fuzzy - library for developing fuzzy models - library for MetaTrader 5

    Cruise control sample (Sugeno)

    The Cruise_Control_Sample_Sugeno.mq5 sample script is an example of a fuzzy regulator. It represents a car cruise control system that uses data on current deviation and the rate of deviation change to calculate the necessary acceleration so that the car reaches the desired speed.

    Enter the input parameters:

    Fuzzy - library for developing fuzzy models - library for MetaTrader 5

    Calculation result:

    Fuzzy - library for developing fuzzy models - library for MetaTrader 5


    Attachment download

    📎 dictionary.mqh (9.1 KB)

    📎 fuzzyrule.mqh (17.77 KB)

    📎 fuzzyterm.mqh (3.73 KB)

    📎 fuzzyvariable.mqh (5.47 KB)

    📎 genericfuzzysystem.mqh (11.88 KB)

    📎 helper.mqh (7.18 KB)

    📎 inferencemethod.mqh (7.06 KB)

    📎 mamdanifuzzysystem.mqh (20.39 KB)

    📎 membershipfunction.mqh (43.45 KB)

    📎 ruleparser.mqh (36.52 KB)

    📎 sugenofuzzysystem.mqh (12.69 KB)

    📎 sugenovariable.mqh (11 KB)

    📎 cruise_control_sample_sugeno.mq5 (7.65 KB)

    📎 TestFuzzy.mq5 (26.37 KB)

    📎tips_sample_mamdani.mq5 (5.98 KB)

    Source: MQL5 #13697

    Verification code Refresh