Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random, pprint, time
- class Creature(object):
- def __init__(self, metaB, reproRate, temperatureLimit, infantDeath, ageLimit, geneticStability, breedingAge, age):
- self.metaB, self.reproRate, self.temperatureLimit = metaB, reproRate, temperatureLimit
- self.infantDeath, self.ageLimit, self.geneticStability, self.breedingAge, self.age = infantDeath, ageLimit, geneticStability, breedingAge, age
- def mutate(self):
- if random.randint(1, 1000) <= self.geneticStability:
- attr = random.choice(["metaB", "reproRate", "temperatureLimit", "infantDeath", "ageLimit", "geneticStability", "breedingAge"])
- setattr(self, attr, getattr(self, attr) + random.choice([-1, 1]))
- self.ageLimit = self.ageLimit or 1
- def doAge(self):
- self.age += 1
- # Breed with another creature
- def breed(self, other):
- # Find the minimum, maximum litter size
- minimum, maximum = min(self.reproRate, other.reproRate), max(self.reproRate, other.reproRate)
- # Determine litter size
- litterSize = random.randint(minimum, maximum) if minimum != maximum else minimum
- litter = []
- for _ in range(litterSize):
- # Copy either of the parent's attributes, minus age.
- baby = Creature(
- random.choice([self.metaB, other.metaB]),
- random.choice([self.reproRate, other.reproRate]),
- random.choice([self.temperatureLimit, other.temperatureLimit]),
- random.choice([self.infantDeath, other.infantDeath]),
- random.choice([self.ageLimit, other.ageLimit]),
- random.choice([self.geneticStability, other.geneticStability]),
- random.choice([self.breedingAge, other.breedingAge]),
- 0
- )
- baby.mutate()
- litter.append(baby)
- return litter
- # Starting population
- population = [Creature(1, 2, 10, 1, 3, 1, 1, 0)] * 10
- totalDeaths, totalBirths = 0, 0
- start = time.time()
- # iterate for 100 generations
- for generation in range(100):
- gen_start = time.time()
- breeding_pool = []
- deaths, births = 0, 0
- # Age Limit cull
- popBefore = len(population)
- population = list(filter(lambda creature : creature.age < creature.ageLimit, population))
- popAfter = len(population)
- deaths += popBefore - popAfter
- totalDeaths += deaths
- # If breeding age allows breeding
- breeding_pool = list(filter(lambda creature : creature.breedingAge >= creature.age, population))
- # Shuffle breeding pool
- random.shuffle(breeding_pool)
- # If pool count is odd, cull a single creature
- if len(breeding_pool) % 2 == 1:
- del breeding_pool[0]
- # Make pairs
- center = len(breeding_pool) // 2
- pair1, pair2 = breeding_pool[:center], breeding_pool[center:]
- pairs = list(zip(pair1, pair2))
- # Breeding
- offspring = []
- for parent1, parent2 in pairs:
- offspring.extend(parent1.breed(parent2))
- births += len(offspring)
- totalBirths += births
- # Age all creatures
- [creature.doAge() for creature in population]
- # Extend population, we're done
- population.extend(offspring)
- gen_end = time.time()
- # Print current status
- print('-'*45)
- print('[{}s] Generation {} finished in {} seconds.'.format(round(gen_end - start, 2), generation, round(gen_end - gen_start, 2)))
- print(f"Number of people: {len(population)}\nDeaths: {deaths}\nBirths: {births}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement