-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.rs
More file actions
352 lines (320 loc) · 9.28 KB
/
Copy pathnode.rs
File metadata and controls
352 lines (320 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! Node: the cognitive atom. Three Planes (S/P/O), separately addressable via Mask.
use super::fingerprint::Fingerprint;
use super::plane::{Distance, Plane, Truth};
/// Attention mask over S/P/O planes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Mask {
pub s: bool,
pub p: bool,
pub o: bool,
}
pub const SPO: Mask = Mask {
s: true,
p: true,
o: true,
};
pub const SP_: Mask = Mask {
s: true,
p: true,
o: false,
};
pub const S_O: Mask = Mask {
s: true,
p: false,
o: true,
};
pub const _PO: Mask = Mask {
s: false,
p: true,
o: true,
};
pub const S__: Mask = Mask {
s: true,
p: false,
o: false,
};
pub const _P_: Mask = Mask {
s: false,
p: true,
o: false,
};
pub const __O: Mask = Mask {
s: false,
p: false,
o: true,
};
pub const ___: Mask = Mask {
s: false,
p: false,
o: false,
};
impl Mask {
#[inline]
pub fn count(&self) -> u32 {
self.s as u32 + self.p as u32 + self.o as u32
}
}
/// The cognitive atom. Three planes, separately addressable.
pub struct Node {
pub s: Plane,
pub p: Plane,
pub o: Plane,
}
impl Clone for Node {
fn clone(&self) -> Self {
Self {
s: self.s.clone(),
p: self.p.clone(),
o: self.o.clone(),
}
}
}
/// Simple SplitMix64 RNG for deterministic random node generation.
struct SplitMix64(u64);
impl SplitMix64 {
fn new(seed: u64) -> Self {
Self(seed)
}
fn next_u64(&mut self) -> u64 {
self.0 = self.0.wrapping_add(0x9E3779B97F4A7C15);
let mut z = self.0;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB);
z ^ (z >> 31)
}
}
impl Node {
pub fn new() -> Self {
Self {
s: Plane::new(),
p: Plane::new(),
o: Plane::new(),
}
}
/// Random node for testing. Seed → deterministic.
pub fn random(seed: u64) -> Self {
let mut rng = SplitMix64::new(seed);
let mut node = Self::new();
for plane in [&mut node.s, &mut node.p, &mut node.o] {
let mut words = [0u64; 256];
for w in words.iter_mut() {
*w = rng.next_u64();
}
let fp = Fingerprint::<256>::from_words(words);
plane.encounter_bits(&fp);
plane.encounter_bits(&fp);
plane.encounter_bits(&fp);
}
node
}
pub fn distance(&mut self, other: &mut Node, mask: Mask) -> Distance {
let mut total_disagreement = 0u32;
let mut total_overlap = 0u32;
let mut total_penalty = 0u32;
let mut any_measured = false;
macro_rules! add_plane {
($self_plane:expr, $other_plane:expr, $active:expr) => {
if $active {
match $self_plane.distance(&mut $other_plane) {
Distance::Measured {
disagreement,
overlap,
penalty,
} => {
total_disagreement += disagreement;
total_overlap += overlap;
total_penalty += penalty;
any_measured = true;
}
Distance::Incomparable => {}
}
}
};
}
add_plane!(self.s, other.s, mask.s);
add_plane!(self.p, other.p, mask.p);
add_plane!(self.o, other.o, mask.o);
if !any_measured || total_overlap == 0 {
Distance::Incomparable
} else {
Distance::Measured {
disagreement: total_disagreement,
overlap: total_overlap,
penalty: total_penalty,
}
}
}
/// Project all 7 non-empty mask combinations and return distances.
///
/// Returns distances in order: `[S, P, O, SP, SO, PO, SPO]`.
///
/// # Example
///
/// ```
/// use ndarray::hpc::node::Node;
///
/// let mut a = Node::random(42);
/// let mut b = Node::random(43);
/// let projections = a.project_all(&mut b);
/// assert_eq!(projections.len(), 7);
/// ```
pub fn project_all(&mut self, other: &mut Node) -> [Distance; 7] {
[
self.distance(other, S__),
self.distance(other, _P_),
self.distance(other, __O),
self.distance(other, SP_),
self.distance(other, S_O),
self.distance(other, _PO),
self.distance(other, SPO),
]
}
pub fn truth(&mut self, mask: Mask) -> Truth {
let mut total_freq = 0u64;
let mut total_conf = 0u64;
let mut total_evidence = 0u32;
let mut count = 0u32;
macro_rules! add_truth {
($plane:expr, $active:expr) => {
if $active {
let t = $plane.truth();
total_freq += t.frequency as u64;
total_conf += t.confidence as u64;
total_evidence += t.evidence;
count += 1;
}
};
}
add_truth!(self.s, mask.s);
add_truth!(self.p, mask.p);
add_truth!(self.o, mask.o);
if count == 0 {
return Truth {
frequency: 32768,
confidence: 0,
evidence: 0,
};
}
Truth {
frequency: (total_freq / count as u64) as u16,
confidence: (total_conf / count as u64) as u16,
evidence: total_evidence,
}
}
}
impl Default for Node {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn node_new_is_empty() {
let n = Node::new();
assert_eq!(n.s.encounters(), 0);
assert_eq!(n.p.encounters(), 0);
assert_eq!(n.o.encounters(), 0);
}
#[test]
fn mask_count() {
assert_eq!(SPO.count(), 3);
assert_eq!(SP_.count(), 2);
assert_eq!(S__.count(), 1);
assert_eq!(___.count(), 0);
}
#[test]
fn mask_skips_planes() {
let mut a = Node::random(42);
let mut b = Node::random(43);
let d_spo = a.distance(&mut b, SPO);
let d_s = a.distance(&mut b, S__);
match (d_spo, d_s) {
(Distance::Measured { overlap: o_spo, .. }, Distance::Measured { overlap: o_s, .. }) => {
assert!(o_spo >= o_s);
}
_ => panic!("expected Measured for random nodes"),
}
}
#[test]
fn empty_mask_is_incomparable() {
let mut a = Node::random(42);
let mut b = Node::random(43);
let d = a.distance(&mut b, ___);
assert!(matches!(d, Distance::Incomparable));
}
#[test]
fn node_truth_empty_mask() {
let mut n = Node::new();
let t = n.truth(___);
assert_eq!(t.frequency, 32768);
assert_eq!(t.confidence, 0);
assert_eq!(t.evidence, 0);
}
#[test]
fn project_all_returns_seven_distances() {
let mut a = Node::random(42);
let mut b = Node::random(43);
let projections = a.project_all(&mut b);
assert_eq!(projections.len(), 7);
// All should be Measured for random nodes with encounters
for (i, d) in projections.iter().enumerate() {
match d {
Distance::Measured { overlap, .. } => {
assert!(*overlap > 0, "projection {} should have overlap", i);
}
Distance::Incomparable => panic!("projection {} should be Measured", i),
}
}
}
#[test]
fn project_all_spo_matches_direct() {
let mut a = Node::random(42);
let mut b = Node::random(43);
let projections = a.project_all(&mut b);
let direct_spo = a.distance(&mut b, SPO);
// SPO is the last element (index 6)
match (projections[6], direct_spo) {
(
Distance::Measured {
disagreement: d1,
overlap: o1,
penalty: p1,
},
Distance::Measured {
disagreement: d2,
overlap: o2,
penalty: p2,
},
) => {
assert_eq!(d1, d2);
assert_eq!(o1, o2);
assert_eq!(p1, p2);
}
_ => panic!("expected both Measured"),
}
}
#[test]
fn project_all_single_planes_match_direct() {
let mut a = Node::random(100);
let mut b = Node::random(200);
let projections = a.project_all(&mut b);
// S__ is index 0
let d_s = a.distance(&mut b, S__);
match (projections[0], d_s) {
(Distance::Measured { disagreement: d1, .. }, Distance::Measured { disagreement: d2, .. }) => {
assert_eq!(d1, d2)
}
_ => panic!("expected Measured"),
}
}
#[test]
fn node_random_deterministic() {
let a = Node::random(99);
let b = Node::random(99);
assert_eq!(a.s.acc(), b.s.acc());
assert_eq!(a.p.acc(), b.p.acc());
assert_eq!(a.o.acc(), b.o.acc());
}
}