FuzzyBrain is a fuzzy logic inference engine written entirely in C++. It is designed to be simple to use, easy to extend, and extremely fast! You can take the code in the /src directory and add it directly to your project.
Every variable and fuzzy set in the system can be configured manually, and you can run the inference engine as many times as you want.
Conceptually, several elements play different roles within the fuzzy inference system:
| Component | Role |
|---|---|
FuzzySet |
Represents a single fuzzy set within a linguistic variable |
LinguisticVariable |
A linguistic variable that contains multiple fuzzy sets |
FuzzyRule |
A single fuzzy rule written in natural language |
FuzzyObject |
A single unit of fuzzy inference containing linguistic variables and rules |
Defuzzificators |
Objects used to defuzzify an output variable |
FuzzyEngine |
A container for multiple FuzzyObject instances |
Here is a basic implementation of a fuzzy calculator:
// First: creation of the fuzzy object
calculator = new MamdaniFuzzyObject();
/*
* Creation of the linguistic variables for input and output
*/
InputLinguisticVariable* x = new InputLinguisticVariable("x", -10, 10);
TrapezoidalFuzzySet* negative = new TrapezoidalFuzzySet("Negative", -10, -10, -1, 0);
TriangularFuzzySet* zero = new TriangularFuzzySet("Zero", -1, 0, 1);
TrapezoidalFuzzySet* positive = new TrapezoidalFuzzySet("Positive", 0, 1, 10, 10);
// Add sets to variable x
x->addSet(negative);
x->addSet(zero);
x->addSet(positive);
MamdaniOutputVariable* y = new MamdaniOutputVariable("y", -3, 3);
TriangularFuzzySet* negative_2 = new TriangularFuzzySet("Negative", -2, -1, 0);
TriangularFuzzySet* positive_2 = new TriangularFuzzySet("Positive", 0, 1, 2);
TriangularFuzzySet* largenegative = new TriangularFuzzySet("Largenegative", -3, -2, -1);
TriangularFuzzySet* largepositive = new TriangularFuzzySet("Largepositive", 1, 2, 3);
y->addSet(negative_2);
y->addSet(zero);
y->addSet(positive_2);
y->addSet(largenegative);
y->addSet(largepositive);
InputLinguisticVariable* z = new InputLinguisticVariable("z", -10, 10);
z->addSet(negative);
z->addSet(zero);
z->addSet(positive);
calculator->addInputVar(x);
calculator->setOutputVar(y);
calculator->addInputVar(z);
/*
* Adding a series of rules
*/
calculator->addRule(new MamdaniRule("IF x IS Zero AND z IS Negative THEN y IS Negative"));
calculator->addRule(new MamdaniRule("IF x IS Negative AND z IS Negative THEN y IS Largenegative"));
calculator->addRule(new MamdaniRule("IF x IS Negative AND z IS Zero THEN y IS Negative"));
calculator->addRule(new MamdaniRule("IF x IS Negative AND z IS Positive THEN y IS Zero"));
calculator->addRule(new MamdaniRule("IF x IS Zero AND z IS Zero THEN y IS Zero"));
calculator->addRule(new MamdaniRule("IF x IS Zero AND z IS Positive THEN y IS Positive"));
calculator->addRule(new MamdaniRule("IF x IS Positive AND z IS Negative THEN y IS Zero"));
calculator->addRule(new MamdaniRule("IF x IS Positive AND z IS Zero THEN y IS Positive"));
calculator->addRule(new MamdaniRule("IF x IS Positive AND z IS Positive THEN y IS Largepositive"));
/*
* Set input for the variables
*/
calculator->setInput("z", 1);
/*
* Fire the inference and get an output
*/
float output = calculator->getOutput();This is a basic case using only one FuzzyObject. If needed, a FuzzyEngine can be used to collect several FuzzyObject instances, manage relationships between objects, and handle inputs and outputs. Additionally, you can provide an EngineCreator to import the engine from XML or other sources.
All documentation for the source code can be found at: http://fuzzybrain.pablosproject.com/
All software components were developed using a Test-Driven Development (TDD) approach. All unit tests are stored in the /test folder and are written and executed using GoogleTest. For a guide on setting up a TDD environment using Google Test and Eclipse, you can watch: http://www.pablosproject.com/c-2/test-driven-developement-c-with-eclipse
If you have any questions, you can contact me at pablosproject@gmail.com or on Twitter @PablosProject.