Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 1 file changed
  • 0 comments
  • 1 contributor

Showing 1 changed file with 40 additions and 4 deletions. Show diff stats Hide diff stats

  1. +40 4 lib/genetic/Task.js
44 lib/genetic/Task.js
@@ -29,7 +29,7 @@ Task.prototype.run = function (callback) {
29 29 }
30 30 else {
31 31 if (typeof(callback)=="function") {
32   - callback(self.statistics)
  32 + callback(self.bestSolution, self.statistics)
33 33 }
34 34 self.emit('run finished', self.statistics)
35 35 }
@@ -172,6 +172,32 @@ Task.prototype.reproduction = function (callback) {
172 172 )
173 173 }
174 174 , function (callback) {
  175 + // record best value, best solution, and count stableGeneration
  176 + var localBestSolution = self.minimize ?
  177 + self.statistics.min : self.statistics.max
  178 + var localBestScore = localBestSolution.score
  179 +
  180 + var setBestRecord = function() {
  181 + self.bestScore = localBestScore
  182 + self.bestSolution = localBestSolution
  183 + self.stableGeneration = 0
  184 + console.log(localBestScore)
  185 + }
  186 +
  187 + if (self.bestScore === undefined) {
  188 + // initialize best record
  189 + setBestRecord()
  190 + } else if (self.minimize ?
  191 + localBestScore < self.bestScore :
  192 + localBestScore > self.bestScore) {
  193 + setBestRecord()
  194 + } else {
  195 + self.stableGeneration++
  196 + }
  197 +
  198 + callback()
  199 + }
  200 + , function (callback) {
175 201 // invert scores, if we minimize fitness
176 202 var avg = self.statistics.avg
177 203 if (self.minimize) {
@@ -189,7 +215,6 @@ Task.prototype.reproduction = function (callback) {
189 215 }
190 216 callback();
191 217 }
192   -
193 218 , function (callback) {
194 219 self.emit('child forming start')
195 220 // transfer parent to children accordingly to normalized fitness
@@ -207,14 +232,25 @@ Task.prototype.reproduction = function (callback) {
207 232 position++
208 233 level+=self.parents[position].score
209 234 }
210   - child = JSON.parse(JSON.stringify(self.parents[position]))
211 235
212 236 parent = self.parents[position]
213 237 secondParentPosition = position
214 238 secondParent = self.parents[secondParentPosition]
215 239 if (Math.random() < self.crossoverProbability) {
216 240 self.emit('crossover')
217   - secondParentPosition = Math.floor(Math.random()*self.parents.length)
  241 +
  242 + if (Math.random() < .5) {
  243 + secondParentPosition = Math.floor(Math.random()*self.parents.length)
  244 + } else {
  245 + point = Math.random()*sum
  246 + secondParentPosition = 0
  247 + level = self.parents[secondParentPosition].score
  248 + while (point>level) {
  249 + secondParentPosition++
  250 + level+=self.parents[secondParentPosition].score
  251 + }
  252 + }
  253 +
218 254 secondParent = self.parents[secondParentPosition]
219 255 }
220 256 self.crossover(self.parents[position], secondParent, function (child) {

No commit comments for this range

Something went wrong with that request. Please try again.