Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement