Advertisement
Guest User

Untitled

a guest
May 25th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import random
  5.  
  6. PROB_FIND_A_COUPLE = 0.8
  7.  
  8. ADEPT_HUMAN = 1
  9. ORDINARY_HUMAN = 2
  10.  
  11. def make_children(human1, human2):
  12.     if human1 == ADEPT_HUMAN and human2 == ADEPT_HUMAN:
  13.         children_count = random.randint(0, 6)
  14.         prob_is_adept = 1
  15.     elif human1 == ORDINARY_HUMAN and human2 == ORDINARY_HUMAN:
  16.         children_count = random.randint(0, 3)
  17.         prob_is_adept = 0
  18.     else:
  19.         children_count = random.randint(0, 4)
  20.         prob_is_adept = 0.5
  21.  
  22.     children = []
  23.     for i in xrange(children_count):
  24.         if prob_is_adept >= random.random():
  25.             children.append(ADEPT_HUMAN)
  26.         else:
  27.             children.append(ORDINARY_HUMAN)
  28.  
  29.     return children
  30.  
  31.  
  32. def generate_new_population(population):
  33.     new_population = []
  34.     while population:
  35.         population_len = len(population)
  36.         if population_len == 1:
  37.             break
  38.         else:
  39.             human = population.pop(random.randint(0, population_len - 1))
  40.             if random.random() <= PROB_FIND_A_COUPLE:
  41.                 couple = population.pop(random.randint(0, population_len - 2))
  42.                 new_population += make_children(human, couple)
  43.     return new_population
  44.  
  45.  
  46. def show_stats(population):
  47.     pop_len = len(population)
  48.     adept_count = 0
  49.     for human in population:
  50.         if human == ADEPT_HUMAN:
  51.             adept_count += 1
  52.     if pop_len:
  53.         p = float(adept_count) / pop_len
  54.     else:
  55.         p = 0
  56.     print adept_count, '\t of\t', pop_len, '\t(' + str(p) + ')'
  57.  
  58.  
  59. if __name__ == '__main__':
  60.     # random.seed(1)
  61.     start_population = []
  62.     for i in xrange(1, 1000):
  63.         start_population.append(ADEPT_HUMAN)
  64.  
  65.     for i in xrange(1, 9000):
  66.         start_population.append(ORDINARY_HUMAN)
  67.  
  68.     population = start_population
  69.     show_stats(population)
  70.     for i in xrange(15):
  71.         population = generate_new_population(population)
  72.         show_stats(population)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement