-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
408 lines (302 loc) · 7.4 KB
/
test.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "CS207/Util.hpp"
//#include "Point.hpp"
//#include <algorithm>
#include <vector>
#include <cassert>
//#include <map>
using namespace std;
#include "Sampler.hpp"
//#include <unordered_set>
//#include "Graph.hpp"
#include <float.h>
//#include <limits.h>
#include "Manager.hpp"
#include "Selector.hpp"
//#include <cstdlib>
//#include <math.h>
struct policy_value_type;
struct X_type;
typedef X_type X;
struct Y_type;
typedef Y_type Y;
//template <typename X>
//struct domain_value_type;
typedef vector<float> S;
typedef policy_value_type P;
typedef Sampler<P,S> SamplerType;
typedef SamplerType::Policy policy;
typedef SamplerType::size_type size_type;
//typedef typename SamplerType::Samples C;
typedef S C;
//typedef domain_value_type D;
//Example of using the model manager with two different types of models
struct LearningModelType; //This will be class of models a manager stores
struct Regression_type; //This will be a type of Learning Model;
//typedef LearningModelType<regression_type,X_type,Y_type> Regression;
//typedef Manager<regression_type> ManagerType;
//typedef Selector<Regression,X,Y> SelectorType;
//typedef SelectorType::error_type error_type;
//typedef SelectorType::ManagerType Regression;
//element must have a size() element as well
//must have an insert method as well
template <typename V,typename Vec>
struct V_type{
private:
Vec x_;
public:
typedef typename Vec::iterator iterator;
size_type size() const{
return x_.size();
}
void add_vector(Vec t){
for (size_type i=0; i < t.size(); ++i)
x_.push_back(t[i]);
}
/*void add_element(Vec::value_type s){
x_.push_back(s);
}*/
bool operator==(const V& y) const{
return y.x_ == x_;
}
/*void remove_element(size_type idx){
vector<double> tmp;
tmp.copy(x_.begin(),x_.begin()+idx);
tmp.copy(x.begin()+idx+1,x.end());
x_ = tmp;
copy_if(x_.begin(),x_.end(),tmp.begin(), );
}*/
bool operator<(const V& y) const{
return x_ < y.x_;
}
iterator begin(){
return x_.begin();
}
iterator end(){
return x_.end();
}
void insert(V val){
x_.insert(x_.begin(),val);
}
};
struct X_type: totally_ordered<X_type>, V_type<X_type, vector<double> >{
private:
vector<double> x_;
public:
void x(vector<double> t){
for (size_type i=0; i < t.size(); ++i)
x_.push_back(t[i]);
}
bool operator==(const X_type& x) const{
/*if (x.size() != x_.size())
return false;
for(size_type i=0; i< x.size(); ++i){
if (x[i] != x_[i])
return false;
return true;*/
return x_ == x.x_;
}
bool operator<(const X_type& x) const{
return x_ < x.x_;
}
vector<double>::iterator begin(){
return x_.begin();
}
vector<double>::iterator end(){
return x_.end();
}
};
struct Y_type: totally_ordered<Y_type>, V_type<int, std::vector<int> >{
};
/*struct domain_value_type: domain_type<X>{
size_type id;
bool in_domain(X x){
(void) x;
return (rand()%100 > 90);
}
};*/
struct random_forest_type;
typedef random_forest_type F;
typedef Manager<F> RandomForest;
struct policy_value_type: pvt<C> {
private:
std::vector<C> s_;
public:
policy_value_type(){
}
void collect(){
size_type num_ele = 10;
size_type num_samples = 1+rand()%15;
C tmp = C();
for(size_type i = 0; i < num_samples; ++i){
//C::value_type tmp;
tmp.clear();
for(size_type j=0; j < num_ele; ++j){
tmp.push_back(rand()%400);//tmp.insert(tmp.end(),rand()%4000);
}
s_.push_back(tmp);//s_.insert(s_.end(),tmp);
}
}
void clear(){
s_.clear();
}
void stats(){
cout << "Number in vector: " << s_.size() << endl;
for(auto it = s_.begin(); it != s_.end(); ++it){
for(auto itt = (*it).begin(); itt != (*it).end(); ++itt)
cout << *itt << " ";
cout << endl;
}
cout << endl;
}
bool has_met_limit(){
return (rand()%100 < 10);
}
std::vector<C>& samples(){
return s_;
}
};
//template <typename X, typename Y>
struct Regression_type{
private:
vector<double> w_;
bool has_fit_;
public:
Regression_type():w_(),has_fit_(false){
}
void fit(X_type& x,Y_type& y){
(void) x;
(void) y;
cout << "Fitting parameters to training examples";
w_.clear();
w_.push_back(rand()%100);
w_.push_back(rand()%100);
w_.push_back(rand()%100);
w_.push_back(rand()%100);
has_fit_ = true;
}
Y predict(X_type& x){
(void) x;
//do some calculation
Y_type y_hat;
for (size_type i=0; i < x.size(); ++i)
y_hat.insert(rand()%1000);
return y_hat;
}
double score(X_type x,Y_type y){
if (!has_fit_)
return 0.0;
auto y_hat = predict(x);
assert(y_hat.size()==y.size());
auto it2 = y_hat.begin();
double loss_val = 0.0;
for (auto it1 = y.begin(); it1 < y.end(); ++it1, ++it2){
loss_val += pow((*it1)-(*it2),2);
}
return loss_val;
}
void set_params(vector<double> w){
w_.clear();
w_.insert(w_.begin(),w.begin(),w.end());
}
vector<double> get_params(){
return w_;
}
void operator=(Regression_type r){
set_params(r.w_);
r.has_fit_ = has_fit_;
}
bool operator==(Regression_type r){
return (w_==r.w_) && (has_fit_==r.has_fit_);
}
bool operator!=(Regression_type r){
return !((*this)==r);
}
};
/*template <typename Q, typename X, typename Y>
struct LearningModelType {
private:
typedef Q<X,Y> P;
P p_;
public:
LearningModelType(P p): p_(p){};
void fit(X& x,Y& y){
p_.fit(x,y);
}
Y predict(X& x){
return p_.predict(x);
}
double score(X x,Y y)){
return p_.score(x,y);
}
void set_params(P p){
p_.set_params(p);
}
P get_params(){
return p_.get_params();
}
};*/
/**Timers**/
CS207::Clock start_time(){
CS207::Clock t;
return t;
}
void end_time(CS207::Clock t){
cout << "Time: " << chrono::duration_cast<chrono::nanoseconds>(t.elapsed()).count() << '\n';
}
void visual_output(SamplerType Sa,string doing){
(void) Sa;
cout << "I just did something " << doing << endl;
cout<<endl;
}
int main(){
CS207::Clock t;
SamplerType Samp;
t = start_time();
visual_output(Samp,"initiated variable with samples");
SamplerType::Policy p1 = Samp.create_policy(25,60);
SamplerType::Policy p2 = Samp.create_policy(50,60);
visual_output(Samp,"created 2 policies");
Samp.stats();
Samp.start_collections();
Samp.stats();
visual_output(Samp,"initiated all collections");
p2.delete_samples();
visual_output(Samp,"deleted all samples in one policy");
Samp.stats();
Manager<Regression_type,Regression_type> mgr;
Regression_type r1;
mgr.add_model(r1,r1);
cout << "Number of models:" <<mgr.num_models() << endl;
/*
vector<double> vt;
vt.push_back(5.5);
vt.push_back(5.7);
vt.push_back(23);
vt.push_back(26.5);
vt.push_back(1.5);
regression_type rt1 {0,vt,10};
rt1.update_w(0,5);
rt1.update_w(1,6.6);
rt1.update_w(2,0.25);
rt1.update_w(3,1.25);
auto m1 = r1.add_model(rt1);//causes segmentation fault
(void) m1;
regression_type rt2{1,vt,5};
rt2.update_w(0,7);
rt2.update_w(1,7.6);
rt2.update_w(2,9.25);
auto m2 = r1.add_model(rt2);
cout << "Currently have "<< r1.num_models() << " loaded models" <<endl <<endl;
SelectorType s;
X x1;
Y y1;
s.train(r1, m1, x1, y1);
Y y_hat = s.predict(r1, m1, x1);
y_hat.add_element(0,1.5);
double loss_overall = s.calculate_loss(r1,m1, y1,y_hat);
cout << "Model Loss is"<< loss_overall << " units" <<endl <<endl;
auto best_m = s.best_model(r1, x1, x1,y1,y1);
cout << "Overall loss is " << loss_overall;
(void) best_m;
(void) m2; */
}