Skip to content

Instantly share code, notes, and snippets.

@prabod
Created December 3, 2016 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prabod/73e3a8b0c1dbbadfc9d2b9edb34c3670 to your computer and use it in GitHub Desktop.
Save prabod/73e3a8b0c1dbbadfc9d2b9edb34c3670 to your computer and use it in GitHub Desktop.
Fitness Function
/**
* Fitness Function
* fitness = 1 - (Square of pixel difference between chromosome and reference Image)
* ____________________________________________________________________
* ( Resolution of the Image * count(RGBA) * Number of Possible Values)
*
* This fitness function stays inside [0,1]
* */
fitness(width,height){
//Draw the Chromosome First
this.draw(this.ctx,width,height);
var fit = 0;
var imagedata = this.ctx.getImageData(0,0,width,height).data;
for (var i = 0; i < workingData.length;i++){
var dist = workingData[i] - imagedata[i];
fit += dist * dist;
}
this.fitnessValue = 1 - fit / (75 * 75 * 4 * 256 * 256);
return this.fitnessValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment