Permalink
Browse files

added chance to choose second parent based on fitness

  • Loading branch information...
1 parent b57f98b commit f7830e3aa229881910c9a4cd26ebc9c2a0335e77 @scottcheng committed Feb 17, 2013
Showing with 13 additions and 2 deletions.
  1. +13 −2 lib/genetic/Task.js
View
@@ -232,14 +232,25 @@ Task.prototype.reproduction = function (callback) {
position++
level+=self.parents[position].score
}
- child = JSON.parse(JSON.stringify(self.parents[position]))
parent = self.parents[position]
secondParentPosition = position
secondParent = self.parents[secondParentPosition]
if (Math.random() < self.crossoverProbability) {
self.emit('crossover')
- secondParentPosition = Math.floor(Math.random()*self.parents.length)
+
+ if (Math.random() < .5) {
+ secondParentPosition = Math.floor(Math.random()*self.parents.length)
+ } else {
+ point = Math.random()*sum
+ secondParentPosition = 0
+ level = self.parents[secondParentPosition].score
+ while (point>level) {
+ secondParentPosition++
+ level+=self.parents[secondParentPosition].score
+ }
+ }
+
secondParent = self.parents[secondParentPosition]
}
self.crossover(self.parents[position], secondParent, function (child) {

0 comments on commit f7830e3

Please sign in to comment.