Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. function reproduct(){
  2. var pais = [];
  3. var sum = 0;
  4. var probability = 0;
  5. var sumProb = 0;
  6. var x =0;
  7. for(var i = 0; i < joaninhas.length; i++){//for all members of population
  8. sum += joaninhas[i].score;//sum += fitness of this individual
  9. }// end for
  10.  
  11. for(var i = 0; i < joaninhas.length; i++){//for all members of population
  12. probability = sumProb + (joaninhas[i].score/sum)//probability = sum of probabilities + (fitness / sum)
  13. sumProb += probability;//sum of probabilities += probability
  14. }//end for
  15.  
  16. while(){//loop until new population is full
  17. x = 0;
  18. while(x < 2){//do this twice
  19. number = random(0,1);//number = Random between 0 and 1
  20. for(var i = 0; i < joaninhas.length; i++){//for all members of population
  21. if(number > probability && number < joaninhas[i+1].score){//if number > probability but less than next probability
  22. pais[x] = joaninhas[i];//then you have been selected
  23. }
  24. }//end for
  25. x++;
  26. }//end
  27. //create offspring
  28. break;
  29. }//end loop
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement