Comparing version 0.2.4 to 0.2.5
@@ -20,7 +20,5 @@ "use strict"; | ||
mutation: config.mutation || 0.2, | ||
keepFittest: typeof config.keepFittest !== 'undefined' | ||
? config.keepFittest : true, | ||
keepFittest: typeof config.keepFittest !== 'undefined' ? config.keepFittest : true, | ||
optimizeKey: config.optimizeKey, | ||
optimize: config.optimizeKey === 'Max' | ||
? new utils_1.Optimize().max : new utils_1.Optimize().min, | ||
optimize: config.optimizeKey === 'Max' ? new utils_1.Optimize().max : new utils_1.Optimize().min, | ||
select: config.select, | ||
@@ -35,3 +33,4 @@ pair: config.pair | ||
if (/tournament/g.test(this.config.select)) { | ||
this.config.selectN = Number(this.config.select.replace(/[^0-9]/g, '')) || 2; | ||
this.config.selectN = | ||
Number(this.config.select.replace(/[^0-9]/g, '')) || 2; | ||
this.config.select = this.config.select.replace(/[0-9]/g, ''); | ||
@@ -43,5 +42,3 @@ } | ||
while (this.individuals.length < this.config.size) { | ||
const entity = typeof seed === 'function' | ||
? seed() | ||
: seed; | ||
const entity = typeof seed === 'function' ? seed() : seed; | ||
const config = Object.assign({}, { entity }, this.individualConfig); | ||
@@ -70,3 +67,4 @@ const individual = new individual_1.Individual(config).evolve(); | ||
if (this.config.keepFittest) { | ||
generation.individuals.push(individuals.shift()); | ||
generation.individuals.push(individuals[0]); | ||
generation.individuals.push(individuals[0].evolve()); | ||
} | ||
@@ -88,5 +86,3 @@ const select = new utils_1.Select(this.config.optimize); | ||
let kid = selectFn(select)[0]; | ||
kid = Math.random() <= this.config.mutation | ||
? kid.evolve() | ||
: kid; | ||
kid = Math.random() <= this.config.mutation ? kid.evolve() : kid; | ||
generation.add(kid); | ||
@@ -98,4 +94,3 @@ } | ||
get stats() { | ||
const fitnesses = this.individuals | ||
.map(individual => individual.fitness); | ||
const fitnesses = this.individuals.map(individual => individual.fitness); | ||
const mean = fitnesses.reduce((a, b) => a + b) / fitnesses.length; | ||
@@ -106,3 +101,3 @@ const stdev = this.getStandardDeviation(fitnesses, mean); | ||
if (this.config.optimizeKey === 'Min') { | ||
max = [min, min = max][0]; | ||
max = [min, (min = max)][0]; | ||
} | ||
@@ -112,10 +107,8 @@ return { max, min, mean, stdev }; | ||
getStandardDeviation(items, mean) { | ||
const variance = items | ||
.map(item => (item - mean) * (item - mean)) | ||
.reduce((a, b) => a + b) / items.length; | ||
const variance = items.map(item => (item - mean) * (item - mean)).reduce((a, b) => a + b) / | ||
items.length; | ||
return Math.sqrt(variance); | ||
} | ||
shouldBreed() { | ||
return this.config.crossover > 0 | ||
&& Math.random() <= this.config.crossover; | ||
return this.config.crossover > 0 && Math.random() <= this.config.crossover; | ||
} | ||
@@ -122,0 +115,0 @@ } |
{ | ||
"name": "evjs", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Generic genetic/evolution algorithm in JS", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
36099
484