Click here to Skip to main content
11,647,659 members (54,149 online)
Click here to Skip to main content

Fuzzinator: A Fuzzy Logic Controller

, 20 Jan 2010 CPOL 71.8K 5.3K 56
Rate this:
Please Sign up or sign in to vote.
Fuzzy Logic Controller C# Library based on mamdani Inference Engine + Windows Forms GUI

FuzzyLogicInterface.jpg

FLCInterface2.jpg

Introduction  

This article is about a fuzzy logic controller based on mamdani Inference Engine. Fuzzy logic is an approximation process, in which crisp inputs are turned to fuzzy values based on linguistic variables, set of rules and the inference engine provided. For example:

I have a train, I considered the Linguistic Variable "Speed" of the train has 2 membership functions Low and High, in which "Low" has range from 0 mph - 10 mph and "High" from 10 mph - 20 mph.

So If I got a Crisp input of 5 -> its fuzzy value is Low.

Imagine more if the range from 0-10 is not constant but a triangular graph with the max height of 1 and for Low a triangle graph with 3 distinct points 0,5,10.

Now for a singleton input of 4, the fuzzy value = y = (4-0)/(5-0) = 0.8, therefore our fuzzy input = 0.8 Low, imagine that the linguistic variables are intersecting so that a single input can be defined as (0.8 Low 0.4 High).

This process is called fuzzification...

After we get the fuzzy inputs, we compare it against a rule base. A rule base is a set of rules that is responsible for final output.

For example, if I said my rules are:

Rule 1: If Speed is High, Then Brake is Pushed

Rule 2: If Speed is Low, Then Brake is Released

So now using our Inference Engine, the Engine that would compare and deduct our fuzzy output, with fuzzy input ( 0.8 Low , 0.4 High) we can say based upon the rules mentioned above that the brakes will be down by (0.4 Pushed 0.8 Released) --> Fuzzy output.

This is the Rule Base and Inference Engine Process using mamdani concept...

Then the fuzzy output enters the defuzzification method, which is a method to weigh the fuzzy outputs and return a crisp output force which will be applied to the brakes.

FuzzySystem.png

Fig. 1-1

Using the Code

In the project attached with this article, 3 sub projects exist:

  1. A C# class library contains the engine that implements the steps talked about in the above example.
  2. A Window Forms Project FuzzyLogicUI which is a User Interface built for this fuzzy logic controller, you can add linguistic variables and membership functions, rules and a result panel to show you the different steps of the system.
  3. A console based test to the Fuzzy Logic Controller for better understanding of the system.

The Fuzzy Library (DLL)

The Class Diagram

ClassDiagram1.png - Click to enlarge image

Steps

1- Configure Your Fuzzy Controller

The allowed configuration for this system, is the (And Logic Connection) and Implication.

For a Rule: IF X1 is Low And X2 is High -> Y1 is Low.

How to calculate the final firing strength, if X1-> 0.8 X2-> 0.4 so Y1-> ?

If we use an (AND Connection) in this system you can define the AND Method between product or minimum , so for Y1 = min(0.8,0.4) = 0.4 due to an "AND" connection between X1 & X2.

Config configure = new Config(ImpMethod.Prod,ConnMethod.Min);

2- Create Your Linguistic Variables and its Membership Functions

Using above Train example: we define the Train to have speed for input. For speed: High, Med, Low for the result (output) Brake: Release, Push.

LingVariable speed = new LingVariable("Speed", VarType.Input); //declare new lingustic variable
speed.setRange(0, 35); // set max - min range for our variables 0 mph -35 mph
speed.addMF(new Trapmf("Low", -10, 0, 10, 15)); // trapmf: trapazoid shape behaviour
speed.addMF(new Trimf("Medium", 10, 20, 30)); // trimf: triangle shape behaviour
speed.addMF(new Trapmf("High", 25, 30, 35, 40));

LingVariable brake = new LingVariable("Brake", VarType.Output);
brake.setRange(0, 65);  // Brake Force
brake.addMF(new Trapmf("Released", -10, 0, 20, 41));
brake.addMF(new Trapmf("Pushed", 41, 60, 65, 70));

