Skip to content

Commit

Permalink
Added unique best genomes method to StatisticsReporter.
Browse files Browse the repository at this point in the history
Added refactoring notes.
Randomly inherit ConnectionGene traits from parents instead of copying one randomly selected parent's gene.
  • Loading branch information
CodeReclaimers committed Feb 23, 2016
1 parent a1f5884 commit 0e4b57b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
14 changes: 9 additions & 5 deletions neat/genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ def split(self, innovation_indexer, node_id):
def copy(self):
return ConnectionGene(self.innovation_id, self.in_node_id, self.out_node_id, self.weight, self.enabled)

def is_same_innov(self, cg):
return self.innovation_id == cg.innovation_id
def is_same_innov(self, other):
return self.innovation_id == other.innovation_id

def get_child(self, cg):
# TODO: average both weights (Stanley, p. 38)
return random.choice((self, cg)).copy()
def get_child(self, other):
""" Creates a new ConnectionGene randomly inheriting attributes from its parents."""
assert self.innovation_id == other.innovation_id
cg = ConnectionGene(self.innovation_id, self.in_node_id, self.out_node_id,
random.choice((self.weight, other.weight)),
random.choice((self.enabled, other.enabled)))
return cg
4 changes: 4 additions & 0 deletions neat/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def clear(self):
self.next_id = 1


# TODO: Why not just use (in, out) as a unique innovation index? That's what
# the current scheme ends up doing anyway. If the scheme needs to be configurable,
# then the key member of connection genes should be used everywhere instead of
# having hard-coded (input, output) tuple keys sprinkled all over the code.
class InnovationIndexer(object):
def __init__(self, first):
self.indexer = Indexer(first)
Expand Down
12 changes: 12 additions & 0 deletions neat/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ def get_average_fitness(self):

return avg_fitness

def best_unique_genomes(self, n):
"""Returns the most n fit genomes, with no duplication."""
best_unique = {}
for g in self.most_fit_genomes:
best_unique[g.ID] = g
best_unique = list(best_unique.values())

def key(g):
return g.fitness

return sorted(best_unique, key=key, reverse=True)[:n]

def best_genomes(self, n):
"""Returns the n most fit genomes ever seen."""
def key(g):
Expand Down

0 comments on commit 0e4b57b

Please sign in to comment.