FuzzyLogic is a header-only C++ library for fuzzy set operations and inference. It utilizes sparse storage mechanisms to efficiently handle fuzzy sets and relations defined over arbitrary data types (integers, strings, enums).
- Sparse Representation: Only stores elements with non-zero membership.
- Algebraic Operations: Supports Union (
+), Intersection (*), and Complement (-unary). - Element-wise Implication: The
Implicatemethod allows modifying a set based on another set over the same universe using various rules (Mamdani, Larsen, Zadeh). - Built-in Inference: Includes
GeneralizedModusPonensandGeneralizedModusTollenswhich automatically construct an internal Zadeh relation to deduce consequences.
-
Relation Construction: Create relations from:
- Raw matrix data (
vector<vector<T>>). - The Cartesian product of two Fuzzy Sets (using standard factory functions).
- Arbitrary custom function rules.
- Raw matrix data (
-
Composition: Supports Max-Min Composition (
$R \circ S$ ) for chaining relations. - Logic Operations: Union, Intersection, and Complementation of 2D relations.
- C++17
The following example shows two distinct workflows:
- Set Operations: Manipulating sets element-wise.
- Inference: Using the Generalized Modus Ponens (GMP) to deduce a conclusion based on an observation.
#include "FuzzySets/fuzzy_set.hpp"
#include "FuzzyRelation/fuzzy_relation.hpp"
#include <iostream>
int main() {
// --- 1. Define Fuzzy Sets ---
// "Pressure is High"
FuzzySet<int> pressureHigh;
pressureHigh.insert(50, 0.2);
pressureHigh.insert(70, 0.8);
pressureHigh.insert(90, 1.0);
// "Valve is Open"
FuzzySet<int> valveOpen;
valveOpen.insert(1, 0.1); // State 1
valveOpen.insert(2, 0.5); // State 2
valveOpen.insert(3, 0.9); // State 3
// --- 2. Inference (Generalized Modus Ponens) ---
// Rule: IF Pressure is High THEN Valve is Open
// Fact: Observed Pressure is "More or less High"
FuzzySet<int> observedPressure;
observedPressure.insert(50, 0.4);
observedPressure.insert(70, 0.9);
observedPressure.insert(90, 1.0);
// Calculate Resulting Valve State
// Internally, this constructs a relation using Zadeh Implication:
// R(x,y) = (A(x) AND B(y)) OR (NOT A(x))
FuzzySet<int> result = pressureHigh.GeneralizedModusPonens(valveOpen, observedPressure);
std::cout << "Inferred Valve States:\n";
for(const auto& [state, membership] : result) {
std::cout << "State " << state << ": " << membership << "\n";
}
// --- 3. Creating Explicit Relations (Optional) ---
// If you need the raw Relation Matrix for Mamdani (Min) implication:
FuzzyRelation<int> mamdaniRel = Implication(pressureHigh, valveOpen, ImplicationType::MINIMUM);
// You can now compose this relation with other relations
// mamdaniRel.Composition(otherRelation)...
return 0;
}It is important to distinguish between the two types of "Implication" in this library:
-
FuzzySet::Implicate(other, type)(Member Function)-
Returns:
FuzzySet<T>& - Behavior: Modifies this set by performing an element-wise operation with other.
-
Example: If
typeisMINIMUM, it computes$\mu_{A'}(x) = \min(\mu_A(x), \mu_B(x))$ for all$x$ . This is useful for combining sets on the same universe.
-
Returns:
-
Implication(left, right, type)(Free Function)-
Returns:
FuzzyRelation<T> - Behavior: Creates a 2D matrix (relation) mapping elements from set left to set right.
-
Example: Used to create the "Rule Base" matrix (e.g., Mamdani
$R = A \times B$ ).
-
Returns:
The FuzzySet class provides high-level inference methods that abstract away the relation construction:
-
GeneralizedModusPonens(B, A'):- Given rule
$A \to B$ and fact$A'$ , deduce$B'$ . -
Logic: Uses Zadeh Implication
$(A \times B) \cup (\neg A \times \mathbb{U})$ internally to build the relation, then composes with$A'$ .
- Given rule
-
GeneralizedModusTollens(B, B'):- Given rule
$A \to B$ and fact$B'$ , deduce$A'$ .
- Given rule
The library supports standard definitions for fuzzy implications (used in FuzzyRelation creation or element-wise set implication):
| Enum Type | Logic |
|---|---|
MINIMUM |
Mamdani: |
ALGPRODUCT |
Larsen: |
ZADEHMAXMIN |
|
ZADEHARITHMETIC |
mkdir build && cd build
cmake ..
make