Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random, time
- print('Loading')
- # Constants
- METABOLISM, REPRO_RATE, TEMPERATURE_LIMIT, INFANT_DEATH_CHANCE, AGE_LIMIT, GENETIC_STABILITY, BREEDING_AGE, AGE = 1, 2, 10, 1, 3, 1, 1, 0
- population = [[METABOLISM, REPRO_RATE, TEMPERATURE_LIMIT, INFANT_DEATH_CHANCE, AGE_LIMIT, GENETIC_STABILITY, BREEDING_AGE, AGE]] * 10
- offspring = shuffled_breed = breedable = []
- number_of_genarations = 0
- deaths = births = total_deaths = total_births = 0
- start = time.time()
- for _ in range(100):
- gen_start = time.time()
- before = len(population)
- # Age Limit Culling
- population = list(filter(lambda item : item[7] != item[4], population))
- after = len(population)
- deaths += before - after
- # If breeding age allows breeding
- breedable.extend(list(filter(lambda item : item[7] >= item[6], population)))
- # Shuffle breeding list
- random.shuffle(breedable)
- if len(breedable) % 2 == 1:
- del breedable[0]
- # loop over the entire breeding catalog
- while len(breedable) != 0:
- # The the first creature's repro rate is higher than the second's
- if breedable[1][1] > breedable[0][1]:
- litter_cap = random.randint(breedable[0][1], breedable[1][1])
- # Second creature's repro rate is higher
- elif breedable[0][1] > breedable[1][1]:
- litter_cap = random.randint(breedable[1][1], breedable[0][1])
- # Repro rate is equal between creatures
- elif breedable[0][1] == breedable[1][1]:
- litter_cap = random.randint(breedable[0][1], breedable[1][1]+1)
- # With a litter_cap set, generate a litter
- for litter in range(litter_cap):
- # Copy either of the parent's attributes, minus age.
- baby = [breedable[random.randint(0, 1)][i] for i in range(7)] + [0]
- # GENETIC STABILITY
- if random.randint(1, 1000) <= baby[5]:
- baby[random.randint(0, 6)] += random.choice([-1, 1])
- baby[4] = baby[4] or 1
- offspring.append(baby)
- births += 1
- del breedable[0:2]
- #aging of population
- age_check = 0
- while len(population) != age_check:
- population[age_check][7] += 1
- age_check += 1
- #offspring added to population
- population.extend(offspring)
- gen_end = time.time()
- #resets
- offspring, breedable, shuffled_breed = [], [], []
- number_of_genarations += 1
- print('-'*45)
- print('[{}s] Generation {} finished in {} seconds.'.format(round(gen_end - start, 2), number_of_genarations, round(gen_end - gen_start, 2)))
- print(f"Number of people: {len(population)}\nDeaths: {deaths}\nBirths: {births}")
- total_deaths += deaths
- total_births += births
- births = deaths = 0
- print(f"Population: {len(population)}")
- print(f"Deaths: {total_deaths}")
- print(f"Births: {total_births}")
- print(f"Creatures: {population}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement