-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathOnlineIF.cpp
More file actions
67 lines (54 loc) · 1.76 KB
/
Copy pathOnlineIF.cpp
File metadata and controls
67 lines (54 loc) · 1.76 KB
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
#include "OnlineIF.hpp"
OnlineIF::OnlineIF(int _ntree, doubleframe* _df, int _nsample, int _maxheight,
bool _rsample, int _windowSize) :
Forest(_ntree, _nsample, _maxheight, _rsample) {
this->windowSize = _windowSize;
this->updateCount = 0;
genInitTreeStructures(_df);
}
/*
* Generate initial tree structures from offline isolation
* forest using first windowSize instances
*/
void OnlineIF::genInitTreeStructures(doubleframe *dataset){
std::vector<int> sampleIndex;
int *rndIdx = new int[dataset->nrow];
for(int i = 0; i < dataset->nrow; ++i)
rndIdx[i] = i;
for (int n = 0; n < this->ntree; n++) {
// Sample and shuffle the data.
sampleIndex.clear();
getSample(sampleIndex, nsample, rsample, windowSize, rndIdx);
Tree::initializeLBandUB(dataset, sampleIndex);
// build trees
Tree *tree = new Tree();
tree->iTree(sampleIndex, dataset, maxheight);
this->trees.push_back(tree); //add tree to forest
}
delete []rndIdx;
}
double OnlineIF::instanceScore(double *inst) {
double avgPathLength = mean(pathLength(inst));
double scores = pow(2, -avgPathLength / avgPL(this->nsample));
return scores;
}
/*
* Update corresponding nodes in the trees for a new instance
*/
void OnlineIF::update(double inst[]){
// double p = this->nsample/double(this->windowSize);
for(int i = 0; i < this->ntree; ++i){
// if(randomD(0, 1) < p)
this->trees[i]->update(inst);
}
this->updateCount++;
// After performing windowSize updates renew trees with new nodeSizes
if(this->updateCount == this->windowSize){
for(int i = 0; i < this->ntree; ++i){
this->trees[i]->renewNodeSize();
}
// reset update count to start again
this->updateCount = 0;
// std::cout << "Tree Renewed" << std::endl;
}
}