Re-write flow and API for hybrid, still broken

This commit is contained in:
Pitchaya Boonsarngsuk
2018-01-26 17:31:04 +00:00
parent 9d6b3e066b
commit 47d9c2d5bb
2 changed files with 122 additions and 86 deletions

View File

@@ -9,33 +9,37 @@ function startHybridSimulation() {
simulation.stop(); simulation.stop();
p1 = performance.now(); p1 = performance.now();
configuration = { function distance (s, t) {
iteration: ITERATIONS, return distanceFunction(s, t, props, norm);
neighbourSize: NEIGHBOUR_SIZE, }
sampleSize: SAMPLE_SIZE,
distanceRange: SELECTED_DISTANCE,
fullIterations: FULL_ITERATIONS,
fullNeighbourSize: FULL_NEIGHBOUR_SIZE,
fullSampleSize: FULL_SAMPLE_SIZE,
fullDistanceRange: FULL_SELECTED_DISTANCE,
distanceFn: function (s, t) {return distanceFunction(s, t, props, norm);},
pivots: PIVOTS,
numPivots: NUM_PIVOTS
};
console.log(configuration);
hybridSimulation = d3.hybridSimulation(nodes, configuration);
let sample = hybridSimulation.sample(); let forceSample = d3.forceNeighbourSamplingDistance()
let remainder = hybridSimulation.remainder(); .neighbourSize(NEIGHBOUR_SIZE)
.sampleSize(SAMPLE_SIZE)
.distance(distance)
addNodesToDOM(sample); let forceFull = d3.forceNeighbourSamplingDistance()
.neighbourSize(FULL_NEIGHBOUR_SIZE)
.sampleSize(FULL_SAMPLE_SIZE)
.distance(distance)
hybridSimulation let hybridSimulation = d3.hybridSimulation(simulation, forceSample, forceFull)
.sampleIterations(ITERATIONS)
.fullIterations(FULL_ITERATIONS)
.pivots(PIVOTS)
.numPivots(NUM_PIVOTS)
.interpFindTuneIts(INTERP_ENDING_ITS)
.interpDistanceFn(distance)
.on("sampleTick", ticked) .on("sampleTick", ticked)
.on("fullTick", ticked) .on("fullTick", ticked)
.on("startFull", startedFull) .on("startInterp", startedFull)
.on("end", ended); .on("end", ended);
let sample = hybridSimulation.sample();
addNodesToDOM(sample);
hybridSimulation.restart();
function startedFull() { function startedFull() {
console.log("startedFull"); console.log("startedFull");
d3.selectAll(".nodes").remove(); d3.selectAll(".nodes").remove();

View File

@@ -2,60 +2,90 @@ import {dispatch} from "d3-dispatch";
import constant from "./constant"; import constant from "./constant";
import interpBruteForce from "./interpolation/interpBruteForce"; import interpBruteForce from "./interpolation/interpBruteForce";
import interpolationPivots from "./interpolation/interpolationPivots"; import interpolationPivots from "./interpolation/interpolationPivots";
import neighbourSamplingDistance from "./neighbourSamplingDistance";
import {takeSampleFrom} from "./interpolation/helpers"; import {takeSampleFrom} from "./interpolation/helpers";
export default function (nodes, config) { export default function (sim, forceS, forceF) {
var hybrid, var
fullSimulation, SAMPLE_ITERATIONS = 300,
SAMPLE_ITERATIONS = readConf(config, "iteration", 300), FULL_ITERATIONS = 20,
neighbourSize = readConf(config, "neighbourSize", 6), interpDistanceFn,
sampleSize = readConf(config, "sampleSize", 3), PIVOTS = false,
distanceRange = readConf(config, "distanceRange", 10), NUMPIVOTS = 3,
FULL_ITERATIONS = readConf(config, "fullIterations", 20), INTERP_FINE_ITS = 20,
FullneighbourSize = readConf(config, "fullNeighbourSize", neighbourSize), sample = [],
FullsampleSize = readConf(config, "fullSampleSize", sampleSize), remainder = [],
FulldistanceRange = readConf(config, "fullDistanceRange", distanceRange), simulation = sim,
distanceFn = readConf(config, "distanceFn", constant(300)), forceSample = forceS,
PIVOTS = readConf(config, "pivots", false), forceFull = forceF,
NUMPIVOTS = readConf(config, "numPivots", 3), event = d3.dispatch("sampleTick", "fullTick", "startInterp", "end"),
event = d3.dispatch("sampleTick", "fullTick", "startFull", "end"); toInit = true,
nodes,
alreadyRanIterations,
hybrid;
var sets = takeSampleFrom(nodes, Math.sqrt(nodes.length)); if(simulation != undefined) initSimulation();
var sample = sets.sample; if(forceS != undefined || forceF != undefined) initForces();
var remainder = sets.remainder;
var sampleSimulation = d3.forceSimulation(sample) function initialize() {
.stop() toInit = false;
.alphaDecay(1 - Math.pow(0.001, 1 / SAMPLE_ITERATIONS)) console.log("Initializing Hybrid");
alreadyRanIterations = 0;
simulation
.on("tick", function () { .on("tick", function () {
event.call("sampleTick", sampleSimulation); event.call("sampleTick");
console.log("InternalTick");
alreadyRanIterations++;
if(alreadyRanIterations >= SAMPLE_ITERATIONS){
ended();
}
}) })
.on("end", ended); .on("end", ended)
.nodes(sample)
.force("Sample force", forceSample);
console.log("Initialized Hybrid");
}
sampleSimulation function initForces(){
.force("forces", neighbourSamplingDistance() if (typeof forceSample.stableVelocity == 'function' &&
.neighbourSize(neighbourSize) typeof forceSample.stableVeloHandler == 'function') {
.sampleSize(sampleSize) forceSample
.distanceRange(distanceRange) .stableVelocity(0.000001)
.distance(distanceFn) .stableVeloHandler(ended);
.stableVelocity(0.004) }
.stableVeloHandler(function(){sampleSimulation.stop(); ended();})
) if(interpDistanceFn === undefined &&
.alpha(1).restart(); typeof forceFull.distance == 'function') {
interpDistanceFn = forceFull.distance();
}
}
function initSimulation(){
nodes = simulation.nodes();
simulation
.stop()
.alphaDecay(0)
.alpha(1)
let sets = takeSampleFrom(nodes, Math.sqrt(nodes.length));
sample = sets.sample;
remainder = sets.remainder;
}
function ended() { function ended() {
sample.forEach(function (d) {
d.vx = 0;
d.vy = 0;
});
event.call("startFull");
console.log("Ended sample simulation"); console.log("Ended sample simulation");
event.call("startInterp");
simulation.stop();
simulation.force("Sample force", null);
for (let i=sample.size-1; i>=0; i--){
sample[i].vx=0;
sample[i].vy=0;
}
alert('About to interpolate'); alert('About to interpolate');
if (PIVOTS) { if (PIVOTS) {
interpolationPivots(sample, remainder, NUMPIVOTS, distanceFn); interpolationPivots(sample, remainder, NUMPIVOTS, distanceFn, INTERP_FINE_ITS);
} else { } else {
interpBruteForce(sample, remainder, distanceFn); interpBruteForce(sample, remainder, distanceFn, INTERP_FINE_ITS);
} }
event.call("fullTick"); event.call("fullTick");
alert('About to Full run'); alert('About to Full run');
@@ -88,17 +118,14 @@ export default function (nodes, config) {
} }
return hybrid = { return hybrid = {
distance: function (_) { restart: function () {
return arguments.length ? (distance = typeof _ === "function" ? _ : constant(+_), hybrid) : distance; if(toInit) initialize();
simulation.restart();
return hybrid;
}, },
stop: function () { stop: function () {
if (typeof sampleSimulation !== 'undefined') { simulation.stop();
sampleSimulation.stop();
}
if (typeof fullSimulation !== 'undefined') {
fullSimulation.stop();
}
return hybrid; return hybrid;
}, },
@@ -110,10 +137,6 @@ export default function (nodes, config) {
return arguments.length ? (NUMPIVOTS = +_, hybrid) : NUMPIVOTS; return arguments.length ? (NUMPIVOTS = +_, hybrid) : NUMPIVOTS;
}, },
multiplier: function (_) {
return arguments.length ? (MULTIPLIER = +_, hybrid) : MULTIPLIER;
},
sampleIterations: function (_) { sampleIterations: function (_) {
return arguments.length ? (SAMPLE_ITERATIONS = +_, hybrid) : SAMPLE_ITERATIONS; return arguments.length ? (SAMPLE_ITERATIONS = +_, hybrid) : SAMPLE_ITERATIONS;
}, },
@@ -122,12 +145,12 @@ export default function (nodes, config) {
return arguments.length ? (FULL_ITERATIONS = +_, hybrid) : FULL_ITERATIONS; return arguments.length ? (FULL_ITERATIONS = +_, hybrid) : FULL_ITERATIONS;
}, },
neighbourSize: function (_) { interpDistanceFn: function (_) {
return arguments.length ? (neighbourSize = +_, hybrid) : neighbourSize; return arguments.length ? (interpDistanceFn = +_, hybrid) : interpDistanceFn;
}, },
sampleSize: function (_) { interpFindTuneIts: function (_) {
return arguments.length ? (sampleSize = +_, hybrid) : sampleSize; return arguments.length ? (INTERP_FINE_ITS = +_, hybrid) : INTERP_FINE_ITS;
}, },
on: function (name, _) { on: function (name, _) {
@@ -142,12 +165,21 @@ export default function (nodes, config) {
return arguments.length ? (remainder = _, hybrid) : remainder; return arguments.length ? (remainder = _, hybrid) : remainder;
}, },
stress: function () { interpDistanceFn: function (_) {
return fullSimulation.force("neighbourSampling").stress(); return arguments.length ? (interpDistanceFn = typeof _ === "function" ? _ : constant(+_), hybrid) : interpDistanceFn;
} },
simulation: function (_) {
return arguments.length ? (toInit = true, simulation = _, hybrid) : simulation;
},
forceSample: function (_) {
return arguments.length ? (forceSample = _, initForces(), hybrid) : forceSample;
},
forceFull: function (_) {
return arguments.length ? (forceFull = _, initForces(), hybrid) : forceFull;
},
}; };
} }
function readConf(config, prop, def){
return config.hasOwnProperty(prop) ? config[prop] : def;
}