14,209,923 members
Article
Posted 11 Jun 2019

10.7K views
4 bookmarked

# C# Fuzzy Logic API

,
Rate this:
13 Jun 2019     MIT
This API executes inferences by fuzzy logic concept on Plain Old CLR Object associating an predicate defined in .NET native object called 'Expression'.

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

## 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 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 practical purpose.

## 2) Fuzzy Logic Concepts

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

## 3) Using the API

### 3.1) Core Flow

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

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 the illustrative example above, let's create a `Model` class that represents the Honest character like integrity, truth and justice sense percentage assessment for all and a boolean expression object that identifies the Honesty Profile, considering the minimal percentage to be an honest person:

```[Serializable, XmlRoot]
public class HonestAssesment
{
[XmlElement]
public int IntegrityPercentage { get; set; }

[XmlElement]
public int TruthPercentage { get; set; }

[XmlElement]
public int JusticeSensePercentage { get; set; }

[XmlElement]
public int MistakesPercentage
{
get
{
return ((100-IntegrityPercentage) + (100-TruthPercentage) +
(100-JusticeSensePercentage))/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 that 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.

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

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

//Third group of assessment:
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:

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

Look at "`HitsPercentage`" property. The inference on Profile 1, with 66% of Honest.

```{
"ID": 0,
"HitsPercentage": "66%",
"Data": {
"IntegrityPercentage": 90,
"TruthPercentage": 70,
"JusticeSensePercentage": 80,
"MistakesPercentage": 20
},
"PropertiesNeedToChange": [
"IntegrityPercentage"
],
"RatingsReport": [
false,
true,
true
],
"ErrorsQuantity": 1
}```
```HonestAssesment profile2 = new HonestAssesment()
{
IntegrityPercentage = 50,
JusticeSensePercentage = 63,
TruthPercentage = 30
};
string infer_profile2 = FuzzyLogic<HonestAssesment>.GetInferenceResult
(_honestyProfile, ResponseType.Json, profile2);```

The inference on Profile 2, with 33% of Honest, that is "Sometimes honest", like a tutorialspoint figure.

```{
"ID": 0,
"HitsPercentage": "33%",
"Data": {
"IntegrityPercentage": 50,
"TruthPercentage": 30,
"JusticeSensePercentage": 63,
"MistakesPercentage": 52
},
"PropertiesNeedToChange": [
"IntegrityPercentage",
"TruthPercentage"
],
"RatingsReport": [
false,
true,
false
],
"ErrorsQuantity": 2
}```
```HonestAssesment profile3 = new HonestAssesment()
{
IntegrityPercentage = 46,
JusticeSensePercentage = 48,
TruthPercentage = 30
};
string infer_profile3 = FuzzyLogic<HonestAssesment>.GetInferenceResult
(_honestyProfile, ResponseType.Json, profile3);```

The inference on Profile 3, with 0% of Honest, that is "Extremely dishonest", like a figure above.

```{
"ID": 0,
"HitsPercentage": "0%",
"Data": {
"IntegrityPercentage": 46,
"TruthPercentage": 30,
"JusticeSensePercentage": 48,
"MistakesPercentage": 58
},
"PropertiesNeedToChange": [
"IntegrityPercentage",
"JusticeSensePercentage",
"TruthPercentage"
],
"RatingsReport": [
false,
false,
false
],
"ErrorsQuantity": 3
}```
```HonestAssesment profile4 = new HonestAssesment()
{
IntegrityPercentage = 91,
JusticeSensePercentage = 83,
TruthPercentage = 81
};
string infer_profile4 = FuzzyLogic<HonestAssesment>.GetInferenceResult
(_honestyProfile, ResponseType.Json, profile4);```

The inference on Profile 4, with 100% of Honest, that is "Extremely honest", like a figure assessment.

```{
"ID": 0,
"HitsPercentage": "100%",
"Data": {
"IntegrityPercentage": 91,
"TruthPercentage": 81,
"JusticeSensePercentage": 83,
"MistakesPercentage": 15
},
"PropertiesNeedToChange": [],
"RatingsReport": [
true,
true,
true
],
"ErrorsQuantity": 0
}```

### 3.2) Design Pattern

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

```//Like a inference object...
Inference<ModelToInfere> inferObj =
FuzzyLogic<ModelToInfere>.GetInferenceResult(_expressionArg, modelObj);

//... get as XML string...
string inferXml = FuzzyLogic<ModelToInfere>.GetInferenceResult
(_expressionArg, ResponseType.Xml, modelObj);

//...or json string.
string inferJson = FuzzyLogic<ModelToInfere>.GetInferenceResult
(_expressionArg, ResponseType.Json, modelObj);```

### 3.3) Dependencies

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`).

## History

• 13th June, 2019: Initial version

## Share

 Architect teeWe Brazil
+14 years of experience in Information Technology, with a background in Software Architecture / Engineering focused on Microsoft Solutions; the entire Development experience is in UI / UX, Desktop and Web Development (both Front End and Back End) through JavaScript and C# languages along with the ability to planning, configure and manage network infrastructure environments in any Windows ecosystem; Then, I gained the technical profile of "Solution Architect", capable of working on critical mission projects on the Microsoft platform, providing aspects in all layers involved: Operating system, database, Web servers, configuration of domain controllers, Integrated SharePoint Services, Server-side development in C # and client-side in HTML5, CSS3 and JavaScript (JQuery) to the client-side interface, along with administration, maintenance and security plans.

 First Prev Next
 Please do not use percentages in fuzzy logic Emile van Gerwen17-Jun-19 3:24 Emile van Gerwen 17-Jun-19 3:24
 nice Member 1449972013-Jun-19 18:51 Member 14499720 13-Jun-19 18:51
 typo ? Member 1411878512-Jun-19 5:23 Member 14118785 12-Jun-19 5:23
 Re: typo ? AntonioLeonardo12-Jun-19 13:10 AntonioLeonardo 12-Jun-19 13:10
 Last Visit: 18-Jun-19 4:56     Last Update: 18-Jun-19 4:56 Refresh 1