Skip to content

Commit d05055a

Browse files
committed
Add documentation comments to Mamdani inference components
1 parent a748ac3 commit d05055a

File tree

5 files changed

+110
-4
lines changed

5 files changed

+110
-4
lines changed

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# rust-fuzzylogic
2+
3+
A work-in-progress Rust crate that provides building blocks for authoring fuzzy inference systems.
4+
The project aims to make it easy to describe linguistic variables, membership functions, and rule
5+
bases while keeping room for experimentation with multiple aggregation operators and inference
6+
strategies.
7+
8+
## Project status
9+
10+
The repository currently focuses on laying down the module structure of the crate. Several source
11+
files exist as stubs and will be fleshed out in future iterations. Even though the public API is not
12+
ready for consumption yet, the layout already highlights the intended responsibilities of each
13+
module:
14+
15+
- `membership`: utilities for defining membership functions (triangular, trapezoidal, gaussian, …).
16+
- `variable`: strongly-typed linguistic variables composed of terms and membership functions.
17+
- `term`: basic linguistic terms that bind membership functions to human-readable labels.
18+
- `rulespace`: abstractions for authoring rule bases and connecting antecedents to consequents.
19+
- `antecedent`: helpers to compose fuzzy predicates out of linguistic terms.
20+
- `aggregate`: algorithms to combine the contribution of multiple rules.
21+
- `defuzz`: defuzzification routines that convert fuzzy outputs into crisp values.
22+
- `mamdani`: reference implementation of a Mamdani-style inference engine.
23+
- `ops`: configurable T-norm/T-conorm operators (minimum, product, Łukasiewicz, …).
24+
- `sampler`: sampling utilities for visualisation or numerical integration tasks.
25+
- `builder`: high-level ergonomics for constructing complete systems.
26+
- `error`: error types returned by the crate.
27+
- `prelude`: convenient re-exports for end users.
28+
29+
## Getting started
30+
31+
Because the implementation is still under construction the crate is not published on crates.io.
32+
Once the API stabilises you will be able to add it as a Git dependency in your `Cargo.toml`:
33+
34+
```toml
35+
[dependencies]
36+
rust-fuzzylogic = { git = "https://github.com/your-org/rust-fuzzylogic" }
37+
```
38+
39+
After the crate reaches its first release the dependency line will become:
40+
41+
```toml
42+
[dependencies]
43+
rust-fuzzylogic = "0.1"
44+
```
45+
46+
## Development
47+
48+
If you would like to contribute, clone the repository and open it with your favourite editor:
49+
50+
```bash
51+
git clone https://github.com/your-org/rust-fuzzylogic.git
52+
cd rust-fuzzylogic
53+
```
54+
55+
The crate targets Rust 1.76+ (edition 2024). Standard Cargo commands apply:
56+
57+
- `cargo check` — make sure the crate compiles.
58+
- `cargo fmt` — format the codebase.
59+
- `cargo test` — run the test suite (currently empty).
60+
61+
### Feature flags
62+
63+
The crate is organised around a set of feature flags to enable or disable components at compile
64+
time:
65+
66+
- `f32` / `f64` — choose the floating-point precision used throughout the inference engine.
67+
- `serde` — derive serialisation support for configuration data structures.
68+
- `parallel` — enable rayon-powered parallel execution for suitable workloads.
69+
- `ops-minmax`, `ops-product`, `ops-lukasiewicz` — opt into specific operator families.
70+
- `ops-dyn` — use dynamic dispatch for selecting operators at runtime.
71+
- `inference-mamdani` — compile the Mamdani inference engine implementation.
72+
73+
## Examples
74+
75+
Example binaries will live under the `examples/` directory. They are not yet implemented, but the
76+
plan is to showcase tasks such as temperature control, batch processing, fuzzy C-means clustering,
77+
and gradient-descent tuning of membership functions. You will be able to run them with Cargo once
78+
they are published:
79+
80+
```bash
81+
cargo run --example temperature
82+
```
83+
84+
## Roadmap
85+
86+
- [ ] Flesh out membership functions and sampling utilities.
87+
- [ ] Implement the Mamdani inference pipeline end-to-end.
88+
- [ ] Document the builder APIs and provide a quick start guide.
89+
- [ ] Publish the crate and examples for real-world testing.
90+
91+
---
92+
93+
This README will evolve alongside the code. Contributions, bug reports, and ideas are warmly
94+
welcome!

