From d95d5a18fb127e2574170b9ea5955d5c582cc3da Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Thu, 23 Jun 2022 20:43:43 -0400 Subject: [PATCH] feat: build a hierarchical estimates visualizer in a similar spirit --- packages/utility-tools/src/example.js | 12 ++-- packages/utility-tools/src/mergeSort.js | 83 ++++++++++++++----------- packages/webpage-refactor/README.md | 3 + 3 files changed, 58 insertions(+), 40 deletions(-) diff --git a/packages/utility-tools/src/example.js b/packages/utility-tools/src/example.js index 5a8f7cb..8a4251e 100644 --- a/packages/utility-tools/src/example.js +++ b/packages/utility-tools/src/example.js @@ -5,7 +5,7 @@ import { findDistances } from "./findPaths.js"; import { aggregatePaths } from "./aggregatePaths.js"; // DEFS -const inputLinksFilePath = "./input/input-links.json"; +const inputLinksFilePath = "./input/input-links-daniel.json"; const inputListFilePath = "./input/input-list.json"; const outputFilePath = "./output/output.json"; @@ -20,7 +20,11 @@ async function main() { const list = JSON.parse(inputListAsString); // Merge sort - let mergeSortOutput = mergeSort({ list, links }); + let mergeSortOutput = mergeSort({ + list, + links, + tryHardWithPermutations: true, + }); if (mergeSortOutput.finishedOrderingList == false) { console.log("Merge could not proceed"); console.group(); @@ -42,8 +46,8 @@ async function main() { let aggregatedPaths = await aggregatePaths({ pathsArray: paths, orderedList, - aggregationType: "mean", // alternatively: aggregationType: "distribution" - VERBOSE: false, // optional arg + aggregationType: "distribution", // alternatively: aggregationType: "distribution" | "mean" + VERBOSE: true, // optional arg DONT_EXCLUDE_INFINITIES_AND_NANS: false, // optional arg }); console.log(aggregatedPaths); diff --git a/packages/utility-tools/src/mergeSort.js b/packages/utility-tools/src/mergeSort.js index 96cf326..09eaaba 100644 --- a/packages/utility-tools/src/mergeSort.js +++ b/packages/utility-tools/src/mergeSort.js @@ -138,7 +138,7 @@ export function mergeSortInner({ recursiveInput, links }) { } } -export function mergeSort({ list, links }) { +export function mergeSort({ list, links, tryHardWithPermutations }) { // Try normally let answer = mergeSortInner({ recursiveInput: { list, bottleneckedByComparison: false }, @@ -150,48 +150,59 @@ export function mergeSort({ list, links }) { orderedList: answer.recursiveInput.list, }; return result; - } else { + } else if (tryHardWithPermutations != true) { let result = { finishedOrderingList: false, uncomparedElements: answer.recursiveInput.uncomparedElements, }; return result; - } - /* - // otherwise, test all other permutations: - let permutation = list.slice(); - var length = list.length; - // let result = [list.slice()]; - let c = new Array(length).fill(0); - let i = 1; - let k; - let p; - let counter = 0; + } else { + // otherwise, test all other permutations: + let permutation = list.slice(); + var length = list.length; + // let result = [list.slice()]; + let c = new Array(length).fill(0); + let i = 1; + let k; + let p; + let counter = 0; + let errorMsg = + "Error: The original list was wrongly ordered, and trying permutations didn't work"; - while (i < length) { - counter++; - if (counter > 10) console.log(counter); - if (c[i] < i) { - k = i % 2 && c[i]; - p = permutation[i]; - permutation[i] = permutation[k]; - permutation[k] = p; - // ++c[i]; - c[i] = c[i] + 1; - i = 1; - let answer = mergeSortInner({ list: permutation, links }); - if (answer != errorMsg) { - console.log(answer); - return answer; + while (i < length) { + counter++; + if (counter > 10) console.log("permutation #" + counter); + if (c[i] < i) { + k = i % 2 && c[i]; + p = permutation[i]; + permutation[i] = permutation[k]; + permutation[k] = p; + // ++c[i]; + c[i] = c[i] + 1; + i = 1; + let answer = mergeSortInner({ + recursiveInput: { + list: permutation, + bottleneckedByComparison: false, + }, + links, + }); + if (answer.recursiveInput.bottleneckedByComparison == false) { + console.log(answer.recursiveInput); + let result = { + finishedOrderingList: true, + orderedList: answer.recursiveInput.list, + }; + return result; + } + // result.push(permutation.slice()); + } else { + c[i] = 0; + i = i + 1; + // ++i; } - // result.push(permutation.slice()); - } else { - c[i] = 0; - i = i + 1; - // ++i; } + console.log(errorMsg); + return errorMsg; } - console.log("Error"); - return "Error: The original list was wrongly ordered, and trying permutations didn't work"; - */ } diff --git a/packages/webpage-refactor/README.md b/packages/webpage-refactor/README.md index 38db172..4c05caa 100644 --- a/packages/webpage-refactor/README.md +++ b/packages/webpage-refactor/README.md @@ -107,5 +107,8 @@ Distributed under the MIT License. See LICENSE.txt for more information. - [ ] Look back at Amazon thing which has been running - [ ] Simplify Graph and DynamicSquiggleChart components - [ ] Add squiggle component to initial comparison? +- [ ] Understand why the rewrite doesn't + - Maybe: When two elements are judged to be roughly equal + - Maybe: Slightly different merge-sort algorithm. [^1]: The program takes each element as a reference point in turn, and computing the possible distances from that reference point to all other points, and taking the geometric mean of these distances. This produces a number representing the value of each element, such that the ratios between elements represent the user's preferences: a utility function. However, this isn't perfect; the principled approach woud be to aggregate the distributions rather than their means. But this principled approach is much more slowly. For the principled approach, see the `utility-tools` repository.