Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. import random, time
  2. print('Loading')
  3.  
  4. # Constants
  5. METABOLISM, REPRO_RATE, TEMPERATURE_LIMIT, INFANT_DEATH_CHANCE, AGE_LIMIT, GENETIC_STABILITY, BREEDING_AGE, AGE = 1, 2, 10, 1, 3, 1, 1, 0
  6. population = [[METABOLISM, REPRO_RATE, TEMPERATURE_LIMIT, INFANT_DEATH_CHANCE, AGE_LIMIT, GENETIC_STABILITY, BREEDING_AGE, AGE]] * 10
  7. offspring = shuffled_breed = breedable = []
  8. number_of_genarations = 0
  9. deaths = births = total_deaths = total_births = 0
  10. start = time.time()
  11.  
  12. for _ in range(100):
  13. gen_start = time.time()
  14. before = len(population)
  15. # Age Limit Culling
  16. population = list(filter(lambda item : item[7] != item[4], population))
  17. after = len(population)
  18. deaths += before - after
  19. # If breeding age allows breeding
  20. breedable.extend(list(filter(lambda item : item[7] >= item[6], population)))
  21. # Shuffle breeding list
  22. random.shuffle(breedable)
  23.  
  24. if len(breedable) % 2 == 1:
  25. del breedable[0]
  26.  
  27. # loop over the entire breeding catalog
  28. while len(breedable) != 0:
  29. # The the first creature's repro rate is higher than the second's
  30. if breedable[1][1] > breedable[0][1]:
  31. litter_cap = random.randint(breedable[0][1], breedable[1][1])
  32. # Second creature's repro rate is higher
  33. elif breedable[0][1] > breedable[1][1]:
  34. litter_cap = random.randint(breedable[1][1], breedable[0][1])
  35. # Repro rate is equal between creatures
  36. elif breedable[0][1] == breedable[1][1]:
  37. litter_cap = random.randint(breedable[0][1], breedable[1][1]+1)
  38.  
  39. # With a litter_cap set, generate a litter
  40. for litter in range(litter_cap):
  41. # Copy either of the parent's attributes, minus age.
  42. baby = [breedable[random.randint(0, 1)][i] for i in range(7)] + [0]
  43. # GENETIC STABILITY
  44. if random.randint(1, 1000) <= baby[5]:
  45. baby[random.randint(0, 6)] += random.choice([-1, 1])
  46. baby[4] = baby[4] or 1
  47. offspring.append(baby)
  48. births += 1
  49. del breedable[0:2]
  50.  
  51. #aging of population
  52. age_check = 0
  53. while len(population) != age_check:
  54. population[age_check][7] += 1
  55. age_check += 1
  56.  
  57. #offspring added to population
  58. population.extend(offspring)
  59.  
  60. gen_end = time.time()
  61.  
  62. #resets
  63. offspring, breedable, shuffled_breed = [], [], []
  64. number_of_genarations += 1
  65. print('-'*45)
  66. print('[{}s] Generation {} finished in {} seconds.'.format(round(gen_end - start, 2), number_of_genarations, round(gen_end - gen_start, 2)))
  67. print(f"Number of people: {len(population)}\nDeaths: {deaths}\nBirths: {births}")
  68. total_deaths += deaths
  69. total_births += births
  70. births = deaths = 0
  71.  
  72. print(f"Population: {len(population)}")
  73. print(f"Deaths: {total_deaths}")
  74. print(f"Births: {total_births}")
  75. print(f"Creatures: {population}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement