#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RcfConfig {
pub input_dim: usize,
#[cfg_attr(feature = "serde", serde(default = "default_shingle_size"))]
pub shingle_size: usize,
#[cfg_attr(feature = "serde", serde(default = "default_capacity"))]
pub capacity: usize,
#[cfg_attr(feature = "serde", serde(default = "default_num_trees"))]
pub num_trees: usize,
#[cfg_attr(feature = "serde", serde(default))]
pub time_decay: f64,
#[cfg_attr(feature = "serde", serde(default))]
pub output_after: usize,
#[cfg_attr(feature = "serde", serde(default = "default_internal_shingling"))]
pub internal_shingling: bool,
#[cfg_attr(feature = "serde", serde(default = "default_initial_accept_fraction"))]
pub initial_accept_fraction: f64,
}
fn default_shingle_size() -> usize {
1
}
fn default_capacity() -> usize {
256
}
fn default_num_trees() -> usize {
50
}
fn default_internal_shingling() -> bool {
true
}
fn default_initial_accept_fraction() -> f64 {
0.125
}
impl RcfConfig {
pub fn new(input_dim: usize) -> Self {
Self {
input_dim,
shingle_size: default_shingle_size(),
capacity: default_capacity(),
num_trees: default_num_trees(),
time_decay: 0.0,
output_after: 0,
internal_shingling: default_internal_shingling(),
initial_accept_fraction: default_initial_accept_fraction(),
}
}
pub fn with_shingle_size(mut self, v: usize) -> Self {
self.shingle_size = v;
self
}
pub fn with_capacity(mut self, v: usize) -> Self {
self.capacity = v;
self
}
pub fn with_num_trees(mut self, v: usize) -> Self {
self.num_trees = v;
self
}
pub fn with_time_decay(mut self, v: f64) -> Self {
self.time_decay = v;
self
}
pub fn with_output_after(mut self, v: usize) -> Self {
self.output_after = v;
self
}
pub fn with_internal_shingling(mut self, v: bool) -> Self {
self.internal_shingling = v;
self
}
pub fn with_initial_accept_fraction(mut self, v: f64) -> Self {
self.initial_accept_fraction = v;
self
}
pub fn effective_time_decay(&self) -> f64 {
if self.time_decay == 0.0 {
0.1 / self.capacity as f64
} else {
self.time_decay
}
}
pub fn effective_output_after(&self) -> usize {
if self.output_after == 0 {
1 + self.capacity / 4
} else {
self.output_after
}
}
pub fn dim(&self) -> usize {
self.input_dim * self.shingle_size
}
}