Separate a Link Force with optimization

This commit is contained in:
Pitchaya Boonsarngsuk
2018-01-24 21:23:25 +00:00
parent 36ddbd13c4
commit 89bc947409
4 changed files with 80 additions and 124 deletions

View File

@@ -41,7 +41,8 @@ var nodes, // as in Data points
fileName = "",
selectedData,
clickedIndex = -1,
paused = false;
paused = false,
alreadyRanIterations;
// Default parameters
var MULTIPLIER = 50,
@@ -119,10 +120,16 @@ function processData(data, error) {
props.pop(); //Hide Iris index / last column from distance function
//Put the nodes in random starting positions
//TODO Change this back
nodes.forEach(function (d) {
d.x = 0;
d.y = 0;
});
/*
nodes.forEach(function (d) {
d.x = (Math.random()-0.5) * 100000;
d.y = (Math.random()-0.5) * 100000;
});
});*/
addNodesToDOM(nodes);
@@ -180,6 +187,8 @@ function addNodesToDOM(data) {
}
function ticked() {
console.log("ticked");
alreadyRanIterations++;
// If rendering is selected, then draw at every iteration.
if (rendering === true) {
node // Each sub-circle in the SVG, update cx and cy
@@ -194,9 +203,14 @@ function ticked() {
if (springForce) {
intercom.emit("passedData", simulation.force(forceName).distributionData());
}
if(alreadyRanIterations == ITERATIONS) {
simulation.stop();
ended();
}
}
function ended() {
console.log("ended");
if (rendering !== true) { // Never drawn anything before? Now it's time.
node
.attr("cx", function (d) {

View File

@@ -1,40 +1,45 @@
/**
* Initialize the link force algorithm and start simulation.
*/
function startLinkSimulation() {
console.log("startLinkSimulation")
springForce = false;
alreadyRanIterations = 0;
simulation.stop();
p1 = performance.now();
let links = [];
// Initialize link array.
nodes = simulation.nodes();
for (i = 0; i < nodes.length; i++) {
for (j = 0; j < i; j++) {
if (i !== j) {
links.push({
source: nodes[i],
target: nodes[j],
});
}
for (i = nodes.length-1; i >= 1; i--) {
for (j = i-1; j >= 0; j--) {
links.push({
source: nodes[i],
target: nodes[j],
});
}
}
// Add the links to the simulation.
simulation.force(forceName, d3.forceLink().links(links));
simulation
.alphaDecay(1 - Math.pow(0.001, 1 / ITERATIONS))
/* Add force
* Please add the distance function before feeding the force
* it to the simulation or adding links.
*
* On setting the distance fn and being initialized by the simulation, the
* force will pre-calculate high-dimensional distances of every link and
* store that as cache.
* Adding distance fn before links means that the first pre-calculation will
* calculate noting as there was no link.
* The full pre-calculation will then occur once when the force is being
* initialized by the simulation.
*/
simulation.force(forceName,
d3.forceLinkOptimized()
.distance(function (n) {
return distanceFunction(n.source, n.target, props, norm);
})
.links(links)
).alphaDecay(0)
//.velocityDecay(0.8)
.force(forceName)
// The distance function that will be used to calculate distances
// between nodes.
.distance(function (n) {
return distanceFunction(n.source, n.target, props, norm);
})
// Set the parameter for the algorithm (optional).
// Restart the simulation.
simulation.alpha(1).restart();
.alpha(1)
.restart();
}