程序库: FuzzyNet 模糊逻辑库

 

FuzzyNet 模糊逻辑库:

FuzzyNet 是用于创建模糊模型的最流行的数学库之一

用于 Microsoft.Net 的模糊逻辑库 (FuzzyNet) 是一款易于使用的控件, 可用来实现马丹尼和关野 (Sugeno) 型模糊推理系统。

FuzzyNet 包括:

  • 5 个隶属函数
  • 形式灵活的开发模糊系统的规则。
  • 马丹尼 模糊推理系统。
  • 关野 模糊推理系统。
  • 一个马丹尼型系统的 去模糊化 方法。
  • 无限量的输入和输出变量。

当程序库移植到 MQL5 时, 补充了以下内容:

  • 8 个新的隶属函数。
  • 4 个马丹尼型系统的去模糊化方法。

作者: MetaQuotes Software Corp.

 

1.现在可以在 MT5 中使用了,这真是太好了。我的理解是否正确,数学是非洲的数学,在 MT4 中也能使用?

2.我第一次尝试在 Matlab 中使用 Fuzzi,但老实说,那是很久以前的事了,我从未真正理解如何将其应用于外汇交易并保证盈利。因此,我转向了小波、统计等方向。

3.您能推荐如何将 Fuzzi-logic 应用于实践的文献吗?即使不是在交易中。

4.我对这个话题很感兴趣,网站需要关于这个话题的文章吗?

 
Alexey Volchanskiy:

1.现在能在 MT5 中使用这个功能真是太好了。我是否正确理解了 "数学 "在非洲就是 "数学",并且可以在 MT4 中使用?

2.我第一次尝试在 Matlab 中使用 Fuzzi,但老实说,那是很久以前的事了,我从未真正理解如何将其应用于外汇交易并保证盈利。因此,我转向了小波、统计等方向。

3.您能推荐如何将 Fuzzi-logic 应用于实践的文献吗?即使不是在交易中。

4.我对这个话题很感兴趣,网站需要这方面的文章吗?

您好。

1.我们将对 MT4 进行改编(稍后)

3.4.我们将很快发布一篇关于使用 FuzzyNet 的文章。之后,请致函服务台,以便更具体地讨论该主题。

 

该主题值得关注。在应用时,需要对主题有所了解。从这个角度看,提示例子并不严肃。至少应该顺便解释一下 Mamdani 系统和 Sugeno 系统之间的区别。而模糊逻辑的基本概念对于那些第一次听说这一概念的人来说也是有用的。

在档案馆的某个地方,有一本俄语的模糊 逻辑书籍(描述了模糊逻辑系统 程序)。因此,在档案库中找不到。稍后我会把它贴出来。

祝好运

 
一些具备相关基本知识的资料来源。
附加的文件:
FuzLog.zip  1062 kb
 

您好、

我喜欢模糊、SVM、神经等系统,所以我发现了这个库,并尝试了一下。其中包含的脚本足够易懂,但在编译时出现了一些错误,我希望能引起注意--因此,在带有 sugeno 的 cruise_control_sample_sugeno.mq5 脚本中,......是......:

//+------------------------------------------------------------------+
//|fuzzynet.mqh | |
//| 2015 年 MetaQuotes 软件公司版权所有。|
//|https://www.mql5.com ||
//+------------------------------------------------------------------+
//| 在 MetaQuotes Language 5(MQL5)中实现 FuzzyNet 库。
//||
//| FuzzyNet 库的功能包括:|||......
//| - 创建马姆达尼模糊模型|
//| - 创建菅野模糊模型|
//| - 正态成员函数|
//| - 三角成员函数|
//| - 梯形成员函数|
//| - 恒定成员函数|
//| - 重心(COG)的模糊化方法
//| - 面积平分线(BOA)的模糊化方法
//| - 最大值平均值 (MeOM) 的模糊化方法
//||
//| 如果您发现 FuzzyNet for MQL5 在功能上有任何差异,请联系我们。
//| 和原始 FuzzyNet 项目,请联系 | 的开发人员。
//www.mql5.com 论坛上的 MQL5|
//||
//| 您可以报告在计算算法中发现的错误。
//| 通知 FuzzyNet 项目协调人,建立 FuzzyNet 库。
//+------------------------------------------------------------------+
//| 源许可证|
//||
//| 本程序是自由软件,您可以重新发布和/或
//| 根据 GNU 通用公共许可证条款修改为 |
//| 由自由软件基金会(www.fsf.org) 发布;或者 | 由自由软件基金会(www.fsf.org) 发布。
// | 许可证第 2 版,或(由您选择)任何后续版本。|
//||
// | 发布本程序是希望它能有所帮助。
//| 但不附带任何保证;甚至不附带暗示的保证。
//| MERCHANTABILITY(适销性)或 FITNESS FOR A PARTICULAR PURPOSE(适用于特定用途)。参见
//| GNU 通用公共许可证了解更多详情。
//||
//| GNU 通用公共许可证副本可在以下网址获取
//| http://www.fsf.org/licensing/licenses                            |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| 连接图书馆|
//+------------------------------------------------------------------+
#include <Math\FuzzyNet\SugenoFuzzySystem.mqh>
//--- 输入参数
input double   Speed_Error;
input double   Speed_ErrorDot;
//+------------------------------------------------------------------+
//| 脚本程序启动功能|
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 杉野模糊系统
   CSugenoFuzzySystem *fsCruiseControl=new CSugenoFuzzySystem();
//--- 为系统创建第一个输入变量
   CFuzzyVariable *fvSpeedError=new CFuzzyVariable("SpeedError",-20.0,20.0);
   fvSpeedError.Terms().Add(new CFuzzyTerm("slower",new CTriangularMembershipFunction(-35.0,-20.0,-5.0)));
   fvSpeedError.Terms().Add(new CFuzzyTerm("zero", new CTriangularMembershipFunction(-15.0, -0.0, 15.0)));
   fvSpeedError.Terms().Add(new CFuzzyTerm("faster", new CTriangularMembershipFunction(5.0, 20.0, 35.0)));
   fsCruiseControl.Input().Add(fvSpeedError);
//-- 为系统创建第二个输入变量
   CFuzzyVariable *fvSpeedErrorDot=new CFuzzyVariable("SpeedErrorDot",-5.0,5.0);
   fvSpeedErrorDot.Terms().Add(new CFuzzyTerm("slower", new CTriangularMembershipFunction(-9.0, -5.0, -1.0)));
   fvSpeedErrorDot.Terms().Add(new CFuzzyTerm("zero", new CTriangularMembershipFunction(-4.0, -0.0, 4.0)));
   fvSpeedErrorDot.Terms().Add(new CFuzzyTerm("faster", new CTriangularMembershipFunction(1.0, 5.0, 9.0)));
   fsCruiseControl.Input().Add(fvSpeedErrorDot);
//--- 创建输出
   CSugenoVariable *svAccelerate=new CSugenoVariable("Accelerate");
   double coeff1[3]={0.0,0.0,0.0};
   svAccelerate.Functions().Add(fsCruiseControl.CreateSugenoFunction("zero",coeff1));
   double coeff2[3]={0.0,0.0,1.0};
   svAccelerate.Functions().Add(fsCruiseControl.CreateSugenoFunction("faster",coeff2));
   double coeff3[3]={0.0,0.0,-1.0};
   svAccelerate.Functions().Add(fsCruiseControl.CreateSugenoFunction("slower",coeff3));
   double coeff4[3]={-0.04,-0.1,0.0};
   svAccelerate.Functions().Add(fsCruiseControl.CreateSugenoFunction("func",coeff4));
   fsCruiseControl.Output().Add(svAccelerate);
