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
use crate::approximators::{Approximator, adapt_matrix};
use crate::basis::Projection;
use crate::core::*;
use crate::geometry::{norms::l1, Matrix};
use std::collections::HashMap;

/// Weight-`Projection` evaluator with pair `(f64, f64)` output.
#[derive(Clone, Serialize, Deserialize)]
pub struct PairFunction {
    pub weights: Matrix<f64>,
}

impl PairFunction {
    pub fn new(n_features: usize) -> Self {
        PairFunction {
            weights: Matrix::zeros((n_features, 2)),
        }
    }
}

impl Approximator<Projection> for PairFunction {
    type Value = (f64, f64);

    fn n_outputs(&self) -> usize { 2 }

    fn evaluate(&self, p: &Projection) -> EvaluationResult<(f64, f64)> {
        Ok(match p {
            &Projection::Dense(ref activations) => (
                self.weights.column(0).dot(activations),
                self.weights.column(1).dot(activations),
            ),
            &Projection::Sparse(ref indices) => indices.iter().fold((0.0, 0.0), |acc, idx| {
                (
                    acc.0 + self.weights[(*idx, 0)],
                    acc.1 + self.weights[(*idx, 1)],
                )
            }),
        })
    }

    fn update(&mut self, p: &Projection, errors: (f64, f64)) -> UpdateResult<()> {
        Ok(match p {
            &Projection::Dense(ref activations) => {
                let z = l1(activations.as_slice().unwrap());

                let phi_matrix = activations
                    .view()
                    .into_shape((activations.len(), 1))
                    .unwrap();
                let error_matrix =
                    Matrix::from_shape_vec((1, 2), vec![errors.0 / z, errors.1 / z]).unwrap();

                self.weights += &phi_matrix.dot(&error_matrix)
            },
            &Projection::Sparse(ref indices) => {
                let z = indices.len() as f64;
                let scaled_errors = (errors.0 / z, errors.1 / z);

                indices.iter().for_each(|idx| {
                    self.weights[(*idx, 0)] += scaled_errors.0;
                    self.weights[(*idx, 1)] += scaled_errors.1;
                });
            },
        })
    }

    fn adapt(&mut self, new_features: &HashMap<IndexT, IndexSet>) -> AdaptResult<usize> {
        adapt_matrix(&mut self.weights, new_features)
    }
}

impl Parameterised for PairFunction {
    fn weights(&self) -> Matrix<f64> { self.weights.clone() }
}

#[cfg(test)]
mod tests {
    extern crate seahash;

    use crate::approximators::{Approximator, PairFunction};
    use crate::basis::fixed::{Fourier, TileCoding};
    use crate::LFA;
    use std::{
        collections::{BTreeSet, HashMap},
        hash::BuildHasherDefault,
    };

    type SHBuilder = BuildHasherDefault<seahash::SeaHasher>;

    #[test]
    fn test_sparse_update_eval() {
        let p = TileCoding::new(SHBuilder::default(), 4, 100);
        let mut f = LFA::pair_output(p);
        let input = vec![5.0];

        let _ = f.update(input.as_slice(), (20.0, 50.0));
        let out = f.evaluate(input.as_slice()).unwrap();

        assert!((out.0 - 20.0).abs() < 1e-6);
        assert!((out.1 - 50.0).abs() < 1e-6);
    }

    #[test]
    fn test_dense_update_eval() {
        let p = Fourier::new(3, vec![(0.0, 10.0)]);
        let mut f = LFA::pair_output(p);

        let input = vec![5.0];

        let _ = f.update(input.as_slice(), (20.0, 50.0));
        let out = f.evaluate(input.as_slice()).unwrap();

        assert!((out.0 - 20.0).abs() < 1e-6);
        assert!((out.1 - 50.0).abs() < 1e-6);
    }

    #[test]
    fn test_adapt() {
        let mut f = PairFunction::new(100);

        let mut new_features = HashMap::new();
        new_features.insert(100, {
            let mut idx = BTreeSet::new();

            idx.insert(10);
            idx.insert(90);

            idx
        });

        match f.adapt(&new_features) {
            Ok(n) => {
                assert_eq!(n, 1);
                assert_eq!(f.weights.rows(), 101);

                let c0 = f.weights.column(0);
                let c1 = f.weights.column(1);

                assert_eq!(c0[100], c0[10] / 2.0 + c0[90] / 2.0);
                assert_eq!(c1[100], c1[10] / 2.0 + c1[90] / 2.0);
            },
            Err(err) => panic!("PairFunction::adapt failed with AdaptError::{:?}", err),
        }
    }
}