C#模糊逻辑API

This Api executes inferences by fuzzy logic concept on C# Plain Old CLR Object associating an Expression object defined in native .NET Framework.

此Api通过模糊逻辑概念对C#普通旧CLR对象执行推理,该对象与本机.NET Framework中定义的Expression对象相关联。

1)开始之前:抽象 (1) Before you begin: Abstraction)

If do you want to delve into Fuzzy Logic theory (such as mathematical theorems, postulates, and Morgan's law) it's strongly recommended to look for other references to satisfy your curiosity and / or your research need. Through here this git post, you'll access only a practical example to execute the Fuzzy Logic in real world applications; then, the focus in this article is not diving on philosophical dialogue with only a pratical porpuse. The new version update of this API using iteractions with Parallelism or Yield in some portions of code to increase performance; another advance are bugs fixes such like: lack of properties in XML output, output of inference in decimal numbers (following the most diffuse logical theory), and calibration of output values for greater accuracy. Important note: All constructors stay the same, with no impact to the any developer project.

如果您想研究模糊逻辑理论(例如数学定理,假设和摩根定律 ),强烈建议您寻找其他参考资料,以满足您的好奇心和/或您的研究需求。 通过这篇git帖子,您将仅访问一个实际示例在实际应用中执行模糊逻辑。 因此,本文的重点不是只用实用的用法来探讨哲学对话。 此API的新版本更新在代码的某些部分中使用了具有并行性Yield的迭代,以提高性能; 另一个改进是错误修复,例如:XML输出中缺少属性,以十进制数字形式进行推理输出(遵循最分散的逻辑理论)以及对输出值进行校准以提高准确性。 重要说明:所有构造函数都保持不变,不会影响任何开发人员项目。

2)模糊逻辑概念 (2) Fuzzy Logic Concepts)

This figure from tutorialspoint site resumes the real concept of Fuzzy Logic: Nothing is absolutily true or false (for Fuzzy Logic); between 0 and 1 you have a interval from these extremes, beyond the limits of the boolean logic.

来自tutorialspoint站点的此图恢复了模糊逻辑的真实概念:绝对没有(对模糊逻辑而言)是正确的或错误的; 在0和1之间,您有一个与这些极端值之间的间隔,超出了布尔逻辑的限制。

From: https://www.tutorialspoint.com/fuzzy_logic/fuzzy_logic_introduction.htm

3)使用API (3) Using the API)

3.1)核心流程 (3.1) Core flow)

The core concept had the first requirement: Defuzzyfication. In other words, generate a Fuzzy Logic results by Crisp Input expression builded on Fuzzy Logic Engine (view figure bellow, from Wikepdia reference):

核心概念有第一个要求:去模糊化。 换句话说,通过建立在模糊逻辑引擎上的Crisp Input表达式生成模糊逻辑结果(参见下面的图,来自Wikepdia参考):

From: https://es.wikipedia.org/wiki/Defuzzificaci%C3%B3n

The rule of Fuzzy Logic Engine is: break apart any complex boolean expression (crisp input) that resolves a logical boolean problem in minor boolean parts rule (about the theory used of this complex boolean expression, view articles like Many-Valued Logic or /Classical Logic).

模糊逻辑引擎的规则是:分解任何可解决次要布尔零件规则中的逻辑布尔问题的复杂布尔表达式(酥脆输入)(关于此复杂布尔表达式使用的理论,请查看诸如多值逻辑或/ 古典之类的文章)逻辑 )。

Based on illustrative example above, let's create a Model class that represents the Honest charater like integrity, truth and justice sense percentage assesment for all and a boolean expression object that identifies the Honesty Profile, considering the minimal percentage to be a honest person:

基于上面的说明性示例,让我们创建一个Model类,该类代表所有人的诚实品格,如完整性,真相和正义感百分比评估,以及一个布尔表达式对象,该对象标识诚实概况,并考虑将最小百分比视为诚实的人:

[Serializable, XmlRoot]
public class HonestAssesment
{
    [XmlElement]
    public double Integrity { get; set; }

    [XmlElement]
    public double Truth { get; set; }

    [XmlElement]
    public double JusticeSense { get; set; }

    [XmlElement]
    public double MistakesAVG
    {
        get
        {
            return (Integrity + Truth - JusticeSense) / 3;
        }
    }
}

//Crisp Logic expression that represents Honesty Profiles:
static Expression<Func<HonestAssesment, bool>> _honestyProfile = (h) =>
(h.IntegrityPercentage > 75 && h.JusticeSensePercentage > 75 && h.TruthPercentage > 75) || //First group
(h.IntegrityPercentage > 90 && h.JusticeSensePercentage > 60 && h.TruthPercentage > 50) || //Second group
(h.IntegrityPercentage > 70 && h.JusticeSensePercentage > 90 && h.TruthPercentage > 80) || //Third group
(h.IntegrityPercentage > 65 && h.JusticeSensePercentage == 100 && h.TruthPercentage > 95); //Last group

The boolean expression broken is one capacity derived from System.Linq.Expressions.Expression class, converting any block of code to representational string; the derived class who will auxiliate with this job is BinaryExpression: the boolean expression will be sliced in binary tree of smaller boolean expression, whose rule will prioritize the slice where the conditional expression is contained 'OR', then sliced by 'AND' conditional expression.

断开的布尔表达式是从System.Linq.Expressions.Expression类派生的一种能力,它将任何代码块都转换为表示形式的字符串; 与此工作相关的派生类是BinaryExpression :布尔表达式将在较小布尔表达式的二叉树中切片,其规则将优先处理条件表达式包含“ OR”的切片,然后按条件AND切片。

//First group of assesment:
h.IntegrityPercentage > 75;
h.JusticeSensePercentage > 75;
h.TruthPercentage > 75;

//Second group of assesment:
h.IntegrityPercentage > 90;
h.JusticeSensePercentage > 60;
h.TruthPercentage > 50;

//Third group of assesment:
h.IntegrityPercentage > 70;
h.JusticeSensePercentage > 90;
h.TruthPercentage > 80;

//Last group of assesment:
h.IntegrityPercentage > 65;
h.JusticeSensePercentage == 100;
h.TruthPercentage > 95;

This functionality contained in the .NET Framework is the trump card to mitigate the appraisal value that the evaluated profiles have conquered or how close they have come to reach any of the 4 defined valuation groups, for example:

.NET Framework中包含的此功能是一张王牌,可减轻评估的配置文件已征服的评估价值或它们达到4个定义的评估组中的任何一个的接近程度,例如:

HonestAssesment profile1 = new HonestAssesment()
{
    IntegrityPercentage = 90,
    JusticeSensePercentage = 80,
    TruthPercentage = 70
};
string inference_p1 = FuzzyLogic<HonestAssesment>.GetInference(_honestyProfile, ResponseType.Json, profile1);

Look at "HitsPercentage" and "InferenceResult" properties. The inference on Profile 1, with 0.67 of Honest (1 is Extremely Honest). -Result of "inference_p1" string variable (JSON):

查看“ HitsPercentage”和“ InferenceResult”属性。 对配置文件1的推断,其诚实度为0.67(1为“非常诚实”)。 -“ inference_p1”字符串变量(JSON)的结果:

{
    "ID":"72da723b-b879-474c-b2cc-6a11c5965b25",
    "InferenceResult":"0.67",
    "Data":
        {
            "IntegrityPercentage":90,
            "TruthPercentage":70,
            "JusticeSensePercentage":80,
            "MistakesPercentage":20
        },
    "PropertiesNeedToChange":["IntegrityPercentage"],
    "ErrorsQuantity":1
}

HonestAssesment profile2 = new HonestAssesment()
{
    IntegrityPercentage = 50,
    JusticeSensePercentage = 63,
    TruthPercentage = 30
};
string inference_p2 = FuzzyLogic<HonestAssesment>.GetInference(_honestyProfile, ResponseType.Xml, profile2);

The inference on Profile 2, with 33% of Honest, that is "Sometimes honest", like a tutorialspoint figure. --Result of "inference_p2" string variable (XML):

关于Profile 2的推断(占诚实的33%)是“有时是诚实的”,就像tutorialspoint图一样。 -“ inference_p2”字符串变量(XML)的结果:

<?xml version="1.0" encoding="utf-8"?>
<InferenceOfHonestAssesment>
  <PropertiesNeedToChange>IntegrityPercentage</PropertiesNeedToChange>
  <PropertiesNeedToChange>TruthPercentage</PropertiesNeedToChange>
  <ErrorsQuantity>2</ErrorsQuantity>
  <ID>efb7249e-568f-44d4-a8b3-ce728a243273</ID>
  <InferenceResult>0.33</InferenceResult>
  <Data>
    <IntegrityPercentage>50</IntegrityPercentage>
    <TruthPercentage>30</TruthPercentage>
    <JusticeSensePercentage>63</JusticeSensePercentage>
  </Data>
</InferenceOfHonestAssesment>

HonestAssesment profile3 = new HonestAssesment()
{
    IntegrityPercentage = 46,
    JusticeSensePercentage = 48,
    TruthPercentage = 30
};
var inference_p3 = FuzzyLogic<HonestAssesment>.GetInference(_honestyProfile, profile3);

The inference on Profile 3, with 0% of Honest, that is "Extremely dishonest", like a figure above. --Result of "inference_p3" Api Model variable (like "Inference<HonestAssesment" object) in imagem bellow:

如上图所示,对配置文件3的推断(诚实度为0%)为“极度不诚实”。 -在以下图像中,“ inference_p3” Api模型变量(如“ Inference <HonestAssesment”对象)的结果:

From: https://github.com/antonio-leonardo/FuzzyLogicApi

HonestAssesment profile4 = new HonestAssesment()
{
    IntegrityPercentage = 91,
    JusticeSensePercentage = 83,
    TruthPercentage = 81
};
List<HonestAssesment> allProfiles = new List<HonestAssesment>();
allProfiles.Add(profile1);
allProfiles.Add(profile2);
allProfiles.Add(profile3);
allProfiles.Add(profile4);
string inferenceAllProfiles = FuzzyLogic<HonestAssesment>.GetInference(_honestyProfile, ResponseType.Xml, allProfiles);

Inferences with all Profiles, in XML:

用XML推断所有配置文件:

<?xml version="1.0" encoding="utf-8"?>
<InferenceResultOfHonestAssesment>
  <Inferences>
    <PropertiesNeedToChange>IntegrityPercentage</PropertiesNeedToChange>
    <ErrorsQuantity>1</ErrorsQuantity>
    <ID>8d79084c-9402-4683-833d-437cad86ef4a</ID>
    <InferenceResult>0.67</InferenceResult>
    <Data>
      <IntegrityPercentage>90</IntegrityPercentage>
      <TruthPercentage>70</TruthPercentage>
      <JusticeSensePercentage>80</JusticeSensePercentage>
    </Data>
  </Inferences>
  <Inferences>
    <PropertiesNeedToChange>IntegrityPercentage</PropertiesNeedToChange>
    <PropertiesNeedToChange>TruthPercentage</PropertiesNeedToChange>
    <ErrorsQuantity>2</ErrorsQuantity>
    <ID>979d4ebe-6210-46f4-a492-00df88591d17</ID>
    <InferenceResult>0.33</InferenceResult>
    <Data>
      <IntegrityPercentage>50</IntegrityPercentage>
      <TruthPercentage>30</TruthPercentage>
      <JusticeSensePercentage>63</JusticeSensePercentage>
    </Data>
  </Inferences>
  <Inferences>
    <PropertiesNeedToChange>IntegrityPercentage</PropertiesNeedToChange>
    <PropertiesNeedToChange>JusticeSensePercentage</PropertiesNeedToChange>
    <PropertiesNeedToChange>TruthPercentage</PropertiesNeedToChange>
    <ErrorsQuantity>3</ErrorsQuantity>
    <ID>9004cb7a-b75d-4452-9d9a-c8836a5531eb</ID>
    <InferenceResult>0</InferenceResult>
    <Data>
      <IntegrityPercentage>46</IntegrityPercentage>
      <TruthPercentage>30</TruthPercentage>
      <JusticeSensePercentage>48</JusticeSensePercentage>
    </Data>
  </Inferences>
  <Inferences>
    <ErrorsQuantity>0</ErrorsQuantity>
    <ID>36545dae-1dde-4bfd-a528-ae42a7a0748f</ID>
    <InferenceResult>1.00</InferenceResult>
    <Data>
      <IntegrityPercentage>91</IntegrityPercentage>
      <TruthPercentage>81</TruthPercentage>
      <JusticeSensePercentage>83</JusticeSensePercentage>
    </Data>
  </Inferences>
</InferenceResultOfHonestAssesment>

3.2)设计模式 (3.2) Design Pattern)

The 'Fuzzy Logic API' developed with Singleton Design Pattern, structured with one private constructor, where have two arguments parameter: one Expression object and one POCO object (defined in Generic parameter); but the developer will get the inference result by one line of code

Singleton设计模式开发的“模糊逻辑API”,由一个私有构造函数构成,其中具有两个参数参数:一个Expression对象和一个POCO对象(在Generic参数中定义); 但是开发人员将通过一行代码获得推理结果

//Like a inference object...
Inference<ModelToInfere> inferObj = FuzzyLogic<ModelToInfere>.GetInference(_honestyProfileArg, modelObj);

//... get as xml string...
string inferXml = FuzzyLogic<ModelToInfere>.GetInference(_honestyProfileArg, ResponseType.Xml, modelObj);

//...or json string.
string inferJson = FuzzyLogic<ModelToInfere>.GetInference(_honestyProfileArg, ResponseType.Json, modelObj);

3.3)依存关系 (3.3) Dependecies)

To add Fuzzy Logic Api as an Assembly or like classes inner in your Visual Studio project, you'll need to install System.Linq.Dynamic dll, that can be installed by nuget reference or execute command on Nuget Package Console (Install-Package System.Linq.Dynamic).

要将Fuzzy Logic Api作为Assembly或类似的类添加到Visual Studio项目中的内部,您需要安装System.Linq.Dynamic dll,可以通过nuget引用安装该文件,也可以在Nuget软件包控制台(安装软件包系统)上执行命令.Linq.Dynamic)。



(Award)

Voted the Best Article of June/2019 by Code Project

Code Project评选为2019年6月最佳文章



执照 (License)

View MIT license

查看MIT许可证

翻译自: https://www.codeproject.com/Articles/5092762/Csharp-Fuzzy-Logic-API

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值