Published
Edited
Apr 19, 2021
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function simulate() {
const initial = Array.from({ length: N }, () => new Agent())
const gen = [initial];

for (let i = 0; i < r-1; ++i) {
let pop = gen[gen.length - 1]

let offspring = [];

pop.forEach((parent1) => {
// search for possible mate
let mates = pop.filter((candidate) => candidate !== parent1
&& parent1.inRadius(candidate)
)//&& parent1.compatible(candidate))
// if no nearby mates, do nothing
if (!mates.length) {
return;
}

mates.sort((a, b) => a.fitness - b.fitness)
let parent2 = mates.pop()

offspring.push(...parent1.createChildren(parent2));
})

let capacity = N;

let niches = [[], []]

offspring.forEach((o) => {
if (o.x > scaleX / 2) {
niches[1].push(o);
} else {
niches[0].push(o);
}
})

niches = niches.map(niche => niche.sort((a, b) => b.fitness - a.fitness)
.slice(0, N))

offspring = [...niches[0], ...niches[1]]
//offspring = offspring
// .sort((a, b) => b.fitness - a.fitness)
//.slice(0, N);
gen.push(offspring)
}
return gen
}
Insert cell
class Agent {
constructor(parent1, parent2) {
if (parent1 && parent2) {
this.initFromParents(parent1, parent2);
} else {
this.initWithoutParents();
}
}

initWithoutParents() {
this.x = randomUniform(0, scaleX / 2)();

this.gene = new Gene();
}

initFromParents(parent1, parent2) {
this.x = parent1.x + randomNormal(0, speed)()

if (this.x < 0) {
this.x = -this.x
} else if (this.x >= scaleX) {
this.x -= this.x - scaleX
}
this.gene = parent1.gene.recombinate(parent2.gene);
}

get fitness() {
const currentElevation = elevation(this.x)

let fractionOnes = this.gene.fractionOnes;
let fractionZeros = 1 - fractionOnes;
return currentElevation > 0 ? 1.0 + fractionOnes : 0.7 + fractionZeros;
}

inRadius(other) {
return Math.abs(this.x - other.x) < maxRadius;
}

compatible(other) {
return this.gene.compatible(other.gene)
}

createChildren(parent2) {
// Fitness is the expected value on the number of offspring
// If fitness is more than zero, create a second child with probability fitness - 1
// If fitness is less than zero, create a first child with probability fitness
const children = [];

let prob = this.fitness;

while (prob > 0) {
if (random() < prob) {
children.push(new Agent(this, parent2));
}
prob -= 1;
}

return children;
}
render() {
let fill = d3.interpolateSpectral(this.gene.fractionOnes)
let stroke = d3.color(fill).darker()
return `<g><circle cx=${this.x} cy=${y(this.x)} r=20 fill="${fill}" stroke="${stroke}"/><text x=${this.x} y=${y(this.x)}>${this.gene.numOnes}</text></g>`
}
}
Insert cell
Insert cell
//elevationMask = BitSet("11111")
Insert cell
Insert cell
class Gene {
constructor(bits) {
if (bits) {
this.bits = new BitSet(bits);
} else {
this.bits = new BitSet(); // all zeros
}
}

recombinate(other) {
const window = 3;
const newBitChunks = []
for (let i = GENE_SIZE; i >= 0; i -= window) {
// Pick random gene with equal probability for each slice of window size
const bits = random() < 0.5 ? this.bits : other.bits;
const newBits = bits.slice(Math.max(0, i - window), i - 1);
newBitChunks.push(newBits.toString().padStart(3, '0'));
}

const newBits = new BitSet(newBitChunks.join(""));
const mutate = random() < mutationProbability;

if (mutate) {
let j = Math.floor(random() * GENE_SIZE);
newBits.flip(j);
}
return new Gene(newBits)
}

get numOnes() {
return this.bits.cardinality();
}

get fractionOnes() {
return this.numOnes / GENE_SIZE;
}

_similarity(other) {
return (GENE_SIZE - this.gene.xor(other.gene).cardinality()) / GENE_SIZE;
}
compatible(other) {
let distance = this._similarity(other)

return distance > 0.1 && distance < 0.95;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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