all files / lib/nodeo/ vector.js

77.78% Statements 28/36
100% Branches 0/0
80% Functions 4/5
77.78% Lines 28/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                                                80× 80×         16× 16× 16× 32×   16× 16×       16× 16× 16× 16× 16× 16× 16× 16× 16× 16×       35× 35× 73×   35×                    
// Chromosome for Evolutionary Algorithms that uses floating point vectors
/*jslint node: true */
'use strict';
// * @license GPL v3
// * @package nodeo
// * @author J. J. Merelo <jjmerelo@gmail.com>
 
/*jshint smarttabs:true */
 
// Applies operators to the pool
function reproduction(  pool ) {
    var offspring = [];
    while (pool.length ) {
	var first = pool.splice( Math.floor(Math.random()*pool.length), 1 );
	var second = pool.splice( Math.floor(Math.random()*pool.length), 1 );
	var crossovers = crossover( first[0], second[0] );
	for ( var i in crossovers ) {
	    offspring.push( mutate(crossovers[i]));
	}
    }
    return offspring;
}
 
// Creates a single chromosome. Includes all utility functions
function Vector(vector,fitness){
    /*jshint validthis: true */
    this.vector = vector;
    this.fitness = fitness;
}
 
// Changes a single element
function mutate (chromosome ) {
 
    var mutation_point = Math.floor( Math.random() * chromosome.vector.length);
    var temp = [];
    for ( var i in chromosome.vector ) {
	temp[i] = chromosome.vector[i];
    }
    temp[mutation_point] = temp[mutation_point]-0.2+Math.random()*0.1;
    return new Vector( temp );
}
 
// Interchanges a substring between the two parents
function crossover ( chrom1, chrom2 ) {
    var length = chrom1.vector.length;
    var xover_point = Math.floor( Math.random() * length);
    var range = 1 + Math.floor(Math.random() * (length - xover_point) );
    var new_chrom1 = chrom1.vector.splice(0,xover_point);
    var new_chrom2 = chrom2.vector.splice(0,xover_point);
    new_chrom1.push( chrom2.vector.splice(xover_point,xover_point+range));
    new_chrom1.push( chrom1.vector.splice(xover_point+range,length) );
    new_chrom2.push( chrom1.vector.splice(xover_point,xover_point+range) );
    new_chrom2.push( chrom2.vector.splice(xover_point+range,length));
    return [new Vector(new_chrom1), new Vector(new_chrom2)];
}
 
// Returns a random vector of `n` components with minimum `min` and range `range`
function random( n, min, range ) {
    var $vector = [];
    for (var i = 0; i < n; i ++ ) {
	$vector.push( min+Math.random()*range );
    }
    return $vector;
}
	
 
module.exports =  {
    version: '0.0.1',
    Vector : Vector,
    mutate: mutate,
    crossover: crossover, 
    random: random,
    reproduction: reproduction };