Skip to content

Commit

Permalink
Kill some bots with fewest visits
Browse files Browse the repository at this point in the history
  • Loading branch information
denishowe committed Nov 17, 2023
1 parent 1538480 commit 034c0c3
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions _pub/js/automata.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
<script>

const cellSize = 20;
const botsPerCell = 0.5;
const maxCycles = 100;
const botsPerCell = 0.4;

window.onkeydown = function keyDown(e) {
const kc = e.keyCode, ks = String.fromCharCode(kc);
Expand All @@ -48,7 +47,6 @@
Cell.reset();
Bot.reset();
this.periods = [0, 100, 500];
this.botOldCount = -1;
this.start();
}

Expand All @@ -62,14 +60,8 @@
static stop() { this.running = false }

static step() {
if (! this.running) return;
const botCount = Bot.step();
if (botCount !== this.botOldCount) this.t = 0; // Population changed
this.botOldCount = botCount;
if (!botCount || this.t++ > maxCycles) { // Population zero or unchanged for too long
// console.log('Population zero or static');
return this.reset();
}
if (!this.running) return;
if (!Bot.step()) return this.reset();
setTimeout(() => requestAnimationFrame(() => this.step()), this.periods[0]);
}

Expand Down Expand Up @@ -103,22 +95,25 @@

// A rule is just a direction to move

static initialRules() { return mapUpTo(rand(1, 5), () => Direction.random()).join('') }
static initialRules() { return Direction.random() }

// Run one step of bot activity and return number left alive

static step() {
Bot.all.forEach(b => b.move());
// Kill bot with fewest visits
let worstBot;
// Find fewest and most visits
let minVisits = 9E9;
Bot.all.forEach(b => {
const visitCount = b.visited.size;
if (!Bot.best || visitCount > Bot.best.visited.size) Bot.best = b;
if (!worstBot || visitCount < worstBot.visited.size) worstBot = b;
if (visitCount < minVisits) minVisits = visitCount;
});
// Kill some bots with fewest visits
Bot.all = Bot.all.filter(b => {
const dead = b.visited.size === minVisits && p(0.2);
if (dead) b.vacatePos();
return !dead;
});
worstBot.vacatePos();
Bot.all = Bot.all.filter(b => b !== worstBot);

return Bot.all.length;
}

Expand Down

0 comments on commit 034c0c3

Please sign in to comment.