Skip to content

Commit

Permalink
Issue 15 fixation (#21)
Browse files Browse the repository at this point in the history
* #15 tochuu

* approaching #15

* fixation simulation #15

* repeat simulation #15

* #15 solving

* fighting #15

* modified #15

* modifying #15

* Create class Repeat-moran

* Create class Repeat-moran

* solved #15

* delete function of repeat

* Reduce code redundancy
  • Loading branch information
hamazaki1990 authored and heavywatal committed May 8, 2017
1 parent df52d20 commit e103bd6
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 34 deletions.
17 changes: 11 additions & 6 deletions hamazaki1990/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ def __repr__(self):
return str(self._id)


ind = Individual(42)
print(ind.get_id())
print(ind.get_fitness())
def main():
ind = Individual(42)
print(ind.get_id())
print(ind.get_fitness())

ind2 = Individual(30, 0.8)
print(ind2.get_id())
print(ind2.get_fitness())
ind2 = Individual(30, 0.8)
print(ind2.get_id())
print(ind2.get_fitness())


if __name__ == '__main__':
main()
62 changes: 59 additions & 3 deletions hamazaki1990/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""main script
"""
print('Hello, world!')
from population import Population


def calculate_ave(seq):
return sum(seq)/len(seq)


def calculate_var(seq):
ave = calculate_ave(seq)
sqd = [(seq[x] - ave)**2 for x in range(len(seq))]
return sum(sqd)/len(seq)


def simulate_fixwf(Population): # simulate fixation in Wrigft-Fisher model
time = 0
while Population.is_not_fixed():
Population.next_genwf()
time += 1
return time, Population._inds[0].get_id()


def simulate_fixmo(Population): # simulate fixation in Moran model
time = 0
while Population.is_not_fixed():
Population.next_genmo()
time += 1
return time, Population._inds[0].get_id()


class Repeater:
def __init__(self, function, repeat, n, mutantrate=0, s=0):
result = []
for x in range(repeat):
population = Population(n, mutantrate, s)
result.append(function(population))
self._fixtime = [result[x][0] for x in range(len(result))]
fixid = [result[x][1] for x in range(len(result))]
wins = [0 for i in range(n)]
for x in fixid:
wins[x] += 1
self._fixprob = {i: wins[i]/repeat for i in range(n)}

def get_fixtime(self):
return self._fixtime

def get_fixprob(self):
return self._fixprob


trial1 = Repeater(simulate_fixwf, 1000, 10, 0.1, -0.05)
print(calculate_ave(trial1.get_fixtime()))
print(calculate_var(trial1.get_fixtime()))
print(trial1.get_fixprob())


trial2 = Repeater(simulate_fixmo, 1000, 10, 0.1, -0.05)
print(calculate_ave(trial2.get_fixtime()))
print(calculate_var(trial2.get_fixtime()))
print(trial2.get_fixprob())
63 changes: 38 additions & 25 deletions hamazaki1990/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,53 @@ def next_genmo(self):
i_dying = random.randrange(size)
self._inds[i_dying] = roulettechoice(self._inds, cumsum_fitness)

def is_not_fixed(self):
for x in range(1, len(self._inds)):
if self._inds[0] != self._inds[x]:
return True
else:
return False

p1_1 = Population(10)
p1_1.print_ids()
p1_1.print_fitness()

for x in range(20):
p1_1.next_genwf()
def main():
p1_1 = Population(10)
p1_1.print_ids()
p1_1.print_fitness()
p1_1.print_fitness()
print(p1_1.is_not_fixed())

p1_2 = Population(10, 0.3, 0.2)
p1_2.print_ids()
p1_2.print_fitness()
for x in range(20):
p1_1.next_genwf()
p1_1.print_ids()
p1_1.print_fitness()
print(p1_1.is_not_fixed())

for x in range(20):
p1_2.next_genwf()
p1_2 = Population(10, 0.3, 0.2)
p1_2.print_ids()
p1_2.print_fitness()
p1_2.print_fitness()

for x in range(20):
p1_2.next_genwf()
p1_2.print_ids()
p1_2.print_fitness()

p2_1 = Population(10)
p2_1.print_ids()
p2_1.print_fitness()

for x in range(20):
p2_1.next_genmo()
p2_1 = Population(10)
p2_1.print_ids()
p2_1.print_fitness()
p2_1.print_fitness()

p2_2 = Population(10, 0.3, 0.2)
p2_2.print_ids()
p2_2.print_fitness()
for x in range(20):
p2_1.next_genmo()
p2_1.print_ids()
p2_1.print_fitness()

for x in range(20):
p2_2.next_genmo()
p2_2 = Population(10, 0.3, 0.2)
p2_2.print_ids()
p2_2.print_fitness()
p2_2.print_fitness()

for x in range(20):
p2_2.next_genmo()
p2_2.print_ids()
p2_2.print_fitness()


if __name__ == '__main__':
main()

0 comments on commit e103bd6

Please sign in to comment.