Update 'src/interpolation/interpBruteForce.js'

This commit is contained in:
brian
2018-03-16 10:19:13 +07:00
parent 5b75f5b588
commit 6335389c7c

View File

@@ -1,48 +1,48 @@
import {pointOnCircle, takeSampleFrom} from "./helpers"; import {takeSampleFrom} from "./helpers";
import {placeNearToNearestNeighbour} from "./interpCommon"; import {placeNearToNearestNeighbour} from "./interpCommon";
/** /**
* Perform interpolation where the "parent" node is found by brute-force. * Perform interpolation where the "parent" node is found by brute-force.
* A "parent" of a node to be interpolated is a node whose position in 2D space * A "parent" of a node to be interpolated is a node whose position in 2D space
* is already known and have the least high-dimensional distance to the node in * is already known and have the least high-dimensional distance to the node in
* question. * question.
* For each point to be interpolated: * For each point to be interpolated:
* - Phase 1: find the "parent" by comparing high-d distance against every * - Phase 1: find the "parent" by comparing high-d distance against every
points already plotted on the graph. points already plotted on the graph.
this is essentially a nearest neighbour finding problem. this is essentially a nearest neighbour finding problem.
* - Phase 2 and 3 are passed onto placeNearToNearestNeighbour * - Phase 2 and 3 are passed onto placeNearToNearestNeighbour
* @param {list} sampleSet - nodes already plotted on the 2D graph * @param {list} sampleSet - nodes already plotted on the 2D graph
* @param {list} remainderSet - nodes to be interpolated onto the graph * @param {list} remainderSet - nodes to be interpolated onto the graph
* @param {function} distanceFn - f(nodex, nodey) that calculate high-dimensional * @param {function} distanceFn - f(nodex, nodey) that calculate high-dimensional
* distance between 2 nodes * distance between 2 nodes
* @param {number} endingIts - for phase 3, how many iterations to refine the * @param {number} endingIts - for phase 3, how many iterations to refine the
* placement of each interpolated point * placement of each interpolated point
*/ */
export default function(sampleSet, remainderSet, distanceFn, endingIts) { export default function(sampleSet, remainderSet, distanceFn, endingIts) {
let let
sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample, sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample,
sampleSubsetDistanceCache = []; sampleSubsetDistanceCache = [];
// For each datapoint "node" to be interpolated // For each datapoint "node" to be interpolated
for (let i = remainderSet.length-1; i>=0; i--) { for (let i = remainderSet.length-1; i>=0; i--) {
let let
node = remainderSet[i], node = remainderSet[i],
nearestSample, minDist, sample, dist, index; nearestSample, minDist, sample, dist, index;
// For each datapoint "sample" in the sample set // For each datapoint "sample" in the sample set
for (let j = sampleSet.length-1; j>=0; j--) { for (let j = sampleSet.length-1; j>=0; j--) {
sample = sampleSet[j]; sample = sampleSet[j];
dist = distanceFn(node, sample); dist = distanceFn(node, sample);
if (nearestSample === undefined || dist < minDist) { if (nearestSample === undefined || dist < minDist) {
minDist = dist; minDist = dist;
nearestSample = sample; nearestSample = sample;
} }
index = sampleSubset.indexOf(sample); index = sampleSubset.indexOf(sample);
if (index !== -1) if (index !== -1)
sampleSubsetDistanceCache[index] = dist; sampleSubsetDistanceCache[index] = dist;
} }
placeNearToNearestNeighbour(node, nearestSample, minDist, sampleSubset, sampleSubsetDistanceCache, endingIts); placeNearToNearestNeighbour(node, nearestSample, minDist, sampleSubset, sampleSubsetDistanceCache, endingIts);
} }
} }