Click here to Skip to main content
14,355,411 members

C# Fuzzy Logic API

Rate this:
4.53 (22 votes)
Please Sign up or sign in to vote.
4.53 (22 votes)
3 Aug 2019MIT
This API executes inferences by fuzzy logic concept on Plain Old CLR Object associating a 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.

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

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 built on Fuzzy Logic Engine (view figure below, from Wikepdia reference):

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 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 a 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 assessment:
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 inference_p1 = FuzzyLogic<HonestAssesment>.GetInferenceResult
                       (_expression, ResponseType.Json, profile1);

Look at the "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 inference_p2 = FuzzyLogic<HonestAssesment>.GetInferenceResult
                      (_expression, 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 inference_p3 = FuzzyLogic<HonestAssesment>.GetInferenceResult
                      (_expression, 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 inference_p4 = FuzzyLogic<HonestAssesment>.GetInferenceResult
                      (_expression, 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 inner classes 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

  • 11th June, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

AntonioLeonardo
Architect teeWe
Brazil Brazil
+14 years of experience in Computing and IT, with a background in Software Architecture/Engineering focused on Microsoft Solutions and Windows Ecosystem; working on critical mission projects on the Microsoft platforms, act on all aspects and all layers involved.

Certifications:
Microsoft Certified professional (MCP), Microsoft Certified technology specialist (MCTS)
Acknowledgement:
Master in Business Administration (MBA)

Comments and Discussions

 
Questiongood example for Fuzzy logic Pin
Mou_kol8-Aug-19 21:47
memberMou_kol8-Aug-19 21:47 
AnswerRe: good example for Fuzzy logic Pin
AntonioLeonardo9-Aug-19 3:39
professionalAntonioLeonardo9-Aug-19 3:39 
GeneralMy vote of 5 Pin
Thiago Moreira7-Jul-19 17:17
professionalThiago Moreira7-Jul-19 17:17 
GeneralRe: My vote of 5 Pin
AntonioLeonardo16-Jul-19 5:23
professionalAntonioLeonardo16-Jul-19 5:23 
GeneralPlease do not use percentages in fuzzy logic Pin
Emile van Gerwen17-Jun-19 4:24
memberEmile van Gerwen17-Jun-19 4:24 
GeneralRe: Please do not use percentages in fuzzy logic Pin
AntonioLeonardo18-Jun-19 13:24
professionalAntonioLeonardo18-Jun-19 13:24 
GeneralRe: Please do not use percentages in fuzzy logic Pin
Nelek4-Jul-19 20:09
protectorNelek4-Jul-19 20:09 
GeneralRe: Please do not use percentages in fuzzy logic Pin
AntonioLeonardo5-Jul-19 16:41
professionalAntonioLeonardo5-Jul-19 16:41 
GeneralRe: Please do not use percentages in fuzzy logic Pin
MKJCP15-Jul-19 7:53
memberMKJCP15-Jul-19 7:53 
GeneralRe: Please do not use percentages in fuzzy logic Pin
AntonioLeonardo16-Jul-19 5:23
professionalAntonioLeonardo16-Jul-19 5:23 
Questionnice Pin
Member 1449972013-Jun-19 19:51
memberMember 1449972013-Jun-19 19:51 
AnswerRe: nice Pin
AntonioLeonardo18-Jun-19 13:24
professionalAntonioLeonardo18-Jun-19 13:24 
Questiontypo ? Pin
Member 1411878512-Jun-19 6:23
memberMember 1411878512-Jun-19 6:23 
AnswerRe: typo ? Pin
AntonioLeonardo12-Jun-19 14:10
professionalAntonioLeonardo12-Jun-19 14:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 3 Aug 2019

Stats

23.1K views
335 downloads
28 bookmarked