Public
Edited
Sep 28, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
__killSwitch = 120
Insert cell
maxStorage = 0 // Only x amount of generations will be kept. Leave as 0 to keep all
Insert cell
finalGeneration = 100
Insert cell
mutable generations = []
Insert cell
bestGeneration = generations[0]
Insert cell
Insert cell
Insert cell
mutable currentGenerationIndex = 0
Insert cell
Insert cell
mutable stopped = false
Insert cell
function start(numberOfGenerations) {
// i will always start from zero
for (let j = 0; j < __killSwitch; ++j) {
// Only run if plug has not been pulled
if (mutable stopped) {
break;
}

// This is where the generation will be bred
const i = mutable currentGenerationIndex;
const value = breed(i);
const fitness = evaluate(i, value);

// Keep generation data for later
const k = 0; // FIXME: must new position in sorted array
// let k = findBy(mutable generations, ({ f }) => { f < fitness })
if (k < 0) {
k = mutable generations.length;
}

if (!maxStorage || k < maxStorage - 1) {
mutable generations.splice(k, 0, {
i,
value,
fitness
});

// Only keep the most valuable generations
if (maxStorage && mutable generations.length > maxStorage) {
mutable generations = mutable generations.slice(0, maxStorage);
}
}

// See if we still need to continue
if (isDone() || i >= finalGeneration) {
break;
}

// Loop will continue
mutable currentGenerationIndex++;
}

return mutable currentGenerationIndex;
}
Insert cell
function stop () {
mutable stopped = true
}
Insert cell
Insert cell
function reset () {
stop()

mutable generations = []
mutable currentGenerationIndex = 0
}
Insert cell
Insert cell
Insert cell
function evaluate (generationIndex) {}
Insert cell
Insert cell
Insert cell
function isDone () {
return false
}
Insert cell
Insert cell
function select (i, previousGenerations) {
return previousGenerations
}
Insert cell
Insert cell
function breed (i, selected) {
return selected.length ? selected[selected.length] : null
}
Insert cell
function mutate (i, generation) {
return generation
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more