all files / lib/nodeo/Selection/ Tournament.js

94.44% Statements 17/18
75% Branches 3/4
100% Functions 2/2
94.44% Lines 17/18
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                                      26× 26× 26× 26×     26× 6604× 6604× 6604× 6604×   3150×     6604×   26×          
// Selection mechanisms plus common code if needed
/*jslint node: true */
'use strict';
// * @license GPL v3
// * @package nodeo
// * @author J. J. Merelo <jjmerelo@gmail.com>
 
 
// To avoid uncomprehensible radix complaint at charAt
/*jshint -W065 */
/*jshint smarttabs:true */
 
module.exports = Tournament;
 
// Encapsulates parameters
function Tournament (  tournament_size, pool_size ) {
    /*jshint validthis: true */
    this.tournament_size = tournament_size;
    this.pool_size = pool_size;
}
 
// Selects a new population of size pool_size via comparing tournament_size chromosomes and taking the best
Tournament.prototype.select = function ( population ) {
/*jshint validthis: true */
    var pool = [];
    var tournament_size = this.tournament_size;
    var pool_size = this.pool_size;
    Iif ( tournament_size <= 1 ) {
	return new Error ("Tournament size too small");
    }
    do {
	var best =  population.one() ;
	for ( var i = 1; i < tournament_size; i ++) {
	    var another= population.one();
	    if ( population.fitness_of[another]
		 > population.fitness_of[best]) {
		best = another;
	    }
	}
	pool.push( best );
    } while (pool.length < pool_size );
    return pool;
}