Module aggregate

Module aggregate 

Source
Expand description

Aggregation: combine rule outputs into per-variable membership samples.

This module implements the aggregation stage for Mamdani-style fuzzy inference systems. Given a set of rules and crisp inputs, each rule is evaluated to produce its implied output membership functions on a discretized grid. Aggregation then combines those contributions across all rules by taking the pointwise supremum (max) for each output variable.

Key characteristics:

  • Sampling: output membership functions are discretized using a UniformSampler (evenly spaced samples across the variable domain).
  • Implication: individual rules produce per-variable sample vectors via Rule::implicate using the chosen operator family.
  • Aggregation: contributions for the same output variable are merged with a pointwise max (elements_max).

Errors from aggregation originate from rule activation, implication, variable lookup, or input lookup and propagate as FuzzyError.

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};
use rust_fuzzylogic::aggregate::aggregation;

// Build a minimal system
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();

let mut vars: HashMap<&str, Variable> = HashMap::new();
vars.insert("temp", temp);
vars.insert("fan", fan);

// 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() }],
};
let rules = vec![rule];

let mut input: HashMap<&str, Float> = HashMap::new();
input.insert("temp", 7.5);
let sampler = UniformSampler::default();

// Aggregate implied outputs for each consequent variable
let agg = aggregation(&rules, &input, &vars, &sampler).unwrap();
assert!(agg.contains_key("fan"));
assert_eq!(agg["fan"].len(), sampler.n);

Functionsยง

aggregation
Aggregate the contributions of all rules into output membership functions.
elements_max
Combine two membership sample vectors by taking the pointwise maximum.