Module mamdani

Module mamdani 

Source
Expand description

Mamdani inference: rules, activation, and implication (clipping).

This module defines the core building blocks of a Mamdani-style fuzzy inference system:

  • Rule: pairs an Antecedent (IF) with one or more Consequents (THEN).
  • Rule::activation: evaluates the antecedent using the default Min–Max operator family (AND=min, OR=max, NOT=1-x) to produce an activation degree alpha ∈ [0, 1] for the current crisp inputs.
  • Rule::implicate: applies implication using clipping (min(alpha, μ_B(x))) to produce discretized output membership samples for each consequent.

The resulting per-variable membership samples can be aggregated across rules (see crate::aggregate) and then defuzzified to crisp outputs (see crate::defuzz).

Example

use std::collections::HashMap;
use rust_fuzzylogic::prelude::*;
use rust_fuzzylogic::membership::triangular::Triangular;
use rust_fuzzylogic::term::Term;
use rust_fuzzylogic::variable::Variable;
use rust_fuzzylogic::antecedent::Antecedent;
use rust_fuzzylogic::mamdani::{Rule, Consequent};

// Variable and terms
let mut temp = Variable::new(-10.0, 10.0).unwrap();
temp.insert_term("hot", Term::new("hot", Triangular::new(0.0, 5.0, 10.0).unwrap())).unwrap();
let mut fan = Variable::new(0.0, 10.0).unwrap();
fan.insert_term("high", Term::new("high", Triangular::new(5.0, 7.5, 10.0).unwrap())).unwrap();

// IF temp IS hot THEN fan IS high
let rule = Rule{
    antecedent: Antecedent::Atom{ var: "temp".into(), term: "hot".into() },
    consequent: vec![Consequent{ var: "fan".into(), term: "high".into() }],
};

// Activation for a crisp input
let mut inputs: HashMap<&str, Float> = HashMap::new();
inputs.insert("temp", 7.5);
let mut vars: HashMap<&str, Variable> = HashMap::new();
vars.insert("temp", temp);
vars.insert("fan", fan);

let alpha = rule.activation(&inputs, &vars).unwrap();
assert!(alpha > 0.0 && alpha <= 1.0);

// Implication discretizes μ_B(x) clipped by alpha across the domain
let sampler = UniformSampler::default();
let implied = rule.implicate(alpha, &vars, &sampler).unwrap();
assert_eq!(implied["fan"].len(), sampler.n);

Structs§

Consequent
Output clause (THEN-part) referencing a linguistic variable and term.
Rule
Full fuzzy rule pairing an antecedent with one or more consequents.