[lab2] Algo implementation
This commit is contained in:
parent
dc98c25c41
commit
cda30bd374
@ -1,7 +1,7 @@
|
|||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
use std::ops::{BitAnd, BitAndAssign, Index, IndexMut, Not, Shl};
|
use std::ops::{BitAnd, BitAndAssign, Index, IndexMut, Not, Shl};
|
||||||
|
|
||||||
pub(super) struct BitVector {
|
pub struct BitVector {
|
||||||
len: usize,
|
len: usize,
|
||||||
data: Box<[usize]>,
|
data: Box<[usize]>,
|
||||||
}
|
}
|
||||||
@ -27,7 +27,12 @@ impl BitVector {
|
|||||||
|
|
||||||
return counter;
|
return counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
return self.len;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Clone for BitVector {
|
impl Clone for BitVector {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
return Self {
|
return Self {
|
||||||
@ -37,10 +42,10 @@ impl Clone for BitVector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BitAnd<&Self> for BitVector {
|
impl BitAnd<Self> for &BitVector {
|
||||||
type Output = Self;
|
type Output = BitVector;
|
||||||
|
|
||||||
fn bitand(self, rhs: &Self) -> Self::Output {
|
fn bitand(self, rhs: Self) -> Self::Output {
|
||||||
let mut intersected = self.clone();
|
let mut intersected = self.clone();
|
||||||
intersected &= rhs;
|
intersected &= rhs;
|
||||||
return intersected;
|
return intersected;
|
||||||
|
|||||||
35
lab2/src/algo/impl.rs
Normal file
35
lab2/src/algo/impl.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use super::BitVector;
|
||||||
|
use super::operations::attentivenessTest;
|
||||||
|
use super::operations::similarityCheck;
|
||||||
|
|
||||||
|
pub fn adaptiveResonanceTheoryImpl(
|
||||||
|
data: &[BitVector],
|
||||||
|
beta: usize,
|
||||||
|
attentiveness: f64,
|
||||||
|
) -> Box<[usize]> {
|
||||||
|
let mut prototypes = Vec::<BitVector>::new();
|
||||||
|
let mut groups = vec![0usize, data.len()].into_boxed_slice();
|
||||||
|
|
||||||
|
match data.get(0) {
|
||||||
|
None => return groups,
|
||||||
|
Some(p) => prototypes.push(p.clone()),
|
||||||
|
}
|
||||||
|
prototypes.push(data[0].clone());
|
||||||
|
|
||||||
|
'elements: for i in 1..data.len() {
|
||||||
|
for j in 0..prototypes.len() {
|
||||||
|
if !similarityCheck(&prototypes[j], &data[i], beta) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if !attentivenessTest(&prototypes[j], &data[i], attentiveness) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
groups[i] = j;
|
||||||
|
continue 'elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
prototypes.push(data[i].clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
return groups;
|
||||||
|
}
|
||||||
@ -1 +1,5 @@
|
|||||||
mod bit_vector;
|
mod bit_vector;
|
||||||
|
mod operations;
|
||||||
|
mod r#impl;
|
||||||
|
|
||||||
|
pub use bit_vector::BitVector;
|
||||||
10
lab2/src/algo/operations.rs
Normal file
10
lab2/src/algo/operations.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
use super::BitVector;
|
||||||
|
use eframe::emath::Numeric;
|
||||||
|
|
||||||
|
pub(super) fn similarityCheck(prototype: &BitVector, vector: &BitVector, beta: usize) -> bool {
|
||||||
|
return ((prototype & vector).count1() / (beta + prototype.count1()))
|
||||||
|
> (vector.count1() / (beta + vector.len()));
|
||||||
|
}
|
||||||
|
pub(super) fn attentivenessTest(prototype: &BitVector, vector: &BitVector, p: f64) -> bool {
|
||||||
|
return ((prototype & vector).count1() / (vector.count1())).to_f64() < p;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user