Pivot near neighbour finding working (PROPER)
This commit is contained in:
@@ -4,61 +4,62 @@ import {placeNearToNearestNeighbour} from "./interpCommon";
|
|||||||
export default function(sampleSet, remainderSet, numPivots, distanceFn) {
|
export default function(sampleSet, remainderSet, numPivots, distanceFn) {
|
||||||
// Pivot based parent finding
|
// Pivot based parent finding
|
||||||
let numBuckets = Math.floor(Math.sqrt(sampleSet.length));
|
let numBuckets = Math.floor(Math.sqrt(sampleSet.length));
|
||||||
let pivots = takeSampleFrom(sampleSet, numPivots);
|
let sets = takeSampleFrom(sampleSet, numPivots);
|
||||||
console.log("Pivots", pivots);
|
let pivots = sets.sample;
|
||||||
// Common temporary variables
|
let nonPivotSamples = sets.remainder;
|
||||||
let i, j, pivot, sample, bucketWidth; // Temp var, declared seperately to avoid GC
|
|
||||||
|
|
||||||
let pivotsBuckets = []; // [ For each Pivot:[For each bucket:[each point in bucket]] ]
|
let pivotsBuckets = []; // [ For each Pivot:[For each bucket:[each point in bucket]] ]
|
||||||
|
for (let i = 0; i < numPivots; i++) {
|
||||||
for (i = 0; i < numPivots; i++) {
|
|
||||||
pivotsBuckets[i] = [];
|
pivotsBuckets[i] = [];
|
||||||
for (j = 0; j < numBuckets; j++) {
|
for (let j = 0; j < numBuckets; j++) {
|
||||||
pivotsBuckets[i][j] = [];
|
pivotsBuckets[i][j] = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pre-calculate distance between each sample to each pivot
|
// Pre-calculate distance between each non-pivot sample to each pivot
|
||||||
let distCache = [], // [ For each Sample:[For each Pivot: distance] ]
|
// At the same time, determine the bucket width for each pivot based on furthest non-pivot sample
|
||||||
bucketWidths = []; // [ For each Pivot: width of each bucket ]
|
let distCache = []; // [ For each non-pivot Sample:[For each Pivot: distance] ]
|
||||||
|
let bucketWidths = []; // [ For each Pivot: width of each bucket ]
|
||||||
|
|
||||||
for (i = 0; i < sampleSet.length; i++)
|
for (let i = 0; i < nonPivotSamples.length; i++)
|
||||||
distCache[i] = [];
|
distCache[i] = [];
|
||||||
|
|
||||||
for (j = 0; j < numPivots; j++) {
|
for (let j = 0; j < numPivots; j++) {
|
||||||
pivot = pivots[j],
|
let pivot = pivots[j];
|
||||||
maxDist = -1;
|
let maxDist = -1;
|
||||||
|
|
||||||
for (i = 0; i < sampleSet.length; i++) {
|
for (let i = 0; i < nonPivotSamples.length; i++) {
|
||||||
sample = sampleSet[i];
|
let sample = nonPivotSamples[i];
|
||||||
if (pivot !== sample) {
|
|
||||||
distCache[i][j] = distanceFn(pivot, sample);
|
distCache[i][j] = distanceFn(pivot, sample);
|
||||||
if (distCache[i][j] > maxDist)
|
if (distCache[i][j] > maxDist)
|
||||||
maxDist = distCache[i][j];
|
maxDist = distCache[i][j];
|
||||||
} else {
|
|
||||||
distCache[i][j] = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bucketWidths.push(maxDist / numBuckets);
|
bucketWidths.push(maxDist / numBuckets);
|
||||||
}
|
}
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
// console.log(distCaches);
|
// Put samples (pivot not included) into buckets
|
||||||
|
for (let j = 0; j < numPivots; j++) {
|
||||||
// Put samples (pivot included) into buckets
|
let bucketWidth = bucketWidths[j];
|
||||||
for (j = 0; j < numPivots; j++) {
|
for (let i = 0; i < nonPivotSamples.length; i++) {
|
||||||
bucketWidth = bucketWidths[j];
|
let sample = nonPivotSamples[i];
|
||||||
for (i = 0; i < sampleSet.length; i++) {
|
let bucketNumber = Math.floor(distCache[i][j] / bucketWidth);
|
||||||
sample = sampleSet[i];
|
if (bucketNumber >= numBuckets) {
|
||||||
pivotsBuckets[j][Math.floor(distCache[i][j] / bucketWidth)].push(sample);
|
bucketNumber = numBuckets - 1;
|
||||||
|
} else if (bucketNumber < 0) { // Should never be negative anyway
|
||||||
|
bucketNumber = 0;
|
||||||
|
}
|
||||||
|
pivotsBuckets[j][bucketNumber].push(sample);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample;
|
||||||
//Plot each of the remainder nodes
|
//Plot each of the remainder nodes
|
||||||
for (let node of remainderSet) {
|
for (let node of remainderSet) {
|
||||||
let sampleSubsetDistanceCache = [],
|
let sampleSubsetDistanceCache = [],
|
||||||
sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample;
|
minDist, nearSample;
|
||||||
|
|
||||||
// Pivot based parent search
|
// Pivot based parent search
|
||||||
for (let p = 0; p < numPivots; p++) {
|
for (let p = 0; p < numPivots; p++) {
|
||||||
@@ -67,8 +68,13 @@ export default function(sampleSet, remainderSet, numPivots, distanceFn) {
|
|||||||
|
|
||||||
let dist = distanceFn(node, pivot);
|
let dist = distanceFn(node, pivot);
|
||||||
let index = sampleSubset.indexOf(pivot);
|
let index = sampleSubset.indexOf(pivot);
|
||||||
if (index !== -1)
|
if (index !== -1) {
|
||||||
sampleSubsetDistanceCache[index] = dist;
|
sampleSubsetDistanceCache[index] = dist;
|
||||||
|
}
|
||||||
|
if (minDist === undefined || dist < minDist){
|
||||||
|
minDist = dist;
|
||||||
|
nearSample = pivot;
|
||||||
|
}
|
||||||
|
|
||||||
let bucketNumber = Math.floor(dist / bucketWidth);
|
let bucketNumber = Math.floor(dist / bucketWidth);
|
||||||
if (bucketNumber >= numBuckets) {
|
if (bucketNumber >= numBuckets) {
|
||||||
@@ -77,22 +83,28 @@ export default function(sampleSet, remainderSet, numPivots, distanceFn) {
|
|||||||
bucketNumber = 0;
|
bucketNumber = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let clDist, minNode;
|
|
||||||
for (let candidateNode of pivotsBuckets[p][bucketNumber]) {
|
for (let candidateNode of pivotsBuckets[p][bucketNumber]) {
|
||||||
|
let index = sampleSubset.indexOf(candidateNode);
|
||||||
|
if (index !== -1 && sampleSubsetDistanceCache[index] !== undefined)
|
||||||
|
dist = sampleSubsetDistanceCache[index]
|
||||||
|
else {
|
||||||
dist = distanceFn(candidateNode, node);
|
dist = distanceFn(candidateNode, node);
|
||||||
if (candidateNode <= clDist) {
|
if (index !== -1)
|
||||||
clDist = candidateNode;
|
sampleSubsetDistanceCache[index] = dist;
|
||||||
minNode = bucketContents[w];
|
}
|
||||||
|
|
||||||
|
if (dist < minDist){
|
||||||
|
minDist = dist;
|
||||||
|
nearSample = candidateNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fill in holes in cache
|
||||||
for (let k = 0; k < sampleSubset.length; k++) {
|
for (let k = 0; k < sampleSubset.length; k++) {
|
||||||
if (sampleSubsetDistanceCache[k] !== undefined)
|
if (sampleSubsetDistanceCache[k] === undefined)
|
||||||
sampleSubsetDistanceCache[k] = distanceFn(node, sampleSubset[k]);
|
sampleSubsetDistanceCache[k] = distanceFn(node, sampleSubset[k]);
|
||||||
}
|
}
|
||||||
let radius = distanceFn(node, minNode);
|
placeNearToNearestNeighbour(node, nearSample, minDist, sampleSubset, sampleSubsetDistanceCache);
|
||||||
placeNearToNearestNeighbour(node, minNode, radius, sampleSubset, sampleSubsetDistanceCache);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user