Skip to content

A C++ template library for computational intelligence that models uncertainty using Fuzzy Set Theory. It provides containers and algorithms to manipulate degrees of truth (0.0 to 1.0) rather than binary boolean values.

License

Notifications You must be signed in to change notification settings

Gtollm/Fuzzy-Logic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FuzzyLogic

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

Key Features

Fuzzy Sets (FuzzySet<T>)

  • Sparse Representation: Only stores elements with non-zero membership.
  • Algebraic Operations: Supports Union (+), Intersection (*), and Complement (- unary).
  • Element-wise Implication: The Implicate method allows modifying a set based on another set over the same universe using various rules (Mamdani, Larsen, Zadeh).
  • Built-in Inference: Includes GeneralizedModusPonens and GeneralizedModusTollens which automatically construct an internal Zadeh relation to deduce consequences.

Fuzzy Relations (FuzzyRelation<T>)

  • 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.
  • Composition: Supports Max-Min Composition ($R \circ S$) for chaining relations.
  • Logic Operations: Union, Intersection, and Complementation of 2D relations.

Integration

Requirements

  • C++17

Usage Example

The following example shows two distinct workflows:

  1. Set Operations: Manipulating sets element-wise.
  2. 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;
}

API Details

Implication vs. Inference

It is important to distinguish between the two types of "Implication" in this library:

  1. FuzzySet::Implicate(other, type) (Member Function)

    • Returns: FuzzySet<T>&
    • Behavior: Modifies this set by performing an element-wise operation with other.
    • Example: If type is MINIMUM, 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.
  2. 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$).

Inference Methods

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'$.
  • GeneralizedModusTollens(B, B'):
    • Given rule $A \to B$ and fact $B'$, deduce $A'$.

Implication Types

The library supports standard definitions for fuzzy implications (used in FuzzyRelation creation or element-wise set implication):

Enum Type Logic
MINIMUM Mamdani: $\min(\mu_A, \mu_B)$
ALGPRODUCT Larsen: $\mu_A \cdot \mu_B$
ZADEHMAXMIN $\max(1 - \mu_A, \mu_B)$
ZADEHARITHMETIC $\min(1, 1 - \mu_A + \mu_B)$

Building

mkdir build && cd build
cmake ..
make

About

A C++ template library for computational intelligence that models uncertainty using Fuzzy Set Theory. It provides containers and algorithms to manipulate degrees of truth (0.0 to 1.0) rather than binary boolean values.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published