Skip to content

Commit

Permalink
solved #21
Browse files Browse the repository at this point in the history
  • Loading branch information
hamazaki1990 committed May 21, 2017
1 parent a8f9be5 commit 189f1c6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
38 changes: 35 additions & 3 deletions hamazaki1990/individual.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
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

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():
ind = Individual(42)
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()
34 changes: 30 additions & 4 deletions hamazaki1990/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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()

0 comments on commit 189f1c6

Please sign in to comment.