3- Crisp Inputs and Fuzzification Process

Fuzzification is the process of evaluating the crisp input against the membership functions of the lingstic variable Speed, membershipfunctionis an abstract class, with an abstract method "getOutput", any object inheriting this class needs to define their getOutput(double) function, such as in Trimfand Trapmf. Based upon this feature, more membershipfunctions can be implemented to extend the fuzzy controller capabilities.

Fuzzificationmethod then returns a list of fuzzy numbers, each fuzzy number consists of a function name and firing strength from getOutput(double).

Example: fuzzynumber(0.8,"Low") FuzzyNumber(0.4,"High")

The list of fuzzy numbers is then added to a fuzzy Set with list of Fuzzy numbers and linguistic Variable name for further processing.

FLC c = new FLC(conf);
double speedCrispVal = 30;
FuzzySet fuzzy_speed = new FuzzySet(c.Fuzzification(speedCrispVal,speed), speed.Name);

After we fuzzify all our inputs, add it to a list of fuzzysets:

List<FuzzySet> input_sets = new List<FuzzySet>();
input_sets.Add(fuzzy_speed);

4- Make the rules !

Define the rulebase, the rules that our system uses to approximate.

For Rule 1, from the above example:

"IF Speed is High, Then Brake is pushed."

List<ruleitem> rule1in = new List<ruleitem>();
List<ruleitem> rule1out = new List<ruleitem>();

// the if part of the Rule, add more than one if X1 and X2, 
// add another RuleItem in the list
rule1in.AddRange(new RuleItem[1] { new RuleItem("Speed", "High") } );

// the then part in the Rule
rule1out.AddRange(new RuleItem[1] { new RuleItem("Brake", "Pushed") } );

// List of rules "RuleBase" passed to the Inference Engine
List<rule> rules = new List<rule>();
rules.Add(new Rule(rule1in, rule1out, Connector.And));

5- Evaluate the Rule

During the rules evaluation at the Inference system, the firing strength of the output is determined as in this example:

Fuzzy input: X1 = 0.8 Low X2 = 0.5 High.

Rule: "IF X1 Low and X2 High Then Y1 is Low."

Configuration: And connection evaluated by min(X1,X2).

Evaluation: min(X1,X2) => min(0.8,0.5) = Y1 = 0.5 Low.

Fuzzy out: Y1 = 0.5 Low.

If more rules are overlapping, such as "IF X1 is High, THEN Y1 is Low" where the Y1 is 0.7 Low, the maximum of Y1 firing strengths is taken, in this case ( 0.7 ).

InferEngine engine = new InferEngine(configure, rules, input_sets);
List<FuzzySet> fuzzy_out = engine.evaluateRules();

6- DeFuzzification and Crisp Output

The Defuzzificationmethod is a method to calculate the fuzzy out to convert to a crisp value. In this system, two methods of defuzzification are used, you can choose between them in Configuration.

Centroid Defuzzification, Modified High Defuzzification.

double crisp_brake_force = c.DeFuzzification(fuzzy_out, brake);

Points of Interest

This project has been made very flexible and extendable for future development and upgrading. The GUI is made to realise the Fuzzy Logic controller and to try it out, it is made mainly for educational purposes. The FuzzySets and FuzzyNumberclasses are provided for more depth in fuzzy process.

For more tests and examples, try GUI Project Predefined Tests, or the console based test project "FuzzyTest". 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

homran
Software Developer (Junior)
Egypt Egypt
My Name is Hesham Omran, I am a Digital Media Engineer graduated from the German University in Cairo, with background in various languages C++/VC++, PHP, SQL, C#, MATLAB,....etc. I am a big fan of CodeProject, it saved my life a couple of time, so I thought of giving back some ....

You may also be interested in...

