Guest User

Untitled

a guest
Jul 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. func generateGenerations(org, goalOrg organism) {
  2. var offspring []organism
  3.  
  4. // Loop through the amount of offspring you want the next generation to have
  5. // Select an arbitrary chance for any given offspring to mutate
  6. amountOffspring := 5
  7. chanceToMutate := rand.Intn(5) == 1 // 20% chance
  8. for i := 0; i < amountOffspring; i++ {
  9. if chanceToMutate {
  10. offspring = append(offspring, mutate(org, goalOrg))
  11. } else {
  12. offspring = append(offspring, org)
  13. }
  14. }
  15.  
  16. // Get the best fitted offspring
  17. bestOffspring := calcBestOffs(offspring, goalOrg)
  18.  
  19. fmt.Printf("%d: %s \n", bestOffspring.fit, string(bestOffspring.name))
  20.  
  21. // Check if the best offspring is equal to the goal organism
  22. if string(bestOffspring.name) != string(goalOrg.name) {
  23. // Print out the winner
  24. fmt.Printf("%s -- Goal! \n", string(bestOffspring.name))
  25. } else {
  26. // If not call this function again, but with the best offspring as parent to the next generation
  27. generateGenerations(bestOffspring, goalOrg)
  28. }
  29. }
Add Comment
Please, Sign In to add comment