//--- Craete Sugeno 模糊规则
   CSugenoFuzzyRule *rule1 = fsCruiseControl.ParseRule("if (SpeedError is slower) and (SpeedErrorDot is slower) then (Accelerate is faster)");
   CSugenoFuzzyRule *rule2 = fsCruiseControl.ParseRule("if (SpeedError is slower) and (SpeedErrorDot is zero) then (Accelerate is faster)");
   CSugenoFuzzyRule *rule3 = fsCruiseControl.ParseRule("if (SpeedError is slower) and (SpeedErrorDot is faster) then (Accelerate is zero)");
   CSugenoFuzzyRule *rule4 = fsCruiseControl.ParseRule("if (SpeedError is zero) and (SpeedErrorDot is slower) then (Accelerate is faster)");
   CSugenoFuzzyRule *rule5 = fsCruiseControl.ParseRule("if (SpeedError is zero) and (SpeedErrorDot is zero) then (Accelerate is func)");
   CSugenoFuzzyRule *rule6 = fsCruiseControl.ParseRule("if (SpeedError is zero) and (SpeedErrorDot is faster) then (Accelerate is slower)");
   CSugenoFuzzyRule *rule7 = fsCruiseControl.ParseRule("if (SpeedError is faster) and (SpeedErrorDot is slower) then (Accelerate is faster)");
   CSugenoFuzzyRule *rule8 = fsCruiseControl.ParseRule("if (SpeedError is faster) and (SpeedErrorDot is zero) then (Accelerate is slower)");
   CSugenoFuzzyRule *rule9 = fsCruiseControl.ParseRule("if (SpeedError is faster) and (SpeedErrorDot is faster) then (Accelerate is slower)");
//--- 在系统中添加杉野模糊规则
   fsCruiseControl.Rules().Add(rule1);
   fsCruiseControl.Rules().Add(rule2);
   fsCruiseControl.Rules().Add(rule3);
   fsCruiseControl.Rules().Add(rule4);
   fsCruiseControl.Rules().Add(rule5);
   fsCruiseControl.Rules().Add(rule6);
   fsCruiseControl.Rules().Add(rule7);
   fsCruiseControl.Rules().Add(rule8);
   fsCruiseControl.Rules().Add(rule9);
//--- 设置输入值并获取结果
   CList *in=new CList;
   CDictionary_Obj_Double *p_od_Error=new CDictionary_Obj_Double;
   CDictionary_Obj_Double *p_od_ErrorDot=new CDictionary_Obj_Double;
   p_od_Error.SetAll(fvSpeedError,Speed_Error);
   p_od_ErrorDot.SetAll(fvSpeedErrorDot,Speed_ErrorDot);
   in.Add(p_od_Error);
   in.Add(p_od_ErrorDot);
//--- 获取结果
   CList *result;
   CDictionary_Obj_Double *p_od_Accelerate;
   result=fsCruiseControl.Calculate(in);
   p_od_Accelerate=result.GetNodeAtIndex(0);
   Alert("Accelerate, %: ",p_od_Accelerate.Value()*100);
   delete in;
   delete result;
   delete fsCruiseControl;
  }
//+------------------------------------------------------------------+

而带有 mamdani 的脚本 :

//+------------------------------------------------------------------+
//|fuzzynet.mqh | |
//| 2015 年 MetaQuotes 软件公司版权所有。|
//|https://www.mql5.com ||
//+------------------------------------------------------------------+
//| 在 MetaQuotes Language 5(MQL5)中实现 FuzzyNet 库。
//||
//| FuzzyNet 库的功能包括:|||......
//| - 创建马姆达尼模糊模型|
//| - 创建菅野模糊模型|
//| - 正态成员函数|
//| - 三角成员函数|
//| - 梯形成员函数|
//| - 恒定成员函数|
//| - 重心(COG)的模糊化方法
//| - 面积平分线(BOA)的模糊化方法
//| - 最大值平均值 (MeOM) 的模糊化方法
//||
//| 如果您发现 FuzzyNet for MQL5 在功能上有任何差异,请联系我们。
//| 和原始 FuzzyNet 项目,请联系 | 的开发人员。
//www.mql5.com 论坛上的 MQL5|
//||
//| 您可以报告在计算算法中发现的错误。
//| 通知 FuzzyNet 项目协调人,建立 FuzzyNet 库。
//+------------------------------------------------------------------+
//| 源许可证|
//||
//| 本程序是自由软件,您可以重新发布和/或
//| 根据 GNU 通用公共许可证条款修改为 |
//| 由自由软件基金会(www.fsf.org) 发布;或者 | 由自由软件基金会(www.fsf.org) 发布。
// | 许可证第 2 版,或(由您选择)任何后续版本。|
//||
// | 发布本程序是希望它能有所帮助。
//| 但不附带任何保证;甚至不附带暗示的保证。
//| MERCHANTABILITY(适销性)或 FITNESS FOR A PARTICULAR PURPOSE(适用于特定用途)。参见
//| GNU 通用公共许可证了解更多详情。
//||
//| GNU 通用公共许可证副本可在以下网址获取
//| http://www.fsf.org/licensing/licenses&nbsp;                           |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| 连接图书馆|
//+------------------------------------------------------------------+
#include <Math\FuzzyNet\MamdaniFuzzySystem.mqh>
//--- 输入参数
input double   Service;
input double   Food;
//+------------------------------------------------------------------+
//| 脚本程序启动功能|
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 马姆达尼模糊系统
   CMamdaniFuzzySystem *fsTips=new CMamdaniFuzzySystem();
//--- 为系统创建第一个输入变量
   CFuzzyVariable *fvService=new CFuzzyVariable("service",0.0,10.0);
   fvService.Terms().Add(new CFuzzyTerm("poor", new CTriangularMembershipFunction(-5.0, 0.0, 5.0)));
   fvService.Terms().Add(new CFuzzyTerm("good", new CTriangularMembershipFunction(0.0, 5.0, 10.0)));
   fvService.Terms().Add(new CFuzzyTerm("excellent", new CTriangularMembershipFunction(5.0, 10.0, 15.0)));
   fsTips.Input().Add(fvService);
//-- 为系统创建第二个输入变量
   CFuzzyVariable *fvFood=new CFuzzyVariable("food",0.0,10.0);
   fvFood.Terms().Add(new CFuzzyTerm("rancid", new CTrapezoidMembershipFunction(0.0, 0.0, 1.0, 3.0)));
   fvFood.Terms().Add(new CFuzzyTerm("delicious", new CTrapezoidMembershipFunction(7.0, 9.0, 10.0, 10.0)));
   fsTips.Input().Add(fvFood);
//--- 创建输出
   CFuzzyVariable *fvTips=new CFuzzyVariable("tips",0.0,30.0);
   fvTips.Terms().Add(new CFuzzyTerm("cheap", new CTriangularMembershipFunction(0.0, 5.0, 10.0)));
   fvTips.Terms().Add(new CFuzzyTerm("average", new CTriangularMembershipFunction(10.0, 15.0, 20.0)));
   fvTips.Terms().Add(new CFuzzyTerm("generous", new CTriangularMembershipFunction(20.0, 25.0, 30.0)));
   fsTips.Output().Add(fvTips);
//--- 创建三个马姆达尼模糊规则
   CMamdaniFuzzyRule *rule1 = fsTips.ParseRule("if (service is poor )  or (food is rancid) then tips is cheap");
   CMamdaniFuzzyRule *rule2 = fsTips.ParseRule("if ((service is good)) then tips is average");
   CMamdaniFuzzyRule *rule3 = fsTips.ParseRule("if (service is excellent) or (food is delicious) then (tips is generous)");
//--- 在系统中添加三条马姆达尼模糊规则
   fsTips.Rules().Add(rule1);
   fsTips.Rules().Add(rule2);
   fsTips.Rules().Add(rule3);
//--- 设置输入值
   CList *in=new CList;
   CDictionary_Obj_Double *p_od_Service=new CDictionary_Obj_Double;
   CDictionary_Obj_Double *p_od_Food=new CDictionary_Obj_Double;
   p_od_Service.SetAll(fvService, Service);
   p_od_Food.SetAll(fvFood, Food);
   in.Add(p_od_Service);
   in.Add(p_od_Food);
//--- 获取结果
   CList *result;
   CDictionary_Obj_Double *p_od_Tips;
   result=fsTips.Calculate(in);
   p_od_Tips=result.GetNodeAtIndex(0);
   Alert("Tips, %: ",p_od_Tips.Value());
   delete in;
   delete result;
   delete fsTips;
  }
//+------------------------------------------------------------------+

感谢您的分享!

 
是否有可能创建一个有超过 02 个输入变量的 mandani 模型?我在输入第三个输入变量时出错了。
 

亲爱的支持自由党的人们,或者任何人都可以提供帮助,....。:)我想加快计算速度。在所附示例中,脚本创建模糊逻辑类对象,计算结果并删除。我想让这些对象只创建一次,在 Calculate() 中只传递新值,并通过已配置的模糊逻辑获取结果。

以下是原始示例,运行正常:

//+------------------------------------------------------------------+
//|提示样本。mq5
//| 版权所有 2017, MetaQuotes Software Corp.
//|https://www.mql5.com ||
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Math\Fuzzy\MamdaniFuzzySystem.mqh>
//--- 输入参数
input double   Service;
input double   Food;

//+------------------------------------------------------------------+
//| 专家初始化函数|
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 专家去初始化函数|
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| 专家勾选功能|
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  //--- 马姆达尼模糊系统 
   CMamdaniFuzzySystem *fsTips=new CMamdaniFuzzySystem();
//--- 为系统创建第一个输入变量
   CFuzzyVariable *fvService=new CFuzzyVariable("service",0.0,10.0);
   fvService.Terms().Add(new CFuzzyTerm("poor", new CTriangularMembershipFunction(-5.0, 0.0, 5.0)));
   fvService.Terms().Add(new CFuzzyTerm("good", new CTriangularMembershipFunction(0.0, 5.0, 10.0)));
   fvService.Terms().Add(new CFuzzyTerm("excellent", new CTriangularMembershipFunction(5.0, 10.0, 15.0)));
   fsTips.Input().Add(fvService);
//-- 为系统创建第二个输入变量
   CFuzzyVariable *fvFood=new CFuzzyVariable("food",0.0,10.0);
   fvFood.Terms().Add(new CFuzzyTerm("rancid", new CTrapezoidMembershipFunction(0.0, 0.0, 1.0, 3.0)));
   fvFood.Terms().Add(new CFuzzyTerm("delicious", new CTrapezoidMembershipFunction(7.0, 9.0, 10.0, 10.0)));
   fsTips.Input().Add(fvFood);
//--- 创建输出
   CFuzzyVariable *fvTips=new CFuzzyVariable("tips",0.0,30.0);
   fvTips.Terms().Add(new CFuzzyTerm("cheap", new CTriangularMembershipFunction(0.0, 5.0, 10.0)));
   fvTips.Terms().Add(new CFuzzyTerm("average", new CTriangularMembershipFunction(10.0, 15.0, 20.0)));
   fvTips.Terms().Add(new CFuzzyTerm("generous", new CTriangularMembershipFunction(20.0, 25.0, 30.0)));
   fsTips.Output().Add(fvTips);
//--- 创建三个马姆达尼模糊规则
   CMamdaniFuzzyRule *rule1 = fsTips.ParseRule("if (service is poor )  or (food is rancid) then tips is cheap");
   CMamdaniFuzzyRule *rule2 = fsTips.ParseRule("if ((service is good)) then tips is average");
   CMamdaniFuzzyRule *rule3 = fsTips.ParseRule("if (service is excellent) or (food is delicious) then (tips is generous)");
//--- 在系统中添加三条马姆达尼模糊规则
   fsTips.Rules().Add(rule1);
   fsTips.Rules().Add(rule2);
   fsTips.Rules().Add(rule3);
//--- 设置输入值
   CList *in=new CList;
   CDictionary_Obj_Double *p_od_Service=new CDictionary_Obj_Double;
   CDictionary_Obj_Double *p_od_Food=new CDictionary_Obj_Double;
   p_od_Service.SetAll(fvService, Service);
   p_od_Food.SetAll(fvFood, Food);
   in.Add(p_od_Service);
   in.Add(p_od_Food);
//--- 获取结果
   CList *result;
   CDictionary_Obj_Double *p_od_Tips;
   result=fsTips.Calculate(in);
   p_od_Tips=result.GetNodeAtIndex(0);
   Print("Tips, %: ",p_od_Tips.Value());
   delete in;
   delete result;
   delete fsTips;
  }
//+------------------------------------------------------------------+

下面是我的示例,它出现了错误:

2017.09.07 14:28:56.949 Core 1  2017.07.03 00:00:00   Input values count is incorrect.
2017.09.07 14:28:56.949 Core 1  2017.07.03 00:00:00   invalid pointer access in 'MamdaniFuzzySystem.mqh' (172,42)

