Skip to content

Commit b34ef61

Browse files
authored
Merge pull request #3 from JoshuaKento/main
fundamental logic fixes, prepared crate metadata for publication
2 parents d7c9f7a + 9a362a0 commit b34ef61

File tree

1,941 files changed

+7196
-30
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,941 files changed

+7196
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ readme = "README.md"
1111
keywords = ["fuzzy", "logic", "ai", "inference"]
1212
categories = ["algorithms", "science"]
1313
rust-version = "1.74"
14+
exclude = ["target/**"]
1415

1516
[features]
1617
default = ["f64", "ops-dyn", "inference-mamdani"]

src/aggregate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ pub fn elements_max(data: &mut Vec<Float>, src: &Vec<Float>) {
1212

1313
/// Aggregate the contributions of all rules into output membership functions.
1414
pub fn aggregation<KI, KV>(
15-
rules: Vec<Rule>,
15+
rules: &[Rule],
1616
input: &HashMap<KI, Float>,
1717
vars: &HashMap<KV, Variable>,
18-
sampler: UniformSampler,
18+
sampler: &UniformSampler,
1919
) -> Result<HashMap<String, Vec<Float>>>
2020
where
2121
KI: Eq + Hash + Borrow<str>,

src/defuzz.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ use std::{borrow::Borrow, collections::HashMap, hash::Hash};
44

55
/// Defuzzify aggregated membership samples using the centroid of area method.
66
pub fn defuzzification<KV>(
7-
myu: HashMap<String, Vec<Float>>,
7+
agg_memberships: &HashMap<String, Vec<Float>>,
88
vars: &HashMap<KV, Variable>,
99
) -> Result<HashMap<String, Float>>
1010
where
1111
KV: Eq + Hash + Borrow<str>,
1212
{
1313
let mut result_map: HashMap<String, Float> = HashMap::new();
14-
for (i, j) in myu {
14+
for (i, j) in agg_memberships {
1515
let num = j.len();
1616
let (var_min, var_max) = vars.get(&i).ok_or(FuzzyError::EmptyInput)?.domain();
1717
let step = (var_max - var_min) / (num as Float - 1.0);
1818

19-
let (mut sum_myux, mut sum_myu): (Float, Float) = (0.0, 0.0);
19+
let (mut sum_agg_memberships_x, mut sum_agg_memberships): (Float, Float) = (0.0, 0.0);
2020
let mut l: usize = 0;
2121
for k in j {
2222
let x = var_min + step * l as Float;
23-
sum_myux = sum_myux + (x * k);
24-
sum_myu = sum_myu + k;
23+
sum_agg_memberships_x = sum_agg_memberships_x + (x * k);
24+
sum_agg_memberships = sum_agg_memberships + k;
2525
l += 1;
2626
}
27-
result_map.insert(i, sum_myux / sum_myu);
27+
result_map.insert(i.to_string(), sum_agg_memberships_x / sum_agg_memberships);
2828
}
2929

3030
return Ok(result_map);

src/rulespace.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{borrow::Borrow, collections::HashMap, hash::Hash};
33
use crate::{
44
aggregate::aggregation,
55
defuzz::defuzzification,
6-
error::{self},
6+
error::{self, FuzzyError},
77
mamdani::Rule,
88
sampler::UniformSampler,
99
variable::Variable,
@@ -13,53 +13,57 @@ use crate::{
1313
// Container for fuzzy variables, rules, and intermediate membership data.
1414
pub struct RuleSpace {
1515
pub vars: HashMap<String, Variable>,
16-
pub myu: HashMap<String, Vec<Float>>,
16+
pub agg_memberships: HashMap<String, Vec<Float>>,
1717
pub rules: Vec<Rule>,
1818
}
1919

2020
impl RuleSpace {
2121
/// Create a rule space with the supplied variables and rules.
22-
pub fn new(self, vars: HashMap<String, Variable>, rules: Vec<Rule>) -> Self {
23-
return Self {
24-
vars: vars,
25-
myu: HashMap::new(),
26-
rules: rules,
27-
};
22+
pub fn new(vars: HashMap<String, Variable>, rules: Vec<Rule>) -> error::Result<Self> {
23+
if vars.is_empty() || rules.is_empty() {
24+
return Err(FuzzyError::EmptyInput);
25+
} else {
26+
return Ok(Self {
27+
vars: vars,
28+
agg_memberships: HashMap::new(),
29+
rules: rules,
30+
});
31+
}
2832
}
2933

3034
/// Append additional rules to the existing rule set.
31-
pub fn add_rules(mut self, rules: &mut Vec<Rule>) {
35+
pub fn add_rules(&mut self, rules: &mut Vec<Rule>) {
3236
self.rules.append(rules);
3337
}
3438

3539
/// Run the aggregation step for all rules with the provided crisp inputs.
3640
pub fn aggregate<KI>(
3741
&mut self,
3842
input: &HashMap<KI, Float>,
39-
sampler: UniformSampler,
43+
sampler: &UniformSampler,
4044
) -> error::Result<()>
4145
where
4246
KI: Eq + Hash + Borrow<str>,
4347
{
44-
let rules = std::mem::take(&mut self.rules);
45-
let myu = aggregation(rules, input, &self.vars, sampler)?;
46-
self.myu = myu;
48+
//let rules = std::mem::take(&mut self.rules);
49+
let agg_memberships = aggregation(&self.rules, input, &self.vars, sampler)?;
50+
self.agg_memberships = agg_memberships;
4751

4852
Ok(())
4953
}
5054

5155
/// Aggregate and then defuzzify each output variable using the supplied sampler.
52-
pub fn defuzzificate<KI>(
56+
pub fn defuzzify<KI>(
5357
&mut self,
5458
input: &HashMap<KI, Float>,
55-
sampler: UniformSampler,
56-
) -> crate::error::Result<HashMap<String, Float>>
59+
sampler: &UniformSampler,
60+
) -> error::Result<HashMap<String, Float>>
5761
where
5862
KI: Eq + Hash + Borrow<str>,
5963
{
60-
let _ = self.aggregate(input, sampler);
61-
let myu = std::mem::take(&mut self.myu);
62-
Ok(defuzzification(myu, &self.vars)?)
64+
let _ = self.aggregate(input, sampler)?;
65+
//let agg_memberships = std::mem::take(&mut self.agg_memberships);
66+
Ok(defuzzification(&self.agg_memberships, &self.vars)?)
6367
}
6468
//is there a nessecity?
6569
//pub fn consequent_keys() {}

target/.rustc_info.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc_fingerprint":4715693574508695924,"outputs":{"7971740275564407648":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Joshua\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""},"17747080675513052775":{"success":true,"status":"","code":0,"stdout":"rustc 1.89.0 (29483883e 2025-08-04)\nbinary: rustc\ncommit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2\ncommit-date: 2025-08-04\nhost: x86_64-pc-windows-msvc\nrelease: 1.89.0\nLLVM version: 20.1.7\n","stderr":""}},"successes":{}}

target/.rustdoc_fingerprint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc_vv":"rustc 1.89.0 (29483883e 2025-08-04)\nbinary: rustc\ncommit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2\ncommit-date: 2025-08-04\nhost: x86_64-pc-windows-msvc\nrelease: 1.89.0\nLLVM version: 20.1.7\n"}

target/CACHEDIR.TAG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Signature: 8a477f597d28d172789f06886806bc55
2+
# This file is a cache directory tag created by cargo.
3+
# For information about cache directory tags see https://bford.info/cachedir/

target/debug/.cargo-lock

Whitespace-only changes.
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.

0 commit comments

Comments
 (0)