shuffle and stop logs

This commit is contained in:
2025-12-20 21:57:23 +01:00
parent 45cf964313
commit 76ad70352e
3 changed files with 23 additions and 10 deletions

View File

@ -167,7 +167,7 @@
<div class="live-preview-content" id="live-preview-content"></div>
</div>
</div>
<button type="button" id="btn-stop-matching" class="btn btn-secondary">
<button type="button" id="btn-stop-matching" class="btn btn-danger">
Stop Matching
</button>
</section>

View File

@ -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) {

View File

@ -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();
}