Skip to content

Instantly share code, notes, and snippets.

@prabod
Last active December 3, 2016 19:44
Show Gist options
  • Save prabod/2f0c4219209a35366dee1970ceac6e66 to your computer and use it in GitHub Desktop.
Save prabod/2f0c4219209a35366dee1970ceac6e66 to your computer and use it in GitHub Desktop.
Selection Procedure
/**
* Breed a New Generation of Chromosomes
* */
function breed() {
var totalFitness = 0;
var fittest;
var fit=0;
/**
* Calculate fitness of each and every chromosome
* */
for(var j = 0 ; j < population.length ; j++){
var temp = population[j].fitness(75,75,goal);
if (temp >= fit){
fit = temp;
fittest = population[j];// Fittest Chromosome in a Generation
}
totalFitness += temp;
}
/**
* Sort the Generation
* */
population = population.sort(function(a, b) {
return b.fitnessValue - a.fitnessValue;
});
var newPopulation = [];
/**
* Select the Chromosomes with best fitnesses
* */
var selectCount = Math.floor(population.length * CROSSOVER_RATE);
/**
* Number of Chromosomes that needed to be crossed with the each of the Chosen Chromosome
* */
var randCount = Math.ceil(1 / CROSSOVER_RATE);
/**
* Select two parents and breed
* */
for (var i = 0; i < selectCount; i++) {
for (var h = 0; h < randCount; h++) {
var parent = i;
while (parent == i) {
parent = (Math.random() * selectCount) >> 0;
}
/**
* Breed
* */
var crossed = crossover(population[i], population[parent], CROSSOVER_RATE, GENE_SIZE, CHROME_SIZE);
newPopulation.push(crossed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment