//+------------------------------------------------------------------+ //| genericfuzzysystem.mqh | //| Copyright 2000-2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ //| Implementation of Fuzzy library in MetaQuotes Language 5 | //| | //| The features of the library include: | //| - Create Mamdani fuzzy model | //| - Create Sugeno fuzzy model | //| - Normal membership function | //| - Triangular membership function | //| - Trapezoidal membership function | //| - Constant membership function | //| - Defuzzification method of center of gravity (COG) | //| - Defuzzification method of bisector of area (BOA) | //| - Defuzzification method of mean of maxima (MeOM) | //| | //| This file is free software; you can redistribute it and/or | //| modify it under the terms of the GNU General Public License as | //| published by the Free Software Foundation (www.fsf.org); either | //| version 2 of the License, or (at your option) any later version. | //| | //| This program is distributed in the hope that it will be useful, | //| but WITHOUT ANY WARRANTY; without even the implied warranty of | //| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | //| GNU General Public License for more details. | //+------------------------------------------------------------------+ #include #include #include "FuzzyRule.mqh" #include "InferenceMethod.mqh" #include "Dictionary.mqh" //+------------------------------------------------------------------+ //| Purpose: Creating generic fuzzy system | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Common functionality of Mamdani and Sugeno fuzzy systems | //+------------------------------------------------------------------+ class CGenericFuzzySystem { private: CList *m_input; // List of input fuzzy variables EnAndMethod m_and_method; // And method from InferenceMethod EnOrMethod m_or_method; // Or method from InferenceMethod protected: CGenericFuzzySystem(void); ~CGenericFuzzySystem(void); public: //--- method gets the input linguistic variables CList *Input(void) { return(m_input); } //--- method gets or sets the type of "And method" void AndMethod(EnAndMethod value) { m_and_method=value; } EnAndMethod AndMethod(void) const { return (m_and_method); } //--- method gets or sets the type of "Or method" void OrMethod(EnOrMethod value) { m_or_method=value; } EnOrMethod OrMethod(void) const { return (m_or_method); } //--- method gets the varriable by name CFuzzyVariable *InputByName(const string name); //--- common steps of calculating CList *Fuzzify(CList *inputValues); protected: double EvaluateCondition(ICondition *condition,CList *fuzzifiedInput); double EvaluateConditionPair(const double cond1,const double cond2,OperatorType op); private: bool ValidateInputValues(CList *inputValues,string &msg); }; //+------------------------------------------------------------------+ //| Constructor without parameters | //+------------------------------------------------------------------+ CGenericFuzzySystem::CGenericFuzzySystem(void) { m_input=new CList; } //+------------------------------------------------------------------+ //| Destructor | //+------------------------------------------------------------------+ CGenericFuzzySystem::~CGenericFuzzySystem(void) { if(CheckPointer(m_input)==POINTER_DYNAMIC) { delete m_input; } } //+------------------------------------------------------------------+ //| Get input linguistic variable by its name | //+------------------------------------------------------------------+ CFuzzyVariable *CGenericFuzzySystem::InputByName(const string name) { CList *result=CGenericFuzzySystem::Input(); for(int i=0; ivar.Max()) { msg=StringFormat("Value for the %s variable is out of range.",var.Name()); //--- return false return (false); } } } if(contain==false) { msg=StringFormat("Value for the %s variable does not present.",var.Name()); //--- return false return (false); } } //--- return true return (true); } //+------------------------------------------------------------------+