代码本身:

//+------------------------------------------------------------------+
//|TipsSample.mq5
//| 版权所有 2017, MetaQuotes Software Corp.
//|https://www.mql5.com ||
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Math\Fuzzy\MamdaniFuzzySystem.mqh>

input double   Service;
input double   Food;
//+------------------------------------------------------------------+
//| 专家初始化函数|
//+------------------------------------------------------------------+
CMamdaniFuzzySystem *fsTips=new CMamdaniFuzzySystem();
CFuzzyVariable *fvService=new CFuzzyVariable("service",0.0,10.0);
CFuzzyVariable *fvFood=new CFuzzyVariable("food",0.0,10.0);
CFuzzyVariable *fvTips=new CFuzzyVariable("tips",0.0,30.0);
CMamdaniFuzzyRule *rule1, *rule2, *rule3;

CList *in=new CList;
CDictionary_Obj_Double *p_od_Service=new CDictionary_Obj_Double;
CDictionary_Obj_Double *p_od_Food=new CDictionary_Obj_Double;

CList *result;
CDictionary_Obj_Double *p_od_Tips;
   
int OnInit()
  {
//---
   
   fvService.Terms().Add(new CFuzzyTerm("poor", new CTriangularMembershipFunction(-5.0, 0.0, 5.0)));
   fvService.Terms().Add(new CFuzzyTerm("good", new CTriangularMembershipFunction(0.0, 5.0, 10.0)));
   fvService.Terms().Add(new CFuzzyTerm("excellent", new CTriangularMembershipFunction(5.0, 10.0, 15.0)));
   fsTips.Input().Add(fvService);
//-- 为系统创建第二个输入变量
   
   fvFood.Terms().Add(new CFuzzyTerm("rancid", new CTrapezoidMembershipFunction(0.0, 0.0, 1.0, 3.0)));
   fvFood.Terms().Add(new CFuzzyTerm("delicious", new CTrapezoidMembershipFunction(7.0, 9.0, 10.0, 10.0)));
   fsTips.Input().Add(fvFood);
//--- 创建输出
  
   fvTips.Terms().Add(new CFuzzyTerm("cheap", new CTriangularMembershipFunction(0.0, 5.0, 10.0)));
   fvTips.Terms().Add(new CFuzzyTerm("average", new CTriangularMembershipFunction(10.0, 15.0, 20.0)));
   fvTips.Terms().Add(new CFuzzyTerm("generous", new CTriangularMembershipFunction(20.0, 25.0, 30.0)));
   fsTips.Output().Add(fvTips);
//--- 创建三个马姆达尼模糊规则
   rule1 = fsTips.ParseRule("if (service is poor )  or (food is rancid) then tips is cheap");
   rule2 = fsTips.ParseRule("if ((service is good)) then tips is average");
   rule3 = fsTips.ParseRule("if (service is excellent) or (food is delicious) then (tips is generous)");
   
   fsTips.Rules().Add(rule1);
   fsTips.Rules().Add(rule2);
   fsTips.Rules().Add(rule3);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 专家去初始化函数|
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| 专家勾选功能|
//+------------------------------------------------------------------+
void OnTick()
  { 
   int ir; 
   for(ir=1; ir<10; ir++)
   { 
   p_od_Service.SetAll(fvService, ir);
   p_od_Food.SetAll(fvFood, ir);
   Print(CheckPointer(in));
   in.Clear();
   in.Add(p_od_Service);
   in.Add(p_od_Food);
//--- 获取结果
   
   result=fsTips.Calculate(in);
   Print("Error");
   p_od_Tips=result.GetNodeAtIndex(0);
   Print("Tips, %: ",p_od_Tips.Value());
   }
  }
//+------------------------------------------------------------------+

一般来说,函数库是否设计为可以创建对象,然后只获取结果,比如在每个新的条形图上?因为每次重新创建逻辑既慢又不经济。

 

修正库版本,现在只需创建一次模糊逻辑对象,然后只调用计算()

检查示例:

//+------------------------------------------------------------------+
//|TipsSample.mq5
//| 版权所有 2017, MetaQuotes Software Corp.
//|https://www.mql5.com | |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Math\Fuzzy\MamdaniFuzzySystem.mqh>

input double   Service;
input double   Food;
//+------------------------------------------------------------------+
//| 专家初始化函数|
//+------------------------------------------------------------------+
CMamdaniFuzzySystem *FSTips=new CMamdaniFuzzySystem();
CFuzzyVariable *FVService=new CFuzzyVariable("service",0.0,10.0);
CFuzzyVariable *FVFood=new CFuzzyVariable("food",0.0,10.0);
CFuzzyVariable *FVTips=new CFuzzyVariable("tips",0.0,30.0);
CMamdaniFuzzyRule *Rule1,*Rule2,*Rule3;

CList *In=new CList;
CDictionary_Obj_Double *Dic_Service=new CDictionary_Obj_Double;
CDictionary_Obj_Double *Dic_Food=new CDictionary_Obj_Double;
CDictionary_Obj_Double *Dic_Tips;
//+------------------------------------------------------------------+
//||
//+------------------------------------------------------------------+
int OnInit()
  {
   In.FreeMode(false);
//--- 为系统创建第一个输入变量

   FVService.Terms().Add(new CFuzzyTerm("poor", new CTriangularMembershipFunction(-5.0, 0.0, 5.0)));
   FVService.Terms().Add(new CFuzzyTerm("good", new CTriangularMembershipFunction(0.0, 5.0, 10.0)));
   FVService.Terms().Add(new CFuzzyTerm("excellent", new CTriangularMembershipFunction(5.0, 10.0, 15.0)));
   FSTips.Input().Add(FVService);
//-- 为系统创建第二个输入变量

   FVFood.Terms().Add(new CFuzzyTerm("rancid", new CTrapezoidMembershipFunction(0.0, 0.0, 1.0, 3.0)));
   FVFood.Terms().Add(new CFuzzyTerm("delicious", new CTrapezoidMembershipFunction(7.0, 9.0, 10.0, 10.0)));
   FSTips.Input().Add(FVFood);
//--- 创建输出

   FVTips.Terms().Add(new CFuzzyTerm("cheap", new CTriangularMembershipFunction(0.0, 5.0, 10.0)));
   FVTips.Terms().Add(new CFuzzyTerm("average", new CTriangularMembershipFunction(10.0, 15.0, 20.0)));
   FVTips.Terms().Add(new CFuzzyTerm("generous", new CTriangularMembershipFunction(20.0, 25.0, 30.0)));
   FSTips.Output().Add(FVTips);
//--- 创建三个马姆达尼模糊规则
   Rule1 = FSTips.ParseRule("if (service is poor )  or (food is rancid) then tips is cheap");
   Rule2 = FSTips.ParseRule("if ((service is good)) then tips is average");
   Rule3 = FSTips.ParseRule("if (service is excellent) or (food is delicious) then (tips is generous)");

   FSTips.Rules().Add(Rule1);
   FSTips.Rules().Add(Rule2);
   FSTips.Rules().Add(Rule3);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 专家去初始化函数|
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- 删除模糊系统
   In.FreeMode(true);  
   delete In;  
   delete FSTips;  
  }
//+------------------------------------------------------------------+
//| 专家勾选功能|
//+------------------------------------------------------------------+
void OnTick()
  {
   for(int ir=1; ir<10; ir++)
     {
      Dic_Service.SetAll(FVService,ir);
      Dic_Food.SetAll(FVFood,ir);
      In.Clear();
      In.Add(Dic_Service);
      In.Add(Dic_Food);
      //--- 获取结果
      CList *result=FSTips.Calculate(In);
      Dic_Tips=result.GetNodeAtIndex(0);
      Print("Tips, %: ",Dic_Tips.Value());
      delete result;
     }
  }
//+------------------------------------------------------------------+
附加的文件:
Fuzzy.zip  33 kb
 

h

when i updated metatrader to build 2342

all of samples with fuzzy logic library

return error "incorrect casting of pointers" on MQL5 \ Include \ Math \ Fuzzy \ RuleParser.mqh Line 712

please help to fix bug

many thanks