Comments and Discussions

 
Questionc# to c++, its possible? Pin
Tima Sama3-May-15 6:34
memberTima Sama3-May-15 6:34 
Questionsave Pin
Member 1048880914-May-14 5:50
memberMember 1048880914-May-14 5:50 
AnswerRe: save Pin
FireLin10-Dec-14 2:04
memberFireLin10-Dec-14 2:04 
QuestionGood Work. Pin
swad986-Apr-13 10:18
memberswad986-Apr-13 10:18 
GeneralMy vote of 5 Pin
Shayan Ali Bhatti6-Mar-13 0:15
memberShayan Ali Bhatti6-Mar-13 0:15 
GeneralMy vote of 5 Pin
Kanasz Robert6-Nov-12 0:02
mvpKanasz Robert6-Nov-12 0:02 
QuestionA problem with project Pin
Mariusz Domagała16-Jan-12 11:55
memberMariusz Domagała16-Jan-12 11:55 
QuestionFuzzy Wang Mendel Pin
aeranginkaman17-Dec-11 20:06
memberaeranginkaman17-Dec-11 20:06 
AnswerRe: Fuzzy Wang Mendel Pin
FireLin18-Dec-11 0:18
memberFireLin18-Dec-11 0:18 
Questionmany many thanks Pin
outta1323-Sep-11 12:47
memberoutta1323-Sep-11 12:47 
GeneralVery nice article Pin
anupamaz29-Dec-10 7:00
memberanupamaz29-Dec-10 7:00 
GeneralGaussian MF Pin
AlbertoBenavides23-Nov-10 7:06
memberAlbertoBenavides23-Nov-10 7:06 
AnswerRe: Gaussian MF Pin
FireLin23-Nov-10 17:17
memberFireLin23-Nov-10 17:17 
GeneralHaving issues adding more than one rule, it just appends new rules to old rule Pin
pharaohautomation29-Jul-10 18:15
memberpharaohautomation29-Jul-10 18:15 
GeneralRe: Having issues adding more than one rule, it just appends new rules to old rule Pin
FireLin23-Nov-10 17:23
memberFireLin23-Nov-10 17:23 
Dear pharaohautomation, I am very sorry I did not reply soon, I am not in to vb.net. I can take a look though on the code, It would be great to convert to vb, as it was converted by other student into java too. find project at sourceforge : JFuzzinator. Thank you very much and sorry again, if you still want me to look at it, I would be more than glad. regards,
HO

NewsFuzzinator's Cousin is here : JFuzzinator Pin
FireLin6-Jun-10 22:12
memberFireLin6-Jun-10 22:12 
GeneralRe: Fuzzinator's Cousin is here : JFuzzinator Pin
MG Husny Ahmad24-Jan-14 17:16
memberMG Husny Ahmad24-Jan-14 17:16 
GeneralPlease give some explanation Pin
sammeeah20-Jan-10 19:31
membersammeeah20-Jan-10 19:31 
GeneralRe: Please give some explanation Pin
FireLin20-Jan-10 20:06
memberFireLin20-Jan-10 20:06 
QuestionFuzzy Logic Controller Gaussian Membership Function Pin
inancigdem3-Jan-10 20:09
memberinancigdem3-Jan-10 20:09 
AnswerRe: Fuzzy Logic Controller Gaussian Membership Function Pin
FireLin4-Jan-10 0:22
memberFireLin4-Jan-10 0:22 
GeneralRe: Fuzzy Logic Controller Gaussian Membership Function Pin
inancigdem4-Jan-10 0:55
memberinancigdem4-Jan-10 0:55 
GeneralRe: Fuzzy Logic Controller Gaussian Membership Function Pin
inancigdem5-Jan-10 22:37
memberinancigdem5-Jan-10 22:37 
GeneralRe: Fuzzy Logic Controller Gaussian Membership Function Pin
FireLin7-Jan-10 0:38
memberFireLin7-Jan-10 0:38 
GeneralRe: Fuzzy Logic Controller Gaussian Membership Function Pin
inancigdem7-Jan-10 1:37
memberinancigdem7-Jan-10 1:37 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150804.3 | Last Updated 21 Jan 2010
Article Copyright 2009 by homran
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid