Skip to content

Commit 08b1abb

Browse files
Remove naturally extinct species from stagnation tracking.
Fixed incorrect species fitness tracking.
1 parent de22762 commit 08b1abb

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

neat/reproduction.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import random
33

44
from neat.indexer import Indexer
5-
from neat.six_util import iteritems, itervalues
5+
from neat.six_util import iteritems, iterkeys, itervalues
66

77
# TODO: Provide some sort of optional cross-species performance criteria, which
88
# are then used to control stagnation and possibly the mutation rate configuration.
@@ -52,8 +52,8 @@ def create_new(self, genome_type, genome_config, num_genomes):
5252
return new_genomes
5353

5454
def reproduce(self, config, species, pop_size):
55-
# TODO: I don't like this modification of the species object,
56-
# because it requires internal knowledge of the object.
55+
# TODO: I don't like this modification of the species and stagnation objects,
56+
# because it requires internal knowledge of the objects.
5757

5858
# Filter out stagnated species and collect the set of non-stagnated species members.
5959
num_remaining = 0
@@ -148,6 +148,11 @@ def reproduce(self, config, species, pop_size):
148148
new_population[gid] = child
149149
self.ancestors[gid] = (parent1_id, parent2_id)
150150

151+
# Remove empty species from the stagnation tracking.
152+
keys = list(iterkeys(self.stagnation.stagnant_counts))
153+
for sid in keys:
154+
if sid not in species.species:
155+
self.stagnation.remove(sid)
151156

152157
# Sort species by ID (purely for ease of reading the reported list).
153158
# TODO: This should probably be done by the species object.

neat/stagnation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class DefaultStagnation(object):
1010
@classmethod
1111
def parse_config(cls, param_dict):
1212
config = {'species_fitness_func': 'mean',
13-
'max_stagnation': 15}
13+
'max_stagnation': 15}
1414
config.update(param_dict)
1515

1616
return config
@@ -53,10 +53,10 @@ def update(self, species):
5353
prev_fitness = self.previous_fitnesses.get(sid, -sys.float_info.max)
5454
if fitness > prev_fitness:
5555
scount = 0
56+
self.previous_fitnesses[sid] = fitness
5657
else:
5758
scount = self.stagnant_counts.get(sid, 0) + 1
5859

59-
self.previous_fitnesses[sid] = fitness
6060
self.stagnant_counts[sid] = scount
6161

6262
is_stagnant = scount >= self.max_stagnation

0 commit comments

Comments
 (0)