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
use error::*;
use geometry::Matrix;
use projectors::{IndexSet, IndexT};
use std::collections::HashMap;

/// An interface for function approximators.
pub trait Approximator<I: ?Sized> {
    type Value;

    /// Evaluate the function and return its value.
    fn evaluate(&self, input: &I) -> EvaluationResult<Self::Value>;

    /// Update the approximator's estimate for the given input.
    fn update(&mut self, input: &I, update: Self::Value) -> UpdateResult<()>;

    #[allow(unused_variables)]
    /// Adapt the approximator in light of newly discovered features.
    fn adapt(&mut self, new_features: &HashMap<IndexT, IndexSet>) -> AdaptResult<usize> {
        Err(AdaptError::NotImplemented)
    }
}

impl<I: ?Sized, T: Approximator<I>> Approximator<I> for Box<T> {
    type Value = T::Value;

    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)
    }
}

/// An interface for approximators parameterised by a set of weights.
pub trait Parameterised {
    /// Return a copy of the approximator weights.
    fn weights(&self) -> Matrix<f64>;
}

impl<T: Parameterised> Parameterised for Box<T> {
    fn weights(&self) -> Matrix<f64> {
        (**self).weights()
    }
}