-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_VantagePoint.js
165 lines (165 loc) · 7.69 KB
/
2_VantagePoint.js
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
let VantagePointElementCapacity = 1;
let VantagePointMaximumInception =18;
let VantagePointQueryArray = [];
let ResultOfTheQueryVP = [];
let numberOfDimentions;
class VantagePointElement{
constructor(){
this.IndexOfSeed = undefined;
this.Mu = undefined;
this.ListOfIndexElementsInsideMe = [];
this.ListOfIndexElementsOutsideMe = [];
this.InsideVantagePointSon = undefined;
this.OutsideVantagePointSon = undefined;
this.AmountOfElementsInMe = undefined;
this.level = 0;
}
SearchKNeighbors(DataBase, numberOfSamplesInX, DesiredPerplexity){
let threshold;
let RequestedNeighbors = DesiredPerplexity * 1.5;
for(let i = 0 ; i < numberOfSamplesInX; i++){
threshold = 0.0001;
for(let iter=0; iter < 100; iter++){
ResultOfTheQueryVP = [];
this.SearchNeighborOfI(DataBase, i, threshold);
if (ResultOfTheQueryVP.length < RequestedNeighbors){
threshold = (threshold + 0.001) * 2
}else{
iter = 100;
}
}
VantagePointQueryArray[i] = ResultOfTheQueryVP;
}
}
SearchAllNeighbors(DataBase, numberOfSamplesInX, threshold){
for(let i = 0 ; i < numberOfSamplesInX; i++){
ResultOfTheQueryVP = [];
this.SearchNeighborOfI(DataBase, i, -threshold[i]);
VantagePointQueryArray[i] = ResultOfTheQueryVP;
}
}
SearchNeighborOfI(DataBase, Sample, threshold){
//Measure my distance
let DistanceToTheSample = 0;
for(let i=0; i < numberOfDimentions; i++){
let aux1 = DataBase[this.IndexOfSeed][i]-DataBase[Sample][i];
DistanceToTheSample += aux1 * aux1;
}
DistanceToTheSample = Math.sqrt(DistanceToTheSample);
//If i am far away
if(DistanceToTheSample > this.Mu + threshold){
this.SearchOutside(DataBase, Sample, threshold);
return;
}
if(this.Mu > threshold + DistanceToTheSample){
//If i am very very close
this.SearchInside(DataBase, Sample, threshold);
return;
}
//Is in between
this.SearchOutside(DataBase, Sample, threshold);
this.SearchInside(DataBase, Sample, threshold);
}
SearchOutside(DataBase, Sample, threshold){
if(typeof this.OutsideVantagePointSon != 'undefined'){
this.OutsideVantagePointSon.SearchNeighborOfI(DataBase, Sample, threshold);
}else{
for(let i=0; i < this.ListOfIndexElementsOutsideMe.length; i++){
ResultOfTheQueryVP.push(this.ListOfIndexElementsOutsideMe[i]);
}
}
}
SearchInside(DataBase, Sample, threshold){
if(typeof this.InsideVantagePointSon != 'undefined'){
this.InsideVantagePointSon.SearchNeighborOfI(DataBase, Sample, threshold);
}else{
for(let i=0; i < this.ListOfIndexElementsInsideMe.length; i++){
ResultOfTheQueryVP.push(this.ListOfIndexElementsInsideMe[i]);
}
}
}
SelectASeedAndFindMu(DataBase, IndexOfElementsToEvaluate){
numberOfDimentions = DataBase[0].length;
//Select a Seed
let TheChoosenIndex = Math.floor(Math.random()*(IndexOfElementsToEvaluate.length));
this.IndexOfSeed = IndexOfElementsToEvaluate[TheChoosenIndex];
this.AmountOfElementsInMe = IndexOfElementsToEvaluate.length;
//FindTheMu
let NonDefinitiveListOfIndexInsideMe=[];
let NonDefinitiveListOfIndexOutsideMe=[];
let TargetOfElementsInsideMe = Math.floor(this.AmountOfElementsInMe/2);
let aux;
let TopMu = 400;
let BottomMu = 0;
let CounterOfElementsInsideMe = 0;
let ToleranceOfIteration = 100;
let MiddleMu = 10;
for(let iter = 0; iter < ToleranceOfIteration; iter++){
CounterOfElementsInsideMe = 0;
aux = Array(this.AmountOfElementsInMe).fill(0);
for(let Sample = 0; Sample < this.AmountOfElementsInMe; Sample++){
for(let i=0; i < numberOfDimentions; i++){
let aux1 = DataBase[this.IndexOfSeed][i]-DataBase[IndexOfElementsToEvaluate[Sample]][i];
aux[Sample] += aux1 * aux1;
}
aux[Sample] = Math.sqrt(aux[Sample]);
if(aux[Sample] < MiddleMu){
CounterOfElementsInsideMe += 1;
}
}
if(CounterOfElementsInsideMe > TargetOfElementsInsideMe){
TopMu = MiddleMu;
MiddleMu = MiddleMu*0.5;
}else if(CounterOfElementsInsideMe < TargetOfElementsInsideMe){
BottomMu = MiddleMu;
MiddleMu = (MiddleMu + 0.5) * 2;
}else{iter = ToleranceOfIteration}
}
aux = Array(this.AmountOfElementsInMe).fill(0);
for(let Sample = 0; Sample < this.AmountOfElementsInMe; Sample++){
for(let i=0; i < numberOfDimentions; i++){
let aux1 = DataBase[this.IndexOfSeed][i]-DataBase[IndexOfElementsToEvaluate[Sample]][i];
aux[Sample] += aux1 * aux1;
}
aux[Sample] = Math.sqrt(aux[Sample]);
}
for(let iter = 0; iter < ToleranceOfIteration; iter++){
NonDefinitiveListOfIndexOutsideMe = [];
NonDefinitiveListOfIndexInsideMe = [];
CounterOfElementsInsideMe = 0;
for(let Sample = 0; Sample < this.AmountOfElementsInMe; Sample++){
if(aux[Sample] > MiddleMu){
NonDefinitiveListOfIndexOutsideMe.push(IndexOfElementsToEvaluate[Sample]);
}else{
NonDefinitiveListOfIndexInsideMe.push(IndexOfElementsToEvaluate[Sample]);
CounterOfElementsInsideMe += 1;
}
}
if (CounterOfElementsInsideMe == TargetOfElementsInsideMe){
iter = ToleranceOfIteration;
}else if(CounterOfElementsInsideMe > TargetOfElementsInsideMe){
TopMu = MiddleMu;
MiddleMu = (TopMu + BottomMu)*0.5;
}else{
BottomMu = MiddleMu;
MiddleMu = (TopMu + BottomMu)*0.5;
}
}
this.ListOfIndexElementsInsideMe = NonDefinitiveListOfIndexInsideMe;
this.ListOfIndexElementsOutsideMe = NonDefinitiveListOfIndexOutsideMe;
this.Mu = MiddleMu;
//Should I be Divided
if(this.ListOfIndexElementsInsideMe.length > VantagePointElementCapacity && this.level < VantagePointMaximumInception){
this.InsideVantagePointSon = new VantagePointElement();
this.InsideVantagePointSon.level = this.level + 1;
this.InsideVantagePointSon.SelectASeedAndFindMu(DataBase, this.ListOfIndexElementsInsideMe);
delete this.ListOfIndexElementsInsideMe; //This is recommended to aliviate memory. but is not mandatory. Specially if you are looking for an error;
}
if(this.ListOfIndexElementsOutsideMe.length > VantagePointElementCapacity && this.level < VantagePointMaximumInception){
this.OutsideVantagePointSon = new VantagePointElement();
this.OutsideVantagePointSon.level = this.level + 1;
this.OutsideVantagePointSon.SelectASeedAndFindMu(DataBase, this.ListOfIndexElementsOutsideMe);
delete this.ListOfIndexElementsOutsideMe; //This is recommended to aliviate memory. but is not mandatory Specially if you are looking for an error;
}
}
}