Created
September 5, 2016 08:37
-
-
Save MatthiasKrijgsman/7ad5ad09cbad5ea7146c84fc9fde7386 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DNA; | |
DNA = function(min, max, length){ | |
// Gene's min and max value | |
this.minValue = parseFloat(min); | |
this.maxValue = parseFloat(max); | |
// Gene's array length | |
this.dnaLength = length | |
// Generate new random gene | |
this.gene = []; | |
for(var x = 0; x < length; x++){ | |
this.gene.push(this.newRandomGene()); | |
} | |
// Defining Fitness | |
this.fitness = 0; | |
}; | |
// Creates new random gene | |
DNA.prototype.newRandomGene = function(){ | |
return Math.random() * (this.maxValue - this.minValue) + this.minValue; | |
}; | |
// Performs mutation based on mutation rate | |
DNA.prototype.mutate = function(rate){ | |
for(var x = 0; x < this.gene.length; x++){ | |
if(Math.random() < rate){ | |
this.gene[x] = this.newRandomGene(); | |
} | |
} | |
}; | |
DNA.prototype.partner = function(partner, mutationRate){ | |
// Picks crossover point | |
var point = Math.floor(Math.random() * this.gene.length); | |
// Create new child | |
var g = this.gene.slice(0, point); | |
g = g.concat(partner.gene.slice(point, partner.gene.length)); | |
var child = new DNA(this.minValue, this.maxValue, this.length); | |
child.gene = g; | |
child.mutate(mutationRate); | |
return child; | |
}; | |
DNA.prototype.getFitness = function(){ | |
/* Build custom fitness function here */ | |
// Don't forget to Reset the Fitness | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment