SHARE
TWEET

Untitled

a guest Aug 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random, pprint, time
  2.  
  3. class Creature(object):
  4.     def __init__(self, metaB, reproRate, temperatureLimit, infantDeath, ageLimit, geneticStability, breedingAge, age):
  5.         self.metaB, self.reproRate, self.temperatureLimit = metaB, reproRate, temperatureLimit
  6.         self.infantDeath, self.ageLimit, self.geneticStability, self.breedingAge, self.age = infantDeath, ageLimit, geneticStability, breedingAge, age
  7.    
  8.     def mutate(self):
  9.         if random.randint(1, 1000) <= self.geneticStability:
  10.             attr = random.choice(["metaB", "reproRate", "temperatureLimit", "infantDeath", "ageLimit", "geneticStability", "breedingAge"])
  11.             setattr(self, attr, getattr(self, attr) + random.choice([-1, 1]))
  12.         self.ageLimit = self.ageLimit or 1
  13.  
  14.     def doAge(self):
  15.         self.age += 1
  16.  
  17.     # Breed with another creature
  18.     def breed(self, other):
  19.         # Find the minimum, maximum litter size
  20.         minimum, maximum = min(self.reproRate, other.reproRate), max(self.reproRate, other.reproRate)
  21.         # Determine litter size
  22.         litterSize = random.randint(minimum, maximum) if minimum != maximum else minimum
  23.         litter = []
  24.         for _ in range(litterSize):
  25.             # Copy either of the parent's attributes, minus age.
  26.             baby = Creature(
  27.                                 random.choice([self.metaB, other.metaB]),
  28.                                 random.choice([self.reproRate, other.reproRate]),
  29.                                 random.choice([self.temperatureLimit, other.temperatureLimit]),
  30.                                 random.choice([self.infantDeath, other.infantDeath]),
  31.                                 random.choice([self.ageLimit, other.ageLimit]),
  32.                                 random.choice([self.geneticStability, other.geneticStability]),
  33.                                 random.choice([self.breedingAge, other.breedingAge]),
  34.                                 0
  35.                             )
  36.             baby.mutate()
  37.             litter.append(baby)
  38.         return litter
  39.  
  40.  
  41. # Starting population
  42. population = [Creature(1, 2, 10, 1, 3, 1, 1, 0)] * 10
  43. totalDeaths, totalBirths = 0, 0
  44. start = time.time()
  45.  
  46. # iterate for 100 generations
  47. for generation in range(100):
  48.     gen_start = time.time()
  49.     breeding_pool = []
  50.     deaths, births = 0, 0
  51.    
  52.     # Age Limit cull
  53.     popBefore = len(population)
  54.     population = list(filter(lambda creature : creature.age < creature.ageLimit, population))
  55.     popAfter = len(population)
  56.     deaths += popBefore - popAfter
  57.     totalDeaths += deaths
  58.  
  59.     # If breeding age allows breeding
  60.     breeding_pool = list(filter(lambda creature : creature.breedingAge >= creature.age, population))
  61.  
  62.     # Shuffle breeding pool
  63.     random.shuffle(breeding_pool)
  64.  
  65.     # If pool count is odd, cull a single creature
  66.     if len(breeding_pool) % 2 == 1:
  67.         del breeding_pool[0]
  68.  
  69.     # Make pairs
  70.     center = len(breeding_pool) // 2
  71.     pair1, pair2 = breeding_pool[:center], breeding_pool[center:]
  72.     pairs = list(zip(pair1, pair2))
  73.    
  74.     # Breeding
  75.     offspring = []
  76.     for parent1, parent2 in pairs:
  77.         offspring.extend(parent1.breed(parent2))
  78.     births += len(offspring)
  79.     totalBirths += births
  80.  
  81.     # Age all creatures
  82.     [creature.doAge() for creature in population]
  83.  
  84.     # Extend population, we're done
  85.     population.extend(offspring)
  86.     gen_end = time.time()
  87.  
  88.     # Print current status
  89.     print('-'*45)
  90.     print('[{}s] Generation {} finished in {} seconds.'.format(round(gen_end - start, 2), generation, round(gen_end - gen_start, 2)))
  91.     print(f"Number of people: {len(population)}\nDeaths: {deaths}\nBirths: {births}")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top