Skip to content

Instantly share code, notes, and snippets.

@kmaher9
Created September 9, 2018 01:49
Show Gist options
  • Save kmaher9/5f509011e7327ebbc04b2c1cb4ab80e2 to your computer and use it in GitHub Desktop.
Save kmaher9/5f509011e7327ebbc04b2c1cb4ab80e2 to your computer and use it in GitHub Desktop.
var population
var lifespan = 200
var visualLifespan
var count = 0
var target
function setup() {
createCanvas(400, 300)
population = new Population()
visualLifespan = createP()
target = createVector(width/2, 50)
}
function draw() {
background(42, 49, 57)
population.run()
visualLifespan.html(count)
count++
if (count == lifespan) {
population.evaluate()
population.selection()
count = 0
}
ellipse(target.x, target.y, 16, 16)
}
// dna.js
function dna(genes) {
if (genes) {
this.genes = genes
} else {
this.genes = []
for (var i = 0; i < lifespan; i++) {
this.genes[i] = p5.Vector.random2D()
this.genes[i].setMag(0.2)
}
}
this.crossover = function(partner) {
var newgenes = [];
var mid = floor(random(this.genes.length));
for (var i = 0; i < this.genes.length; i++) {
if (i > mid) {
newgenes[i] = this.genes[i];
}
else {
newgenes[i] = partner.genes[i];
}
}
return new dna(newgenes);
}
}
// population.js
function Population() {
this.sticks = []
this.populationSize = 25
this.pool = []
for(var i = 0; i < this.populationSize; i++) {
this.sticks[i] = new Stick()
}
this.evaluate = function() {
var maxFitness = 0
for(var i = 0; i < this.populationSize; i++) {
this.sticks[i].calculateFitness()
if (this.sticks[i].fitness > maxFitness) {
maxFitness = this.sticks[i].fitness
}
}
for(var i = 0; i < this.populationSize; i++) {
this.sticks[i].fitness /= maxFitness // normalize
}
this.pool = []
for(var i = 0; i < this.populationSize; i++) {
var n = this.sticks[i].fitness * 100
for (var j = 0; j < n; j++) {
this.pool.push(this.sticks[i])
}
}
}
this.selection = function() {
var newSticks = [];
for (var i = 0; i < this.sticks.length; i++) {
var parentA = random(this.pool).dna;
var parentB = random(this.pool).dna;
var child = parentA.crossover(parentB);
//child.mutation();
newSticks[i] = new Stick(child);
}
this.sticks = newSticks;
}
this.run = function() {
for(var i = 0; i < this.populationSize; i++) {
this.sticks[i].update()
this.sticks[i].show()
}
}
}
// stick.js
function Stick(dnas) {
if (dnas) {
this.dna = dnas
} else {
this.dna = new dna()
}
this.pos = createVector(width/2, height)
this.vel = createVector()
this.acc = createVector()
this.fitness = 0
this.applyForce = function(force) {
this.acc.add(force)
}
this.calculateFitness = function() {
var distance = dist(this.pos.x, this.pos.y, target.x, target.y)
this.fitness = map(distance, 0, width, width, 0)
}
this.update = function() {
this.applyForce(this.dna.genes[count])
this.vel.add(this.acc)
this.pos.add(this.vel)
this.acc.mult(0)
}
this.show = function() {
push()
noStroke()
fill(255, 150)
translate(this.pos.x, this.pos.y)
rotate(this.vel.heading())
rectMode(CENTER)
rect(0, 0, 25, 5)
pop()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment