Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

record best solution and number of stable generations

  • Loading branch information...
commit b57f98b2c4f6449d46ef9689f90c620611221068 1 parent 8bd290c
@scottcheng authored
Showing with 27 additions and 2 deletions.
  1. +27 −2 lib/genetic/Task.js
View
29 lib/genetic/Task.js
@@ -29,7 +29,7 @@ Task.prototype.run = function (callback) {
}
else {
if (typeof(callback)=="function") {
- callback(self.statistics)
+ callback(self.bestSolution, self.statistics)
}
self.emit('run finished', self.statistics)
}
@@ -172,6 +172,32 @@ Task.prototype.reproduction = function (callback) {
)
}
, function (callback) {
+ // record best value, best solution, and count stableGeneration
+ var localBestSolution = self.minimize ?
+ self.statistics.min : self.statistics.max
+ var localBestScore = localBestSolution.score
+
+ var setBestRecord = function() {
+ self.bestScore = localBestScore
+ self.bestSolution = localBestSolution
+ self.stableGeneration = 0
+ console.log(localBestScore)
+ }
+
+ if (self.bestScore === undefined) {
+ // initialize best record
+ setBestRecord()
+ } else if (self.minimize ?
+ localBestScore < self.bestScore :
+ localBestScore > self.bestScore) {
+ setBestRecord()
+ } else {
+ self.stableGeneration++
+ }
+
+ callback()
+ }
+ , function (callback) {
// invert scores, if we minimize fitness
var avg = self.statistics.avg
if (self.minimize) {
@@ -189,7 +215,6 @@ Task.prototype.reproduction = function (callback) {
}
callback();
}
-
, function (callback) {
self.emit('child forming start')
// transfer parent to children accordingly to normalized fitness
Please sign in to comment.
Something went wrong with that request. Please try again.