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
use crate::core::*;
use crate::geometry::Matrix;
use std::{collections::HashMap, mem::replace};
pub trait Approximator<I: ?Sized> {
type Value;
fn n_outputs(&self) -> usize;
fn evaluate(&self, input: &I) -> EvaluationResult<Self::Value>;
fn update(&mut self, input: &I, update: Self::Value) -> UpdateResult<()>;
#[allow(unused_variables)]
fn adapt(&mut self, new_features: &HashMap<IndexT, IndexSet>) -> AdaptResult<usize> {
unimplemented!()
}
}
impl<I: ?Sized, T: Approximator<I>> Approximator<I> for Box<T> {
type Value = T::Value;
fn n_outputs(&self) -> usize { (**self).n_outputs() }
fn evaluate(&self, input: &I) -> EvaluationResult<Self::Value> { (**self).evaluate(input) }
fn update(&mut self, input: &I, update: Self::Value) -> UpdateResult<()> {
(**self).update(input, update)
}
fn adapt(&mut self, new_features: &HashMap<IndexT, IndexSet>) -> AdaptResult<usize> {
(**self).adapt(new_features)
}
}
fn append_matrix_rows(weights: &mut Matrix<f64>, new_rows: Vec<Vec<f64>>) {
let n_cols = weights.cols();
let n_rows = weights.rows();
let n_rows_new = new_rows.len();
let mut new_weights = unsafe { replace(weights, Matrix::uninitialized((0, 0))).into_raw_vec() };
new_weights.reserve_exact(n_rows_new);
for row in new_rows {
new_weights.extend(row);
}
*weights = Matrix::from_shape_vec((n_rows + n_rows_new, n_cols), new_weights).unwrap();
}
fn adapt_matrix(
weights: &mut Matrix<f64>,
new_features: &HashMap<IndexT, IndexSet>,
) -> AdaptResult<usize>
{
let n_nfs = new_features.len();
let n_outputs = weights.cols();
let max_index = weights.len() + n_nfs - 1;
let new_weights: Result<Vec<Vec<f64>>, _> = new_features
.into_iter()
.map(|(&i, idx)| {
if i > max_index {
Err(AdaptError::Failed)
} else {
Ok((0..n_outputs)
.map(|c| {
let c = weights.column(c);
idx.iter().fold(0.0, |acc, r| acc + c[*r])
})
.collect())
}
})
.collect();
match new_weights {
Ok(new_weights) => {
append_matrix_rows(weights, new_weights);
Ok(n_nfs)
},
Err(err) => Err(err),
}
}
import_all!(scalar);
import_all!(pair);
import_all!(triple);
import_all!(vector);