Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. /**
  2. * Breed a New Generation of Chromosomes
  3. * */
  4. function breed() {
  5.  
  6. var totalFitness = 0;
  7. var fittest;
  8. var fit=0;
  9.  
  10. /**
  11. * Calculate fitness of each and every chromosome
  12. * */
  13. for(var j = 0 ; j < population.length ; j++){
  14.  
  15. var temp = population[j].fitness(75,75,goal);
  16.  
  17. if (temp >= fit){
  18.  
  19. fit = temp;
  20. fittest = population[j];// Fittest Chromosome in a Generation
  21.  
  22. }
  23.  
  24. totalFitness += temp;
  25. }
  26.  
  27. /**
  28. * Sort the Generation
  29. * */
  30. population = population.sort(function(a, b) {
  31. return b.fitnessValue - a.fitnessValue;
  32. });
  33.  
  34. var newPopulation = [];
  35.  
  36. /**
  37. * Select the Chromosomes with best fitnesses
  38. * */
  39. var selectCount = Math.floor(population.length * CROSSOVER_RATE);
  40.  
  41. /**
  42. * Number of Chromosomes that needed to be crossed with the each of the Chosen Chromosome
  43. * */
  44. var randCount = Math.ceil(1 / CROSSOVER_RATE);
  45.  
  46. /**
  47. * Select two parents and breed
  48. * */
  49. for (var i = 0; i < selectCount; i++) {
  50.  
  51. for (var h = 0; h < randCount; h++) {
  52.  
  53. var parent = i;
  54.  
  55. while (parent == i) {
  56.  
  57. parent = (Math.random() * selectCount) >> 0;
  58.  
  59. }
  60. /**
  61. * Breed
  62. * */
  63. var crossed = crossover(population[i], population[parent], CROSSOVER_RATE, GENE_SIZE, CHROME_SIZE);
  64. newPopulation.push(crossed);
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement