xxxxxxxxxx
107
// class Particle {
// constructor() {
// this.position = createVector(width / 2, height / 2); // start at center
// this.velocity = createVector();
// this.life = 100; // initial life value
// this.genes = []; // array to store movement instructions
// this.history = []; // to store the path
// // generate initial movement instructions
// for (let i = 0; i < this.life; i++) {
// this.genes[i] = p5.Vector.random2D();
// }
// }
// move() {
// if (this.life > 0) {
// // apply movement based on genes and life
// this.velocity.add(this.genes[this.life-1]);
// this.position.add(this.velocity);
// this.life--;
// // store the history
// this.history.push(this.position.copy());
// }
// }
// show() {
// // display the particle
// fill(255 * this.life/100);
// noStroke();
// ellipse(this.position.x, this.position.y, 5, 5);
// }
// evaluate() {
// // calculate the residuals and sum of squared residuals
// let sumOfSquaredResiduals = 0;
// for (let i = 1; i < this.history.length; i++) {
// let expectedPosition = p5.Vector.add(this.history[0], p5.Vector.mult(this.velocity, i));
// let residual = p5.Vector.sub(expectedPosition, this.history[i]);
// sumOfSquaredResiduals += residual.magSq();
// }
// // calculate the 'niceness' based on sum of squared residuals
// let niceness = 1 / (1 + sumOfSquaredResiduals); // we add 1 to the denominator to prevent division by zero
// this.life += niceness; // add to life based on niceness
// }
// breed(partner) {
// let offspring = new Particle();
// // combine this particle's genes with partner's genes
// for (let i = 0; i < this.genes.length; i++) {
// offspring.genes[i] = random() < 0.5 ? this.genes[i] : partner.genes[i];
// // introduce a small chance for mutation
// if (random() < 0.01) {
// offspring.genes[i] = p5.Vector.random2D();
// }
// }
// return offspring;
// }
// }
// let particles = [];
// function setup() {
// createCanvas(700, 400);
// background(0)
// // spawn initial particles
// for (let i = 0; i < 100; i++) {
// particles[i] = new Particle();
// }
// }
// function draw() {
// background(0,0,0,0.1)
// // move and show particles
// for (let particle of particles) {
// particle.move();
// particle.show();
// particle.evaluate();
// }
// // remove dead particles and replace with new ones
// particles = particles.filter(particle => particle.life > 0);
// // every certain number of frames, perform selection and breeding
// if (frameCount % 200 == 0) {
// // sort particles by life (niceness)
// particles.sort((a, b) => b.life - a.life);
// // take the top half of particles and breed them
// let newParticles = [];
// while (newParticles.length < 100) {
// let partnerIndex = floor(random(particles.length / 3)); // choose a random partner from the top half
// let partnerIndex2 = floor(random(particles.length / 3));
// while (partnerIndex2 == partnerIndex){
// partnerIndex2 = floor(random(particles.length / 2));
// }
// let offspring1 = particles[partnerIndex].breed(particles[partnerIndex2]);
// newParticles.push(offspring1); // push both offspring into the new list
// // particles.push(new Particle());
// }
// // replace old particles with new particles
// particles = newParticles;
// }
// }