Skip to content

Commit

Permalink
probrem heavywatal#14 solving (heavywatal#19)
Browse files Browse the repository at this point in the history
* heavywatal#14 cleaned

* heavywatal#14 print fitness

* solved heavywatal#14

* heavywatal#14 modified

* heavywatal#14 modified

* solved heavywatal#14

* Reduce calculation and rename variables
  • Loading branch information
hamazaki1990 authored and heavywatal committed May 2, 2017
1 parent f1fdee2 commit a07961e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
13 changes: 10 additions & 3 deletions hamazaki1990/individual.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
class Individual:
def __init__(self, n):
def __init__(self, n, f=1.0):
self._id = n
self._fitness = f

def get_id(self):
return self._id

def get_fitness(self):
return self._fitness

def __repr__(self):
return str(self._id)


ind = Individual(42)
print(ind.get_id())
print(ind)
print(str(ind))
print(ind.get_fitness())

ind2 = Individual(30, 0.8)
print(ind2.get_id())
print(ind2.get_fitness())
75 changes: 61 additions & 14 deletions hamazaki1990/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,77 @@
from individual import Individual


def roulettechoice(individuals, cumsum_fitness):
r = random.uniform(0.0, max(cumsum_fitness))
for i in range(len(cumsum_fitness)):
if r < cumsum_fitness[i]:
return individuals[i]


class Population:
def __init__(self, size):
self._inds = [Individual(x) for x in range(size)]
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)]
self._inds = mutant_inds + wild_inds

def print_ids(self):
print(self._inds)

def print_fitness(self):
fitness = [x.get_fitness() for x in self._inds]
print(fitness)

def next_genwf(self):
n = len(self._inds)
self._inds = [self._inds[random.randrange(n)] for x in range(n)]
fitness = [x.get_fitness() for x in self._inds]
size = len(self._inds)
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))
self._inds = next_generation

def next_genmo(self):
n = len(self._inds)
self._inds[random.randrange(n)] = self._inds[random.randrange(n)]
fitness = [x.get_fitness() for x in self._inds]
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)


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

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

p1_2 = Population(10, 0.3, 0.2)
p1_2.print_ids()
p1_2.print_fitness()

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


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

for t in range(20):
p1.print_ids()
p1.next_genwf()
for x in range(20):
p2_1.next_genmo()
p2_1.print_ids()
p2_1.print_fitness()

p2 = Population(10)
p2_2 = Population(10, 0.3, 0.2)
p2_2.print_ids()
p2_2.print_fitness()

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

0 comments on commit a07961e

Please sign in to comment.