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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more