Skip to content

Instantly share code, notes, and snippets.

@prabod prabod/selection.js
Last active Dec 3, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.