Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import random
- PROB_FIND_A_COUPLE = 0.8
- ADEPT_HUMAN = 1
- ORDINARY_HUMAN = 2
- def make_children(human1, human2):
- if human1 == ADEPT_HUMAN and human2 == ADEPT_HUMAN:
- children_count = random.randint(0, 6)
- prob_is_adept = 1
- elif human1 == ORDINARY_HUMAN and human2 == ORDINARY_HUMAN:
- children_count = random.randint(0, 3)
- prob_is_adept = 0
- else:
- children_count = random.randint(0, 4)
- prob_is_adept = 0.5
- children = []
- for i in xrange(children_count):
- if prob_is_adept >= random.random():
- children.append(ADEPT_HUMAN)
- else:
- children.append(ORDINARY_HUMAN)
- return children
- def generate_new_population(population):
- new_population = []
- while population:
- population_len = len(population)
- if population_len == 1:
- break
- else:
- human = population.pop(random.randint(0, population_len - 1))
- if random.random() <= PROB_FIND_A_COUPLE:
- couple = population.pop(random.randint(0, population_len - 2))
- new_population += make_children(human, couple)
- return new_population
- def show_stats(population):
- pop_len = len(population)
- adept_count = 0
- for human in population:
- if human == ADEPT_HUMAN:
- adept_count += 1
- if pop_len:
- p = float(adept_count) / pop_len
- else:
- p = 0
- print adept_count, '\t of\t', pop_len, '\t(' + str(p) + ')'
- if __name__ == '__main__':
- # random.seed(1)
- start_population = []
- for i in xrange(1, 1000):
- start_population.append(ADEPT_HUMAN)
- for i in xrange(1, 9000):
- start_population.append(ORDINARY_HUMAN)
- population = start_population
- show_stats(population)
- for i in xrange(15):
- population = generate_new_population(population)
- show_stats(population)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement