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
pub mod table;
pub mod chooser;
pub mod approx;
pub mod feature;
pub mod graddesc;
mod metric;
use std::fmt::Debug;
use num::Num;
use num::Float;
use environment::Space;
pub trait ParameterizedFunc<T: Num> {
fn num_params(&self) -> usize;
fn get_params(&self) -> Vec<T>;
fn set_params(&mut self, params: Vec<T>);
}
pub trait DifferentiableFunc<S: Space, A: Space, T: Num> : ParameterizedFunc<T> {
fn get_grad(&self, state: &S::Element, action: &A::Element) -> Vec<T>;
fn calculate(&self, state: &S::Element, action: &A::Element) -> T;
}
pub trait LogDiffFunc<S: Space, A: Space, T: Num> : ParameterizedFunc<T> {
fn log_grad(&self, state: &S::Element, action: &A::Element) -> Vec<T>;
}
pub trait GradientDescAlgo<F: Float> {
fn calculate(&mut self, grad: Vec<F>, lr: F) -> Vec<F>;
}
pub trait FeatureExtractor<S: Space, A: Space, F: Float> {
fn num_features(&self) -> usize;
fn extract(&self, state: &S::Element, action: &A::Element) -> Vec<F>;
}
pub trait QFunction<S: Space, A: Space> : Debug {
fn eval(&self, state: &S::Element, action: &A::Element) -> f64;
fn update(&mut self, state: &S::Element, action: &A::Element, new_val: f64, alpha: f64);
}
pub trait VFunction<S: Space> : Debug {
fn eval(&self, state: &S::Element) -> f64;
fn update(&mut self, state: &S::Element, new_val: f64, alpha: f64);
}
pub trait Chooser<T> : Debug {
fn choose(&self, choices: &Vec<T>, weights: Vec<f64>) -> T;
}
pub trait Feature<S: Space, F: Float> : Debug {
fn extract(&self, state: &S::Element) -> F;
fn box_clone(&self) -> Box<Feature<S, F>>;
}
impl<F: Float, S: Space> Clone for Box<Feature<S, F>> {
fn clone(&self) -> Self {
self.box_clone()
}
}
pub trait Metric {
fn dist(x: &Self, y: &Self) -> f64 {
Metric::dist2(x, y).sqrt()
}
fn dist2(x: &Self, y: &Self) -> f64;
}
#[derive(Debug, Clone)]
pub enum TimePeriod {
EPISODES(usize),
TIMESTEPS(usize),
OR(Box<TimePeriod>, Box<TimePeriod>),
}
impl TimePeriod {
pub fn is_none(&self) -> bool {
match *self {
TimePeriod::EPISODES(x) => x == 0,
TimePeriod::TIMESTEPS(x) => x == 0,
TimePeriod::OR(ref a, ref b) => a.is_none() || b.is_none(),
}
}
pub fn dec(&self, done: bool) -> TimePeriod {
if self.is_none() {
self.clone()
} else {
match *self {
TimePeriod::EPISODES(x) => TimePeriod::EPISODES(if done {x-1} else {x}),
TimePeriod::TIMESTEPS(x) => TimePeriod::TIMESTEPS(x-1),
TimePeriod::OR(ref a, ref b) => TimePeriod::OR(Box::new(a.dec(done)), Box::new(b.dec(done))),
}
}
}
}