Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func generateGenerations(org, goalOrg organism) {
- var offspring []organism
- // Loop through the amount of offspring you want the next generation to have
- // Select an arbitrary chance for any given offspring to mutate
- amountOffspring := 5
- chanceToMutate := rand.Intn(5) == 1 // 20% chance
- for i := 0; i < amountOffspring; i++ {
- if chanceToMutate {
- offspring = append(offspring, mutate(org, goalOrg))
- } else {
- offspring = append(offspring, org)
- }
- }
- // Get the best fitted offspring
- bestOffspring := calcBestOffs(offspring, goalOrg)
- fmt.Printf("%d: %s \n", bestOffspring.fit, string(bestOffspring.name))
- // Check if the best offspring is equal to the goal organism
- if string(bestOffspring.name) != string(goalOrg.name) {
- // Print out the winner
- fmt.Printf("%s -- Goal! \n", string(bestOffspring.name))
- } else {
- // If not call this function again, but with the best offspring as parent to the next generation
- generateGenerations(bestOffspring, goalOrg)
- }
- }
Add Comment
Please, Sign In to add comment