{
let population = d3.range(populationSize).map(randomPhrase);
let scores = population.map(fitness);
let generations = 1;
let bestPhrase = "";
while (bestPhrase !== target) {
const normalised = scores.map(d3.scaleLinear().domain([0, target.length]).range([0, 1]));
const matingPool = normalised.flatMap((d, i) => d3.range(Math.round(d * 100)).fill(population[i]));
const parent = () => matingPool[d3.randomInt(matingPool.length)()];
population = population.map(d => crossOver(parent(), parent())).map(mutate);
scores = population.map(fitness);
bestPhrase = population[d3.maxIndex(scores)];
yield html`<p>
Best evolved phrase:
<h2>${ bestPhrase }</h2>
after ${ ++generations } generations of evolution.
</p>`;
}
}