all files / lib/nodeo/ functions.js

100% Statements 46/46
100% Branches 2/2
100% Functions 4/4
100% Lines 46/46
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                            11× 11× 11× 11× 11× 11× 80×   11× 11× 11× 80×   11× 11× 11×       6365× 6365× 50792× 50792× 50792× 203152×   50792× 50792× 35006×   15786×   50792×       6365×       44× 44× 44× 44× 216× 216× 216× 1168×   216×     44×         11× 11× 80× 80×   11×        
/**
 * Fitness functions for testing
 *
 * @package nodeo
 * @author J. J. Merelo <jjmerelo@gmail.com>
 * @license GPL v3
 */
 
// To avoid uncomprehensible radix complaint at charAt
/*jshint -W065 */
/*jshint smarttabs:true */
 
var functions = exports;
 
//Ackley description in http://tracer.lcc.uma.es/problems/ackley/ackley.html
functions.ackley = function(x) {
    var result = 0;
    var sum = 0;
    console.log("Working with");
    console.log(x);
    console.log("Checking for " + x.length);
    for ( var i in x ) {
	sum += x[i]*x[i];
    }
    result = 20 - 20*Math.exp(-0.2*Math.sqrt(sum/x.length));
    var cos = 0;
    for (  i in x ) {
	cos += Math.cos(2*Math.PI*x[i]);
    } 
    result += Math.E - Math.exp(cos/x.length); // needed for precision
    console.log("Result = " + result.toPrecision(6));
    return result; // hack for returning 0
};
 
// L-trap function
functions.ltrap = function(x,l,a,b,z) {
    var total = 0;
    for ( var i = 0;  i < x.length; i+= l ) {
	var this_substr = x.substr(  i, l );
	var num_ones = 0;
	for ( var j = 0;  j < this_substr.length; j++ ) {
	  num_ones += (this_substr.substring(j,j+1) === "1"); 
	}
	var this_result;
	if ( num_ones <= z ) {
	  this_result = a*(z-num_ones)/z;
	} else {
	  this_result = b*(num_ones -z)/(l-z);
	}
	total += this_result;
//	console.log("Total " + i + " :"+total + " num_ones " + num_ones );
    }
 
    return total;
};
 
//Masive Multimodal Deceptive Problem, a classic test function
functions.MMDP = function(x) {
    var block_size = 6;
    var unitation = [1,0,0.360384,0.640576,0.360384,0,1];
    var total = 0;
    for ( var i = 0;  i < x.length; i+= block_size ) {
	var this_substr = x.substr(  i, block_size );
	var num_ones = 0;
	for ( var j = 0;  j < this_substr.length; j++ ) {
	  num_ones += (this_substr.substring(j,j+1) === "1"); 
	}
	total += unitation[num_ones];
    }
 
    return total;
};
 
//Rastrigin function
functions.Rastrigin = function(x) {
 
   var total = 0;
   for (var i = 0; i < x.length; i++) {
       var value = x[i];
       total = total + (value*value) - (10.0*Math.cos(2*Math.PI*value));
   }
   return total+10*x.length;
 
 
}