Skip to content

Instantly share code, notes, and snippets.

@kmaher9
Created September 9, 2018 00:43
Show Gist options
  • Select an option

  • Save kmaher9/853edaa221401c537f6acabab6ab01b6 to your computer and use it in GitHub Desktop.

Select an option

Save kmaher9/853edaa221401c537f6acabab6ab01b6 to your computer and use it in GitHub Desktop.
this.evaluate = function() {
var maxFitness = 0
for(var i = 0; i < this.populationSize; i++) {
this.sticks[i].calculateFitness() // the distance from the target relative to current pos
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 the fitness value to be between 0 and 1
}
this.pool = []
for(var i = 0; i < this.populationSize; i++) {
var n = this.sticks[i].fitness * 100 // increase normalized value to between 0 and 100
for (var j = 0; j < n; j++) {
this.pool.add(this.sticks[i]) // add the current gene to the pool relative to how fit it is
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment