binary-breeder
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -27,2 +27,6 @@ 'use strict'; | ||
function validateParentChromosomes(parentChromosomes) { | ||
if(!Array.isArray(parentChromosomes)) { | ||
throw new Error('Parent chromosomes must be provided in an array.'); | ||
} | ||
if(parentChromosomes.length !== 2) { | ||
@@ -59,7 +63,3 @@ throw new Error('Exactly two parent chromosomes should be specified.'); | ||
if(Random.real(0, 0.999999)(Random.engines.nativeMath) < mutationChance) { | ||
if(chromosome[i] === '0') { | ||
mutatedChromosome += '1'; | ||
} else { | ||
mutatedChromosome += '0'; | ||
} | ||
mutatedChromosome += chromosome[i] === '0' ? '1' : '0'; | ||
} else { | ||
@@ -66,0 +66,0 @@ mutatedChromosome += chromosome[i]; |
@@ -5,4 +5,5 @@ { | ||
"license": "MIT", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"main": "lib/binary-breeder.js", | ||
"description": "Breeds the specified number of offspring from two parent strings of binary digits with the possibility of random mutation.", | ||
"devDependencies": { | ||
@@ -9,0 +10,0 @@ "chai": "^3.2.0", |
# binary-breeder | ||
Breeds two strings of binary digits and breeds the requested number of offspring. | ||
Breeds the specified number of offspring from two parent strings of binary digits with the possibility of random mutation. | ||
@@ -40,2 +40,12 @@ [](https://www.npmjs.com/package/binary-breeder) [](https://coveralls.io/github/jhaugh42/binary-breeder?branch=master) [](https://travis-ci.org/jhaugh42/binary-breeder) | ||
var offspring = reproduce(parentChromosomes, numOffspring, mutationChance); | ||
/* | ||
sample output | ||
[ '000010100110110100', | ||
'110011001101100101', | ||
'110010100111100101', | ||
'000011001101110100', | ||
'110011001101100101' ] | ||
*/ | ||
``` |
@@ -22,2 +22,11 @@ 'use strict'; | ||
var BAD_INPUT_NON_ARRAYS = [ | ||
{name: 'integer', data: 12345}, | ||
{name: 'string of two zeros', data: '00'}, | ||
{name: 'string of two ones', data: '11'}, | ||
{name: 'decimal', data: 0.1}, | ||
{name: 'function', data: function(){}}, | ||
{name: 'object literal', data: {}} | ||
]; | ||
BAD_CHROMOSOMES_NOT_TWO_ELEMENTS.forEach(function(inputChromosomes) { | ||
@@ -60,2 +69,12 @@ it('should throw an Error when the length of the parent chromosome array is ' + inputChromosomes.length, function() { | ||
}); | ||
BAD_INPUT_NON_ARRAYS.forEach(function(badInput) { | ||
it('should throw an Error data type of the input chromosome is ' + badInput.name, function() { | ||
function doTest() { | ||
breed(badInput.data, 2, 0.0001); | ||
} | ||
expect(doTest).to.throw(Error); | ||
}); | ||
}); | ||
}); |
16283
287
50