Low-level fuzzy systems toolbox with statically-typed operation systems, values, expressions and memberships.
fuzzy_systems
is especially useful when multiple fuzzy operation
systems are used in different parts of the program or a thin layer
of zero-cost abstraction is needed for raw performance (could be used
for HPC).
use fuzzy_systems::{Hamacher1, Opset, Membership, Value, Expr};
// Take crisp Rust structure:
#[derive(PartialOrd, PartialEq)]
enum ThreatLevel {
VeryLow,
Low,
Medium,
High,
Critical
}
// Implement fuzzyfication:
impl<S: Opset> Value<S> for ThreatLevel {
fn membership(&self) -> Membership<S> {
use ThreatLevel::*;
let raw = match self {
VeryLow => 0.0,
Low => 0.2,
Medium => 0.5,
High => 0.7,
Critical => 1.0
};
Membership::new(raw)
}
}
let crisp_a = ThreatLevel::Medium;
let crisp_b = ThreatLevel::High;
// Do fuzzy operations:
let a = crisp_a.membership();
let b = crisp_b.membership();
let ar = a.as_raw();
let br = b.as_raw();
let c: Membership<Hamacher1> = (a | b);
let cr = c.as_raw();
assert!(cr > ar);
assert!(cr > br);
// ThreatLevel "magically" rises when threats are cumulated!
// Can build fuzzy expressions too:
let d = {
let a = crisp_a.to_expr().with_tag("medium");
let b = crisp_b.to_expr().with_tag("high");
a | b
};
assert_eq!(d.to_string(), "(medium | high)");
assert_eq!(d.to_value(), c);
There are a few other fuzzy systems libraries out there, particularly rsfuzzy, fuzzy_logic and fuzzy-reasoning-mamdani, each with their own respective advantages.
- all others are rule engines,
fuzzy_systems
is more low-level rsfuzzy
andfuzzy_logic
use string-identified variables,fuzzy-reasoning-mamdani
uses strongly-typed domains with string-identified states,fuzzy_systems
has no concept of a variable (although a value can be tagged with string name)- all others can fail in runtime due to non-existing
variables or states,
fuzzy_systems
does its transformations directly on Rust struct so it can't fail - all others operate directly on membership values,
fuzzy_systems
can assign membership to any Rust value (viaValue<S>
trait) rsfuzzy
andfuzzy-reasoning-mamdani
have fixed fuzzy operation system,fuzzy_logic
andfuzzy_systems
enable user to pick one as well as create a custom one
rsfuzzy
does string rule parsing,fuzzy_systems.rs
does notrsfuzzy
has fixed fuzzy operation system,fuzzy_systems
enables user to pick one of many as well as create a custom one
fuzzy_logic
defines sets and sets operations,fuzzy_systems
does notfuzzy_logic
binds operation sets to a rule,fuzzy_systems
binds it directly to expressionfuzzy_logic
doesn't provide easy-to-use fuzzy expressions creation methods,fuzzy_systems
does (via methods, binary operators andfuzzy_math!
macro)fuzzy_logic
heap-allocates (inBox
,HashMap
etc),fuzzy_systems
does notfuzzy_logic
caches results,fuzzy_systems
does notfuzzy_logic
usesf32
,fuzzy_systems
usesf64
- both are strongly-typed
fuzzy-reasoning-mamdani
uses(bool, f64)
for fuzzy values,fuzzy_systems
usesf64