diff --git a/hamazaki1990/individual.py b/hamazaki1990/individual.py index f83aa71..8c0c071 100644 --- a/hamazaki1990/individual.py +++ b/hamazaki1990/individual.py @@ -2,11 +2,12 @@ class Individual: - def __init__(self, n, f=1.0, mutationrate=0.0): + mutationrate = 0.01 + + def __init__(self, n, f=1.0): self._id = n self._fitness = f self._genotype = [] - self._mutationrate = mutationrate def get_id(self): return self._id @@ -20,7 +21,7 @@ def get_genotype(self): def acquire_mutation(self): r = random.random() next_genotype = self._genotype - if r < self._mutationrate: + if r < Individual.mutationrate: next_genotype.append(random.random()) self._genotype = next_genotype @@ -33,7 +34,7 @@ def main(): print(ind.get_id()) print(ind.get_fitness()) - ind2 = Individual(30, 0.8, 1.0) + ind2 = Individual(30, 0.8) print(ind2.get_id()) print(ind2.get_fitness()) ind2.acquire_mutation() @@ -43,8 +44,8 @@ def main(): print(ind2.get_id()) print(ind2.get_fitness()) - ind3 = Individual(0, 1.0, 0.8) - ind4 = Individual(1, 1.0, 0.8) + ind3 = Individual(0, 1.0) + ind4 = Individual(1, 1.0) pop = [ind3, ind4] print([x.get_genotype() for x in pop]) [x.acquire_mutation() for x in pop] diff --git a/hamazaki1990/population.py b/hamazaki1990/population.py index 528c458..801fcd8 100644 --- a/hamazaki1990/population.py +++ b/hamazaki1990/population.py @@ -10,10 +10,10 @@ def roulettechoice(individuals, cumsum_fitness): class Population: - def __init__(self, n, mutant=0, s=0, mutationrate=0.0): + def __init__(self, n, mutant=0, s=0): num_mutant = int(n * mutant) - mutant_inds = [Individual(x, 1 + s, mutationrate) for x in range(num_mutant)] - wild_inds = [Individual(x, 1, mutationrate) for x in range(num_mutant, n)] + mutant_inds = [Individual(x, 1 + s) for x in range(num_mutant)] + wild_inds = [Individual(x, 1) for x in range(num_mutant, n)] self._inds = mutant_inds + wild_inds def get_ids(self): @@ -23,10 +23,9 @@ def get_fitnesses(self): fitness = [x.get_fitness() for x in self._inds] return fitness - def get_mutantfreq(self): - fitness = [x.get_fitness() for x in self._inds] - mutantfreq = 1 - fitness.count(1.0)/len(fitness) - return mutantfreq + def get_genotypes(self): + genotypes = [x.get_genotype() for x in self._inds] + return genotypes def next_genwf(self): fitness = [x.get_fitness() for x in self._inds] @@ -34,7 +33,9 @@ def next_genwf(self): cumsum_fitness = [sum(fitness[:i]) for i in range(1, size + 1)] next_generation = [] for x in range(size): - next_generation.append(roulettechoice(self._inds, cumsum_fitness)) + next_inds = roulettechoice(self._inds, cumsum_fitness) + next_inds.acquire_mutation() + next_generation.append(next_inds) self._inds = next_generation def next_genmo(self): @@ -42,12 +43,9 @@ def next_genmo(self): size = len(self._inds) cumsum_fitness = [sum(fitness[:i]) for i in range(1, size + 1)] i_dying = random.randrange(size) - self._inds[i_dying] = roulettechoice(self._inds, cumsum_fitness) - - def acquire_mutations(self): - [x.acquire_mutation() for x in self._inds] - genotypes = [x.get_genotype() for x in self._inds] - return genotypes + next_inds = roulettechoice(self._inds, cumsum_fitness) + next_inds.acquire_mutation() + self._inds[i_dying] = next_inds def list_mutation(self): genotypes = [x.get_genotype() for x in self._inds] @@ -55,7 +53,8 @@ def list_mutation(self): for x in genotypes: m_sites.extend(x) m_sites = sorted(m_sites) - m_list = [[0 for x in range(len(m_sites))] for y in range(len(self._inds))] + m_list = ([[0 for x in range(len(m_sites))] + for y in range(len(self._inds))]) for i in range(len(self._inds)): for j in range(len(genotypes[i])): k = m_sites.index(genotypes[i][j]) @@ -70,67 +69,46 @@ def is_not_fixed(self): else: return False - def mutation_is_not_fixed(self): - fitness = [x.get_fitness() for x in self._inds] - for x in range(1, len(fitness)): - if fitness[0] != fitness[x]: - return True - else: - return False - def main(): p1_1 = Population(10) print(p1_1.get_ids()) print(p1_1.get_fitnesses()) print(p1_1.is_not_fixed()) - print(p1_1.mutation_is_not_fixed()) - for x in range(20): + for x in range(10): p1_1.next_genwf() print(p1_1.get_ids()) - print(p1_1.get_fitnesses()) + print(p1_1.get_genotypes()) + p1_1.list_mutation() + print(p1_1.is_not_fixed()) - print(p1_1.mutation_is_not_fixed()) + p1_1.list_mutation() p1_2 = Population(10, 0.3, 0.2) print(p1_2.get_ids()) print(p1_2.get_fitnesses()) print(p1_2.is_not_fixed()) - print(p1_2.mutation_is_not_fixed()) - print(p1_2.get_mutantfreq()) - - for x in range(20): - p1_2.next_genwf() - print(p1_2.get_ids()) - print(p1_2.get_fitnesses()) - print(p1_2.is_not_fixed()) - print(p1_2.mutation_is_not_fixed()) p2_1 = Population(10) print(p2_1.get_ids()) print(p2_1.get_fitnesses()) - for x in range(20): + for x in range(10): p2_1.next_genmo() print(p2_1.get_ids()) print(p2_1.get_fitnesses()) + p2_1.list_mutation() p2_2 = Population(10, 0.3, 0.2) print(p2_2.get_ids()) print(p2_2.get_fitnesses()) - for x in range(20): + for x in range(10): p2_2.next_genmo() print(p2_2.get_ids()) print(p2_2.get_fitnesses()) - - p3_1 = Population(10, 0, 0, 0.8) - p3_1.list_mutation() - print(p3_1.acquire_mutations()) - p3_1.list_mutation() - print(p3_1.acquire_mutations()) - p3_1.list_mutation() + p2_2.list_mutation() if __name__ == '__main__': diff --git a/hamazaki1990/visualize.py b/hamazaki1990/visualize.py index d4f43d4..7839484 100644 --- a/hamazaki1990/visualize.py +++ b/hamazaki1990/visualize.py @@ -2,23 +2,34 @@ from population import Population +def calculate_mutantfreq(population): + m_list = population.list_mutation() + m_count = [0 for x in range(len(m_list[0]))] + for i in m_list: + for j in m_list[i]: + if i[j] != 0: + m_count[j] += 1 + mutantfreq = [x/len(m_list) for x in m_count] + return mutantfreq + + def change_allelewf(population): t = 0 - fixprocess = [[t, population.get_mutantfreq()]] - while population.mutation_is_not_fixed(): + fixprocess = [[t, population.calculate_mutantfreq()]] + for x in range(20): t += 1 population.next_genwf() - fixprocess.append([t, population.get_mutantfreq()]) + fixprocess.append([t, population.calculate_mutantfreq()]) return fixprocess def change_allelemo(population): t = 0 - fixprocess = [[t, population.get_mutantfreq()]] - while population.mutation_is_not_fixed(): + fixprocess = [[t, population.calculate_mutantfreq()]] + for x in range(20): t += 1 population.next_genmo() - fixprocess.append([t, population.get_mutantfreq()]) + fixprocess.append([t, population.calculate_mutantfreq()]) return fixprocess