all files / lib/nodeo/ HIFF.js

96.55% Statements 28/29
89.66% Branches 26/29
100% Functions 5/5
96.55% Lines 28/29
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 75 76 77 78 79 80 81 82 83 84 85 86                              425× 172× 46× 126× 20×     253×         957×       177×   76×   89×     190×   425×     425×       107× 15×   92×                     224×     32×     40×     45×   107×            
 
// Trap function with Fitness function signature
//
// Trap is a deceptive function that is used, concatenated, to test
// evolutionary algorithms. 
// @license GPL v3
// @package nodeo
// @author J. J. Merelo <jjmerelo@gmail.com>
 
// To avoid uncomprehensible radix complaint at charAt
/*jshint -W065 */
/*jshint smarttabs:true */
 
 
// basic transform if and only iff
function t( a, b ) {
    if ( a == b ) {
	if(  a == '0' ) {
	    return '0';
	} else if (  a == '1' ) {
	    return '1';
	}
    } else {
	return '-';
    }
}
 
function T( ev ) {
//  console.log(ev);
  switch(ev) {
    case '-':
    case '1':
    case '0':
    return ev;
    case '00':
    return '0';
    case '11':
    return '1';
    case '01':
    case '10':
    return '-';
    default:
    Iif (ev.length == 2 && ev.match(/-/))
      return '-'
    else 
      return t(T(ev.slice(0,ev.length/2)),T(ev.slice(ev.length/2,ev.length)))
  }
}
 
function f( ev ) {
    if ( ev == '0' || ev == '1' )
	return 1;
    else
	return 0
}
 
// Class definition
// ```
// var this_HIFF = new HIFF.HIFF
// ```
//
function HIFF( ) {
    this.apply = apply;
}
 
// Applies trap function to chromosome using instance values
function apply( chromosome ){
    switch( chromosome ) {
    case '0':
    case '1':
	return 1;
    case '00':
    case '11':
	return 4;
    case '01':
    case '10':
	return 2;
    default:
	return chromosome.length*f(T(chromosome))
	    + apply( chromosome.slice(0,chromosome.length/2) )
	    + apply( chromosome.slice(chromosome.length/2,chromosome.length) )
    }
}
 
exports.HIFF = HIFF;