all files / lib/nodeo/ chromosome-float.js

88.89% Statements 24/27
90% Branches 9/10
66.67% Functions 4/6
92.31% Lines 24/26
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                      2713× 2713× 2713× 2713× 2713×                             1348× 1348× 1348×         666× 666× 666× 666×     666×     666×         95× 95× 666× 666× 666× 666× 1332×     95×          
// Chromosome as a vector of float for Evolutionary Algorithms
'use strict';
// * @license GPL v3
// * @author Víctor Rivas Santos (vrivas@ujaen.es)
// *         from chromosome.js, authored by J.J Merelo.
 
 
// Creating namespace
var ChromosomeFloat={
    // Creates a single chromosome. Includes all utility functions
    Chromosome: function (vector,fitness,minvalue,maxvalue){
 
     Iif( minvalue>maxvalue) throw new RangeError ("Function ChromosomeFloat: Minvalue is bigger than maxvalue", "chromosome-float.js");
     this.vector = ( typeof vector!=="undefined" )?vector.slice():[]; // Copying the values, not the reference to the vector.
     this.fitness = fitness || 0.0;
     this.minvalue = minvalue || 0.0;
     this.maxvalue = maxvalue || 1.0;
 
    }
 
 
    // Flips the whole chromosome: maps [minvalue,maxvalue] into [maxvalue, minvalue]
    , invert: function (chrom) {
     return  new ChromosomeFloat.Chromosome(
         chrom.vector.map( function(e) {
             return (-e+chrom.minvalue+chrom.maxvalue);
	 }));
    }
 
    // Changes the value at one point by some other randomly calculated
    , mutate: function (chrom) {
        var toRet=new ChromosomeFloat.Chromosome( chrom.vector );
        toRet.vector[Math.floor(Math.random()*toRet.vector.length)]=toRet.minvalue+Math.random()*(toRet.maxvalue-toRet.minvalue);
        return  toRet;
    }
 
    // Interchanges a substring between the two parents
    , crossover: function ( 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.slice(0,xover_point)
                       .concat(chrom2.vector.slice(xover_point,xover_point+range))
                       .concat(chrom1.vector.slice(xover_point+range));
        var new_chrom2 = chrom2.vector.slice(0,xover_point)
                        .concat(chrom1.vector.slice(xover_point,xover_point+range))
                        .concat(chrom2.vector.slice(xover_point+range));
        return [new ChromosomeFloat.Chromosome(new_chrom1), new ChromosomeFloat.Chromosome(new_chrom2)];
    }
 
    // Applies operators to the pool
    , reproduction: function (  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 = ChromosomeFloat.crossover( first[0], second[0] );
	       for ( var i in crossovers ) {
	           offspring.push( ChromosomeFloat.mutate(crossovers[i]));
	       }
        }
        return offspring;
    }
 
} // namespace
 
module.exports = ChromosomeFloat;