diff --git a/hamazaki1990/individual.py b/hamazaki1990/individual.py index b1ee698..36d06f6 100644 --- a/hamazaki1990/individual.py +++ b/hamazaki1990/individual.py @@ -1,10 +1,12 @@ -from decimal import Decimal +import random class Individual: - def __init__(self, n, f=1.0): + def __init__(self, n, f=1.0, mutationrate=0.0): self._id = n self._fitness = f + self._genotype = [] + self._mutationrate = mutationrate def get_id(self): return self._id @@ -12,9 +14,20 @@ def get_id(self): def get_fitness(self): return self._fitness + def get_genotype(self): + return self._genotype + + def acquire_mutation(self): + r = random.random() + next_genotype = self._genotype + if self._mutationrate > r: + next_genotype.append(random.random()) + self._genotype = next_genotype + def __repr__(self): return str(self._id) - return Decimal(self._fitness) + return str(self._fitness) + return str(self._genotype) def main(): @@ -22,6 +35,25 @@ def main(): print(ind.get_id()) print(ind.get_fitness()) + ind2 = Individual(30, 0.8, 1.0) + print(ind2.get_id()) + print(ind2.get_fitness()) + ind2.acquire_mutation() + print(ind2.get_genotype()) + ind2.acquire_mutation() + print(ind2.get_genotype()) + print(ind2.get_id()) + print(ind2.get_fitness()) + + ind3 = Individual(0, 1.0, 0.8) + ind4 = Individual(1, 1.0, 0.8) + pop = [ind3, ind4] + print([x.get_genotype() for x in pop]) + [x.acquire_mutation() for x in pop] + print([x.get_genotype() for x in pop]) + [x.acquire_mutation() for x in pop] + print([x.get_genotype() for x in pop]) + if __name__ == '__main__': main() diff --git a/hamazaki1990/population.py b/hamazaki1990/population.py index f46fed1..b6bc41d 100644 --- a/hamazaki1990/population.py +++ b/hamazaki1990/population.py @@ -10,10 +10,10 @@ def roulettechoice(individuals, cumsum_fitness): class Population: - def __init__(self, n, mutantrate=0, s=0): - num_mutant = int(n * mutantrate) - mutant_inds = [Individual(x, 1 + s) for x in range(num_mutant)] - wild_inds = [Individual(x) for x in range(num_mutant, n)] + def __init__(self, n, mutant=0, s=0, mutationrate=0.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)] self._inds = mutant_inds + wild_inds def get_ids(self): @@ -44,6 +44,25 @@ def next_genmo(self): 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 + + def get_mutationlist(self): + genotypes = [x.get_genotype() for x in self._inds] + mutation_sites = [] + for x in range(len(genotypes)): + mutation_sites.extend(genotypes[x]) + mutation_sites = sorted(mutation_sites) + mutation_list = [[0 for x in range(len(mutation_sites))] for y in range(len(self._inds))] + for x in range(len(self._inds)): + for y in range(len(genotypes[x])): + i = mutation_sites.index(genotypes[x][y]) + if i != "ValueError": + mutation_list[x][i] += 1 + print(mutation_list) + def is_not_fixed(self): for x in range(1, len(self._inds)): if self._inds[0] != self._inds[x]: @@ -106,6 +125,13 @@ def main(): print(p2_2.get_ids()) print(p2_2.get_fitnesses()) + p3_1 = Population(10, 0, 0, 0.8) + p3_1.get_mutationlist() + print(p3_1.acquire_mutations()) + p3_1.get_mutationlist() + print(p3_1.acquire_mutations()) + p3_1.get_mutationlist() + if __name__ == '__main__': main()