Commit 25d04a5d authored by Pietro Saccardi's avatar Pietro Saccardi

Fix node sampling for the background.

Previous node sampling was a heuristic that may loop infinitely.
Now using proper CDF sampling. Corrected labels, we compute a MST
not a Steiner tree.
parent 79ac62a8
<div id="knWrapper">
<h4 class="d-konami m-2">Multidirectional Dijkstra background by Spak</h4>
<h4 class="d-konami m-2">Multidirectional Spanning Tree by Spak</h4>
<button type="button" class="btn btn-light m-2 d-konami" data-toggle="modal" data-target="#knCustomizeModal">
Customize
</button>
......
......@@ -103,7 +103,7 @@
</div>
</div>
<div class="form-group form-row mb-1">
<label for="knTrunkCol" class="col text-right col-form-label col-form-label-sm">Steiner tree path color</label>
<label for="knTrunkCol" class="col text-right col-form-label col-form-label-sm">Spanning tree path color</label>
<div class="col-5">
<input type="text" class="form-control-sm form-control" id="knTrunkCol" value="#444">
</div>
......
......@@ -4,6 +4,8 @@ import 'bootstrap';
require('typeface-ubuntu');
var bounds = require('binary-search-bounds')
import Konami from 'konami';
const Snap = require( "imports-loader?this=>window,fix=>module.exports=0!snapsvg/dist/snap.svg.js" );
......@@ -72,7 +74,12 @@ function paintTree() {
const dx = FEAS_CENTER[0] - pos[0]
const dy = FEAS_CENTER[1] - pos[1]
const l2 = Math.sqrt(dx * dx + dy * dy)
const unit_l2 = Math.max(Math.min((l2 - FEAS_RAD) / FEAS_FALLOFF, 1.), 0.)
let unit_l2 = null
if (FEAS_FALLOFF > 0) {
unit_l2 = Math.max(Math.min((l2 - FEAS_RAD) / FEAS_FALLOFF, 1.), 0.)
} else {
unit_l2 = l2 >= FEAS_RAD ? 1. : 0.
}
const l2_pow_law = Math.pow(unit_l2, FEAS_FALLOFF_EXP)
const rand_lerp = l2_pow_law * (1. - FEAS_RND_FRAC) + FEAS_RND_FRAC * Math.random()
return rand_lerp
......@@ -134,16 +141,24 @@ function paintTree() {
}
})
let cdf = [0.]
for (let nodeIdx = 0; nodeIdx < grid.numNodes; nodeIdx++) {
cdf.push(cdf[cdf.length - 1] + node_feasibility(nodeIdx)) + 1e-5
}
function get_random_unreachable_node() {
let node = null
while (node == null || algo.labels[node].isReachable() || node_feasibility(node) < 0.5) {
node = Math.floor(Math.random() * grid.numNodes)
while (node == null || algo.labels[node].isReachable()) {
// We should not be getting grid.numNodes, but to be safe we upper bound it.
node = Math.min(grid.numNodes - 1, bounds.le(cdf, Math.random() * cdf[cdf.length - 1]))
}
return node
}
for (let i = 0; i < N_SOURCES; i++) {
algo.addSource(get_random_unreachable_node(), true)
}
const anim = setInterval(function() {
for (let i = 0; i < LBLS_PER_FRAME; ++i) {
const result = algo.propagate(true)
......
......@@ -822,6 +822,12 @@
"integrity": "sha512-DYWGk01lDcxeS/K9IHPGWfT8PsJmbXRtRd2Sx72Tnb8pcYZQFF1oSDb8hJtS1vhp212q1Rzi5dUf9+nq0o9UIg==",
"dev": true
},
"binary-search-bounds": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/binary-search-bounds/-/binary-search-bounds-2.0.4.tgz",
"integrity": "sha512-2hg5kgdKql5ClF2ErBcSx0U5bnl5hgS4v7wMnLFodyR47yMtj2w+UAZB+0CiqyHct2q543i7Bi4/aMIegorCCg==",
"dev": true
},
"block-stream": {
"version": "0.0.9",
"resolved": "https://registry.npmjs.org/block-stream/-/block-stream-0.0.9.tgz",
......
......@@ -31,7 +31,8 @@
"webpack": "^4.25.1",
"webpack-bundle-analyzer": "^3.0.3",
"webpack-cli": "^3.1.2",
"typeface-ubuntu": "^0.0.65"
"typeface-ubuntu": "^0.0.65",
"binary-search-bounds": "^2.0.4"
},
"dependencies": {
"@fortawesome/fontawesome": "^1.1.8",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment