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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#![allow(clippy::ptr_arg)]
#![allow(clippy::type_complexity)]
#![cfg_attr(all(feature = "mesalock_sgx",
not(target_env = "sgx")), no_std)]
#![cfg_attr(all(target_env = "sgx", target_vendor = "mesalock"), feature(rustc_private))]
#[cfg(all(feature = "mesalock_sgx", not(target_env = "sgx")))]
#[macro_use]
extern crate sgx_tstd as std;
pub mod binary_tree;
pub mod config;
pub mod decision_tree;
pub mod fitness;
pub mod gradient_boost;
#[cfg(feature = "input")]
#[macro_use]
extern crate cfg_if;
#[cfg(feature = "input")]
pub mod input;
#[cfg(test)]
mod tests {
#[test]
fn walk_tree() {
use crate::binary_tree::*;
let mut tree: BinaryTree<f32> = BinaryTree::new();
let root = BinaryTreeNode::new(10.0);
let root_index = tree.add_root(root);
let n1 = BinaryTreeNode::new(5.0);
let n2 = BinaryTreeNode::new(6.0);
let n1_index = tree.add_left_node(root_index, n1);
let n2_index = tree.add_right_node(root_index, n2);
let n3 = BinaryTreeNode::new(7.0);
let n4 = BinaryTreeNode::new(8.0);
tree.add_left_node(n2_index, n3);
tree.add_right_node(n2_index, n4);
let n5 = BinaryTreeNode::new(9.0);
tree.add_left_node(n1_index, n5);
tree.print();
}
#[test]
fn decision_tree() {
use crate::config::Loss;
use crate::decision_tree::*;
let mut tree = DecisionTree::new();
tree.set_feature_size(3);
tree.set_max_depth(2);
tree.set_min_leaf_size(1);
tree.set_loss(Loss::SquaredError);
let data1 = Data {
feature: vec![1.0, 2.0, 3.0],
target: 2.0,
weight: 1.0,
label: 1.0,
residual: 1.0,
initial_guess: 1.0,
};
let data2 = Data {
feature: vec![1.1, 2.1, 3.1],
target: 1.0,
weight: 1.0,
label: 1.0,
residual: 1.0,
initial_guess: 1.0,
};
let data3 = Data {
feature: vec![2.0, 2.0, 1.0],
target: 0.5,
weight: 1.0,
label: 2.0,
residual: 2.0,
initial_guess: 2.0,
};
let data4 = Data {
feature: vec![2.0, 2.3, 1.2],
target: 3.0,
weight: 1.0,
label: 0.0,
residual: 0.0,
initial_guess: 1.0,
};
let mut dv = Vec::new();
dv.push(data1.clone());
dv.push(data2.clone());
dv.push(data3.clone());
dv.push(data4.clone());
let mut cache = TrainingCache::get_cache(3, &dv, 3);
tree.fit(&dv, &mut cache);
tree.print();
let mut dv = Vec::new();
dv.push(data1.clone());
dv.push(data2.clone());
dv.push(data3.clone());
dv.push(data4.clone());
println!("{:?}", tree.predict(&dv));
}
#[test]
fn build_decision_tree() {
use crate::decision_tree::DecisionTree;
let _ = DecisionTree::new();
}
#[test]
fn config_express() {
use crate::config::Config;
let c = Config::new();
println!("{}", c.to_string());
}
#[test]
fn loss_type() {
use crate::config::{loss2string, string2loss, Loss};
assert_eq!(string2loss("SquaredError"), Loss::SquaredError);
assert_eq!(string2loss("LogLikelyhood"), Loss::LogLikelyhood);
assert_eq!(string2loss("LAD"), Loss::LAD);
assert_eq!(loss2string(&Loss::SquaredError), "SquaredError");
assert_eq!(loss2string(&Loss::LogLikelyhood), "LogLikelyhood");
assert_eq!(loss2string(&Loss::LAD), "LAD");
}
#[test]
fn fitness() {
use crate::decision_tree::*;
let mut dv: DataVec = Vec::new();
dv.push(Data {
feature: Vec::new(),
target: 1.0,
weight: 0.1,
label: 1.0,
residual: 0.5,
initial_guess: VALUE_TYPE_UNKNOWN,
});
dv.push(Data {
feature: Vec::new(),
target: 1.0,
weight: 0.2,
label: 0.0,
residual: 0.5,
initial_guess: VALUE_TYPE_UNKNOWN,
});
dv.push(Data {
feature: Vec::new(),
target: 0.0,
weight: 0.3,
label: 1.0,
residual: 0.5,
initial_guess: VALUE_TYPE_UNKNOWN,
});
dv.push(Data {
feature: Vec::new(),
target: 0.0,
weight: 0.4,
label: 0.0,
residual: 0.5,
initial_guess: VALUE_TYPE_UNKNOWN,
});
use crate::fitness::{
almost_equal, average, label_average, same, weighted_label_median,
weighted_residual_median,
};
assert_eq!(true, almost_equal(0.1, 0.100000000001));
assert_eq!(false, same(&dv, dv.len()));
assert!(almost_equal(0.3, average(&dv, dv.len())));
assert!(almost_equal(0.4, label_average(&dv, dv.len())));
assert!(almost_equal(0.0, weighted_label_median(&dv, dv.len())));
assert!(almost_equal(0.5, weighted_residual_median(&dv, dv.len())));
}
}