def test_connectivity_matrix(): """ This test will ensure two things: 1. That all values of the connectivity matrix are 0 or 1, representing nodes that are connected (1) or disconnected (0). 2. That the number of connections in a society using a very high connectivity_probability is greater then a society using a very low connectivity_probability. """ # TO DO FOR MITCHMAN # First, go through the matrix and test to make sure all edges are an # appropriate value. # Instantiate constants population_size = 50 connectivity_probability = 0.15 # Create society object. society = Society( population_size=population_size, connectivity_probability=connectivity_probability, ) # Loop through each entry and assert either 0 or 1. for x in range(population_size): for y in range(population_size): assert (society.edge_matrix[x][y] == 0) or (society.edge_matrix[x][y] == 1) # Next, create two society classes, one with connectivity probability 0.99 # and another with probability 0.01. Both will have larger population # sizes to avoid fluke statistical generation. Assert sums of the two # edge matrices. # Set up constants population_size = 1000 connectivity_probability_first = 0.99 connectivity_probability_second = 0.01 # Create both society objects society_first = Society( population_size=population_size, connectivity_probability=connectivity_probability_first, ) society_second = Society( population_size=population_size, connectivity_probability=connectivity_probability_second, ) # Sum edges, and assert that the first has more than the second. count_first = 0 count_second = 0 for x in range(population_size): for y in range(population_size): if society_first.edge_matrix[x][y] == 1: count_first += 1 if society_second.edge_matrix[x][y] == 1: count_second += 1 assert count_first > count_second
def demo_2(): # 遺伝子長 dimension = 30 # 集団数 individual_num = 300 # 世代数の最大 generation_loop = 1000 # 評価関数 evaluator = Rosenbrock() # 親の選択と交叉 # 親の数は交叉方法に合わせて設定する # 次世代に残す個体の選択 # Simplex, ルーレット選択 society = Society( evaluator, Simplex(dimension * 10), RouletteSelector(dimension + 1), JGG()) generator = Generator(10., -10., dimension) society.generate_individuals(individual_num, generator) # 初期個体の生成 society.generate_individuals(individual_num, generator) # 画像データ表示用 best_value = [] iterate = [] generation_data = [] generation_data.append(get_generation_data(society.individuals)) best = None for i in range(generation_loop): society.change_generation() # 画像データ表示用処理 best = society.get_best_individual() iterate.append(i) best_value.append(best.evaluate_value) generation_data.append(get_generation_data(society.individuals)) print(i, best.evaluate_value) # 最良評価値の推移を表示 plot_transition(iterate, best_value, generation_data)
def test_lawInstitution_fields(): # Verify, upon instantiation, that all fields of the law and institution # classes are of the type they should be. society = Society(population_size=10) institution = Institution(society) laws = institution._generate_laws(society) assert isinstance(institution.get_affiliation(), float) for law in laws: for person_id in range(society.population_size): assert isinstance(law.affected_persons[person_id], int) assert isinstance(law.mean, float) assert isinstance(law.law_type, str) assert isinstance(law.actual_value, float)
def test_population(): """ This test will simply ensure that a created Society object has a list of Person objects, that is of length population_size. """ # Set up constants population_size = 50 connectivity_probability = 0.15 # Create Society object. society = Society( population_size=population_size, connectivity_probability=connectivity_probability, ) # Assert the correct population. assert len(society.person_vector) == population_size # Assert each object in society.person_vector is a Person object for person in society.person_vector: assert type(person) == Person
def test_toy_model(): random.seed(42) s = Society(episodes_per_day=5, encounter_size=2) d = Disease(days_infectious=10, pr_transmission_per_day=0.2) # seed size is the number of people in the population who we seed as being infected: o = Outbreak(s, d, pop_size=1000, seed_size=2, n_days=ALL_TIME_DAYS, population_type=Population, person_type=Person) # so, this is a village of 10000 people with 2 starting off infected o.simulate() assert o.recorder.story[90:95] == \ [[18.199999999999967, 0.619, 0.57, 0.0, 0.0, 0.0], [18.399999999999967, 0.643, 0.592, 0.0, 0.0, 0.0], [18.599999999999966, 0.656, 0.603, 0.0, 0.0, 0.0], [18.799999999999965, 0.673, 0.618, 0.0, 0.0, 0.0], [18.999999999999964, 0.69, 0.635, 0.0, 0.0, 0.0]]
def demo_3(): # 遺伝子長 dimension = 30 # 集団数 individual_num = 300 # 世代数の最大 generation_loop = 1000 # 評価関数 evaluator = Rosenbrock() # 次世代に残す個体の選択 society_simplex = Society( evaluator, Simplex(dimension * 10), RouletteSelector(dimension + 1), JGG()) generator = Generator(10., -10., dimension) society_blx = Society( evaluator, BLX_alpha(dimension * 10), RouletteSelector(2), JGG()) # 初期個体の生成 society_simplex.generate_individuals(individual_num, generator) society_blx.generate_individuals(individual_num, generator) # 画像データ表示用 simplex_data = [] blx_data = [] iterator_list = [] best_simplex = society_simplex.get_best_individual() best_blx = society_blx.get_best_individual() simplex_data.append(best_simplex.evaluate_value) best_blx.append(best_blx.evaluate_value) iterator_list.append() for i in range(generation_loop): society_simplex.change_generation() society_blx.change_generation() # 画像データ表示用処理 best_simplex = society_simplex.get_best_individual() best_blx = society_blx.get_best_individual() simplex_data.append(best_simplex.evaluate_value) blx_data.append(best_blx.evaluate_value) iterator_list.append(i) print(i) # 評価値の推移を表示 plot_evaluate_values(iterator_list, simplex_data, blx_data)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Apr 7 18:42:28 2020 @author: beca4397 """ from society import Society soc1 = Society()
from person import Person from meme import Meme from society import Society SCREEN_SIZE = 1000 clock = pygame.time.Clock() pygame.init() pygame.display.set_caption('Memetic Evolution Simulation') screen = pygame.display.set_mode((SCREEN_SIZE, SCREEN_SIZE), 0, 32) missionary = Meme(spread=200) cult = Meme(spread=5) society_1 = Society(SCREEN_SIZE / 3, SCREEN_SIZE / 2) society_2 = Society(2 * SCREEN_SIZE/3, SCREEN_SIZE/2) societies = [society_1, society_2] people = [] for _ in range(100): people.append(Person(society=society_1)) for _ in range(10): people.append(Person(society=society_1, meme=missionary)) for _ in range(100): people.append(Person(society=society_2)) for _ in range(10): people.append(Person(society=society_2, meme=cult))