From 76ad70352e0ca8ead14d4f3d0e16576c83ecf7c1 Mon Sep 17 00:00:00 2001 From: localhorst Date: Sat, 20 Dec 2025 21:57:23 +0100 Subject: [PATCH] shuffle and stop logs --- index.html | 2 +- js/app.js | 5 ++++- js/matching-worker.js | 26 ++++++++++++++++++-------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index 80cf85c..6d47b54 100644 --- a/index.html +++ b/index.html @@ -167,7 +167,7 @@
- diff --git a/js/app.js b/js/app.js index d31a41e..665106a 100644 --- a/js/app.js +++ b/js/app.js @@ -302,7 +302,10 @@ function startMatching() { } function stopMatching() { - if (AppState.worker) AppState.worker.postMessage({ type: 'stop' }); + console.log("stopMatching") + if (AppState.worker) { + AppState.worker.postMessage({ type: 'stop' }); + } } function updateProgress(progress) { diff --git a/js/matching-worker.js b/js/matching-worker.js index f01605b..6db38ad 100644 --- a/js/matching-worker.js +++ b/js/matching-worker.js @@ -38,6 +38,20 @@ function cloneConfiguration(arr) { return arr.map(group => [...group]); } +/** + * Shuffle cells based on random + */ +function shuffleCells(cells) { + const shuffled = [...cells]; + + for (let i = shuffled.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; + } + + return shuffled; +} + // ============================================================================= // Scoring Functions // ============================================================================= @@ -128,7 +142,7 @@ class StatsTracker { class ExhaustiveSearch { constructor(cells, serial, parallel, options = {}) { - this.cells = cells; + this.cells = shuffleCells(cells); this.serial = serial; this.parallel = parallel; this.totalCellsNeeded = serial * parallel; @@ -144,6 +158,7 @@ class ExhaustiveSearch { } stop() { + console.log("ExhaustiveSearch: stop requested") this.stopped = true; } @@ -232,7 +247,7 @@ class ExhaustiveSearch { iteration++; this.stats.recordIteration(); - if (iteration % 500 === 0) { + if (iteration % (this.maxIterations * 0.01) === 0) { const stats = this.stats.getStats(iteration, Math.min(totalCombinations, this.maxIterations)); self.postMessage({ @@ -301,12 +316,6 @@ self.onmessage = function (e) { const { cells, serial, parallel, algorithm, options } = data; switch (algorithm) { - case 'genetic': - currentAlgorithm = new GeneticAlgorithm(cells, serial, parallel, options); - break; - case 'simulated-annealing': - currentAlgorithm = new SimulatedAnnealing(cells, serial, parallel, options); - break; case 'exhaustive': currentAlgorithm = new ExhaustiveSearch(cells, serial, parallel, options); break; @@ -323,6 +332,7 @@ self.onmessage = function (e) { break; case 'stop': + console.log("Algo: Stop requested") if (currentAlgorithm) { currentAlgorithm.stop(); }