src/aggregate.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
//Agregation acros sets of rules
1+
// Aggregation utilities for combining rule outputs across consequents.
22

33
use crate::{mamdani::Rule, prelude::*, variable::Variable};
44
use std::{borrow::Borrow, collections::HashMap, hash::Hash};
55

6+
/// Combine two membership sample vectors by taking the pointwise maximum.
67
pub fn elements_max(data: &mut Vec<Float>, src: &Vec<Float>) {
78
for (d, s) in data.iter_mut().zip(src) {
89
*d = d.max(*s)
910
}
1011
}
1112

13+
/// Aggregate the contributions of all rules into output membership functions.
1214
pub fn aggregation<KI, KV>(
1315
rules: Vec<Rule>,
1416
input: &HashMap<KI, Float>,

src/defuzz.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
//Possible Defuzz methods => Bisecctor of Area, Centroid, ...
1+
// Defuzzification utilities for collapsing aggregated membership values.
22
use crate::{prelude::*, variable::Variable};
33
use std::{borrow::Borrow, collections::HashMap, hash::Hash};
44

5+
/// Defuzzify aggregated membership samples using the centroid of area method.
56
pub fn defuzzification<KV>(
67
myu: HashMap<String, Vec<Float>>,
78
vars: &HashMap<KV, Variable>,

src/mamdani.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//mamdani style Implication and Consequent/Rule Behaviour
1+
// Mamdani-style implication and rule behavior primitives.
22

33
use std::{borrow::Borrow, collections::HashMap, hash::Hash};
44

@@ -11,18 +11,21 @@ use crate::{
1111
variable::Variable,
1212
};
1313

14+
/// Implication modes supported by a Mamdani rule.
1415
pub enum Implication {
1516
Clip,
1617
Product,
1718
}
1819

20+
/// Output clause of a fuzzy rule referencing a linguistic variable and term.
1921
pub struct Consequent {
2022
pub var: String,
2123
pub term: String,
2224
//pub weight: Float,
2325
//pub imp: Implication,
2426
}
2527

28+
/// Full fuzzy rule pairing an antecedent with one or more consequents.
2629
pub struct Rule {
2730
pub antecedent: Antecedent,
2831
pub consequent: Vec<Consequent>,
@@ -31,6 +34,7 @@ pub struct Rule {
3134
//Mamdani Inference Engine
3235
#[cfg(feature = "inference-mamdani")]
3336
impl Rule {
37+
/// Evaluate the antecedent against crisp input values to obtain activation.
3438
pub fn activation<KI, KV>(
3539
&self,
3640
input: &HashMap<KI, Float>,
@@ -43,6 +47,7 @@ impl Rule {
4347
eval_antecedent(&self.antecedent, input, vars)
4448
}
4549

50+
/// Apply the selected implication operator to produce discretized membership outputs.
4651
pub fn implicate<KV>(
4752
&self,
4853
alpha: Float,

src/rulespace.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ use crate::{
1010
variable::Variable,
1111
};
1212

13-
//#[derive()]
13+
// Container for fuzzy variables, rules, and intermediate membership data.
1414
pub struct RuleSpace {
1515
pub vars: HashMap<String, Variable>,
1616
pub myu: HashMap<String, Vec<Float>>,
1717
pub rules: Vec<Rule>,
1818
}
1919

2020
impl RuleSpace {
21+
/// Create a rule space with the supplied variables and rules.
2122
pub fn new(self, vars: HashMap<String, Variable>, rules: Vec<Rule>) -> Self {
2223
return Self {
2324
vars: vars,
@@ -26,10 +27,12 @@ impl RuleSpace {
2627
};
2728
}
2829

30+
/// Append additional rules to the existing rule set.
2931
pub fn add_rules(mut self, rules: &mut Vec<Rule>) {
3032
self.rules.append(rules);
3133
}
3234

35+
/// Run the aggregation step for all rules with the provided crisp inputs.
3336
pub fn aggregate<KI>(
3437
&mut self,
3538
input: &HashMap<KI, Float>,
@@ -45,6 +48,7 @@ impl RuleSpace {
4548
Ok(())
4649
}
4750

51+
/// Aggregate and then defuzzify each output variable using the supplied sampler.
4852
pub fn defuzzificate<KI>(
4953
&mut self,
5054
input: &HashMap<KI, Float>,

0 commit comments

Comments
 (0)