Created
June 27, 2019 17:19
-
-
Save jadbox/336900cbd91e0f77055bd4ba33abd336 to your computer and use it in GitHub Desktop.
Clusters of diversity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require("http"); | |
var kmpp = require("kmpp"); | |
// const skmeans = require("skmeans"); | |
var pnts_old_test_case = [ | |
[0, 1, 0], | |
[0, 1, 0], | |
[1, 0, 0], | |
[1, 0, 1], | |
[0, 0, 1], | |
[0, 0, 0], | |
[0, 0, 1] | |
]; | |
var pnts = [ | |
[0, 1, 0], | |
[0, 1, 0], | |
[0, 0, 0], | |
[0, 0, 0], | |
[0, 0, 0], | |
[0, 0, 0], | |
[0, 0, 0] | |
]; | |
// K clusters | |
var k = 3; | |
// Run k-means | |
var r = kmpp(pnts, { | |
k: k | |
}); | |
// Transform kmean idxs into real grouped points | |
function groupify(pnts, assignments, k) { | |
const groups = Array(k); | |
for (var i = 0; i < pnts.length; i++) { | |
var pnt = pnts[i]; | |
var g = assignments[i]; // group assigned | |
if (!groups[g]) groups[g] = []; | |
groups[g] = groups[g].concat([pnt]); | |
} | |
console.log("groups", groups); | |
return groups; | |
} | |
// takes in pnts and | |
function diversify(groups, len, k, tables) { | |
// const numAssigned = assignments.length; | |
const dgroups = Array(tables); | |
var l = len; | |
var rb = 0; | |
var rb2 = 0; | |
while (l > 0) { | |
// console.log(rb, groups.length); | |
var pnt = groups[rb].pop(); | |
if (!pnt) { | |
rb = ++rb % k; | |
continue; | |
} | |
if (!dgroups[rb2]) dgroups[rb2] = []; | |
dgroups[rb2] = dgroups[rb2].concat([pnt]); | |
if (dgroups[rb2].length % 2 === 0) rb2 = ++rb2 % tables; | |
--l; | |
rb = ++rb % k; | |
} | |
return dgroups; | |
} | |
console.log("k", k); | |
// Get the count of non-empty kmean clusters | |
var counts = r.counts.filter(x => x > 0).reduce((a, b) => ++a, 0); | |
console.log("counts", counts); | |
// Group the actual points of the kmeans pass | |
var groups = groupify(pnts, r.assignments, counts); | |
console.log("k-means clusters"); | |
console.table(groups); | |
const tables = 3; | |
// Diversify them into new groups! | |
var r2 = diversify(groups, r.assignments.length, counts, tables); | |
console.log("-diversified table-"); | |
console.table(r2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment