def post_evaluate(self, config, population, species, best_genome): self.most_fit_genomes.append(copy.deepcopy(best_genome)) # Store the fitnesses of the members of each currently active species. species_stats = {} #species_cross_validation_stats = {} for sid, s in iteritems(species.species): species_stats[sid] = dict( (k, v.fitness) for k, v in iteritems(s.members)) ##species_cross_validation_stats[sid] = dict((k, v.cross_fitness) for ## k, v in iteritems(s.members)) self.generation_statistics.append(species_stats)
def distance(self, other, config): """ Returns the genetic distance between this genome and the other. This distance value is used to compute genome compatibility for speciation. """ # Compute node gene distance component. node_distance = 0.0 if self.nodes or other.nodes: disjoint_nodes = 0 # other有, self没有 的结点基因 for k2 in iterkeys(other.nodes): if k2 not in self.nodes: disjoint_nodes += 1 for k1, n1 in iteritems(self.nodes): n2 = other.nodes.get(k1) # self有, other 没有的 结点基因 if n2 is None: disjoint_nodes += 1 else: # Homologous genes compute their own distance value. node_distance += n1.distance(n2, config) max_nodes = max(len(self.nodes), len(other.nodes)) node_distance = (node_distance + (config.compatibility_disjoint_coefficient * disjoint_nodes)) / max_nodes # Compute connection gene differences. connection_distance = 0.0 if self.connections or other.connections: disjoint_connections = 0 for k2 in iterkeys(other.connections): if k2 not in self.connections: disjoint_connections += 1 for k1, c1 in iteritems(self.connections): c2 = other.connections.get(k1) if c2 is None: disjoint_connections += 1 else: # Homologous genes compute their own distance value. connection_distance += c1.distance(c2, config) max_conn = max(len(self.connections), len(other.connections)) connection_distance = (connection_distance + (config.compatibility_disjoint_coefficient * disjoint_connections)) / max_conn distance = node_distance + connection_distance return distance
def create(genome, config): """ Receives a genome and returns its phenotype (a RecurrentNetwork). """ genome_config = config.genome_config required = required_for_output(genome_config.input_keys, genome_config.output_keys, genome.connections) # Gather inputs and expressed connections. node_inputs = {} for cg in itervalues(genome.connections): if not cg.enabled: continue i, o = cg.key if o not in required and i not in required: continue if o not in node_inputs: node_inputs[o] = [(i, cg.weight)] else: node_inputs[o].append((i, cg.weight)) node_evals = [] for node_key, inputs in iteritems(node_inputs): node = genome.nodes[node_key] activation_function = genome_config.activation_defs.get(node.activation) aggregation_function = genome_config.aggregation_function_defs.get(node.aggregation) node_evals.append((node_key, activation_function, aggregation_function, node.bias, node.response, inputs)) return RecurrentNetwork(genome_config.input_keys, genome_config.output_keys, node_evals)
def configure_crossover(self, genome1, genome2, config): """ Configure a new genome by crossover from two parent genomes. """ assert isinstance(genome1.fitness, (int, float)) assert isinstance(genome2.fitness, (int, float)) # 保证 parent1.fit >= parent2.fit if genome1.fitness > genome2.fitness: parent1, parent2 = genome1, genome2 else: parent1, parent2 = genome2, genome1 # Inherit connection genes for key, cg1 in iteritems(parent1.connections): cg2 = parent2.connections.get(key) if cg2 is None: # Excess or disjoint gene: copy from the fittest parent. self.connections[key] = cg1.copy() else: # Homologous gene: combine genes from both parents. self.connections[key] = cg1.crossover(cg2) # Inherit node genes parent1_set = parent1.nodes parent2_set = parent2.nodes for key, ng1 in iteritems(parent1_set): ng2 = parent2_set.get(key) assert key not in self.nodes # 结点基因node gene 不能重复出现 if ng2 is None: # Extra gene: copy from the fittest parent self.nodes[key] = ng1.copy() else: # Homologous gene: combine genes from both parents. self.nodes[key] = ng1.crossover(ng2) # Create layer: cnn layer & all to all layer for i in range(config.num_cnn_layer): self.layer.append(['cnn', set()]) for i in range(config.num_cnn_layer, config.num_layer): self.layer.append(['fc', set()]) # Add layer according to nodes in new genome for node in iteritems(self.nodes): # node[0] 是基因结点的key,即 id, node[0] == node[1].key ??? # node[1].layer 结点所在层, self.layer[][1] 是结点集合set() self.layer[node[1].layer][1].add(node[1].key)
def mutate_delete_node(self, config): # Do nothing if there are no non-output nodes. # 只能删除隐藏层结点 available_nodes = [ k for k in iterkeys(self.nodes) if k not in config.output_keys ] if not available_nodes: return -1 del_key = choice(available_nodes) # 修改为 可以改变 第一层fc的结点 # if self.nodes[del_key].layer == config.num_cnn_layer: # return -1 # If there is only one node if len(self.layer[self.nodes[del_key].layer][1]) <= 1: return -1 connections_to_delete = set() for k, v in iteritems(self.connections): # v=(a,b), either del_key == a or b should be deleted if del_key in v.key: connections_to_delete.add(v.key) for key in connections_to_delete: if key[0] == del_key: # del_key是 输入 idx = -1 for i in range(len(self.nodes[key[1]].in_nodes)): if self.nodes[key[1]].in_nodes[i] == key[0]: idx = i break assert idx > -1 self.nodes[key[1]].in_nodes = self.nodes[key[1]].in_nodes[:idx] + \ self.nodes[key[1]].in_nodes[idx+1 : ] # TODO: 保证 结点至少有一个kernal if len(self.nodes[key[1]].in_nodes) > 0: self.nodes[key[1]].kernal = self.nodes[key[1]].kernal[:idx] + \ self.nodes[key[1]].kernal[idx+1:] else: # key[1] == del_key self.nodes[del_key].in_nodes = [] self.nodes[del_key].kernal = self.nodes[del_key].kernal[:1] del self.connections[key] #i = self.nodes[del_key].layer self.layer[self.nodes[del_key].layer][1].remove(del_key) del self.nodes[del_key] return del_key
def update(self, species_set, generation): """ Required interface method. Updates species fitness history information, checking for ones that have not improved in max_stagnation generations, and - unless it would result in the number of species dropping below the configured species_elitism parameter if they were removed, in which case the highest-fitness species are spared - returns a list with stagnant species marked for removal. """ species_data = [] for sid, s in iteritems(species_set.species): if s.fitness_history: prev_fitness = max(s.fitness_history) else: prev_fitness = -sys.float_info.max s.fitness = self.species_fitness_func(s.get_fitnesses()) s.fitness_history.append(s.fitness) s.adjusted_fitness = None if prev_fitness is None or s.fitness > prev_fitness: s.last_improved = generation species_data.append((sid, s)) # Sort in ascending fitness order. species_data.sort(key=lambda x: x[1].fitness) result = [] species_fitnesses = [] num_non_stagnant = len(species_data) for idx, (sid, s) in enumerate(species_data): # Override stagnant state if marking this species as stagnant would # result in the total number of species dropping below the limit. # Because species are in ascending fitness order, less fit species # will be marked as stagnant first. stagnant_time = generation - s.last_improved is_stagnant = False if num_non_stagnant > self.stagnation_config.species_elitism: is_stagnant = stagnant_time >= self.stagnation_config.max_stagnation if (len(species_data) - idx) <= self.stagnation_config.species_elitism: is_stagnant = False if is_stagnant: num_non_stagnant -= 1 result.append((sid, s, is_stagnant)) species_fitnesses.append(s.fitness) return result
def __init__(self, config, initial_state=None): self.checkpointer = Checkpointer(generation_interval=1, time_interval_seconds=None, filename_prefix='./checkpoints/neat-checkpoint-') self.reporters = ReporterSet() self.config = config stagnation = config.stagnation_type(config.stagnation_config, self.reporters) if initial_state is None: self.reproduction = config.reproduction_type(config.reproduction_config, self.reporters, stagnation) if config.fitness_criterion == 'max': self.fitness_criterion = max elif config.fitness_criterion == 'min': self.fitness_criterion = min elif config.fitness_criterion == 'mean': self.fitness_criterion = mean elif not config.no_fitness_termination: raise RuntimeError( "Unexpected fitness_criterion: {0!r}".format(config.fitness_criterion)) if initial_state is None: # Create a population from scratch, then partition into species. self.population = self.reproduction.create_new(config.genome_type, config.genome_config, config.pop_size) #print('population in_nodes ',self.population.nodes[0].in_nodes) self.species = config.species_set_type(config.species_set_config, self.reporters) self.generation = 0 self.species.speciate(config, self.population, self.generation) else: self.population, self.species, self.generation = initial_state genomes = list(iteritems(self.population)) start_idx=0 for genome_id, genome in genomes: if genome_id > start_idx: start_idx = genome_id self.reproduction = config.reproduction_type(config.reproduction_config, self.reporters, stagnation, start_idx + 1) self.best_genome = []
def __str__(self): s = "Key: {0}\nFitness: {1}\nNodes:".format(self.key, self.fitness) for k, ng in iteritems(self.nodes): s += "\n\t{0} {1!s}".format(k, ng) s += "\nConnections:" connections = list(self.connections.values()) connections.sort() for c in connections: s += "\n\t" + str(c) s += "\nLayers:" for i in range(len(self.layer)): s += "\n\t" + self.layer[i][0] + ": " l = list(self.layer[i][1]) l.sort() for node in l: s += " {0}".format(node) return s
def __init__(self, name, **default_dict): self.name = name for n, default in iteritems(default_dict): self._config_items[n] = [self._config_items[n][0], default] for n in iterkeys(self._config_items): setattr(self, n + "_name", self.config_item_name(n))
def speciate(self, config, population, generation): """ Place genomes into species by genetic similarity. Note that this method assumes the current representatives of the species are from the old generation, and that after speciation has been performed, the old representatives should be dropped and replaced with representatives from the new generation. If you violate this assumption, you should make sure other necessary parts of the code are updated to reflect the new behavior. """ assert isinstance(population, dict) compatibility_threshold = self.species_set_config.compatibility_threshold # Find the best representatives for each existing species. unspeciated = set(iterkeys(population)) distances = GenomeDistanceCache(config.genome_config) new_representatives = {} new_members = {} for sid, s in iteritems(self.species): candidates = [] for gid in unspeciated: g = population[gid] d = distances(s.representative, g) candidates.append((d, g)) # The new representative is the genome closest to the current representative. ignored_rdist, new_rep = min(candidates, key=lambda x: x[0]) new_rid = new_rep.key new_representatives[sid] = new_rid new_members[sid] = [new_rid] unspeciated.remove(new_rid) # Partition population into species based on genetic similarity. while unspeciated: gid = unspeciated.pop() g = population[gid] # Find the species with the most similar representative. candidates = [] for sid, rid in iteritems(new_representatives): rep = population[rid] d = distances(rep, g) if d < compatibility_threshold: candidates.append((d, sid)) if candidates: ignored_sdist, sid = min(candidates, key=lambda x: x[0]) new_members[sid].append(gid) else: # No species is similar enough, create a new species, using # this genome as its representative. sid = next(self.indexer) new_representatives[sid] = gid new_members[sid] = [gid] # Update species collection based on new speciation. self.genome_to_species = {} for sid, rid in iteritems(new_representatives): s = self.species.get(sid) if s is None: s = Species(sid, generation) self.species[sid] = s members = new_members[sid] for gid in members: self.genome_to_species[gid] = sid member_dict = dict((gid, population[gid]) for gid in members) s.update(population[rid], member_dict) gdmean = mean(itervalues(distances.distances)) gdstdev = stdev(itervalues(distances.distances)) self.reporters.info( 'Mean genetic distance {0:.3f}, standard deviation {1:.3f}'.format( gdmean, gdstdev))
def run(self, fitness_function, n=None): """ Runs NEAT's genetic algorithm for at most n generations. If n is None, run until solution is found or extinction occurs. The user-provided fitness_function must take only two arguments: 1. The population as a list of (genome id, genome) tuples. 2. The current configuration object. The return value of the fitness function is ignored, but it must assign a Python float to the `fitness` member of each genome. The fitness function is free to maintain external state, perform evaluations in parallel, etc. It is assumed that fitness_function does not modify the list of genomes, the genomes themselves (apart from updating the fitness member), or the configuration object. """ if self.config.no_fitness_termination and (n is None): raise RuntimeError("Cannot have no generational limit with no fitness termination") if self.generation > 0: # start from saved_state self.reporters.start_generation(self.generation) # Gather and report statistics. best = None for g in itervalues(self.population): if best is None or g.fitness > best.fitness: best = g self.reporters.post_evaluate(self.config, self.population, self.species, best) # Track the best genome ever seen. self.best_genome.append((best, best.fitness)) # Create the next generation from the current generation. self.population = self.reproduction.reproduce(self.config, self.species, self.config.pop_size, self.generation) # Check for complete extinction. if not self.species.species: self.reporters.complete_extinction() # If requested by the user, create a completely new population, # otherwise raise an exception. if self.config.reset_on_extinction: self.population = self.reproduction.create_new(self.config.genome_type, self.config.genome_config, self.config.pop_size) else: raise CompleteExtinctionException() # Divide the new population into species. self.species.speciate(self.config, self.population, self.generation) self.reporters.end_generation(self.config, self.population, self.species) self.generation += 1 k = 0 while n is None or k < n: k += 1 self.checkpointer.start_generation(self.generation) self.reporters.start_generation(self.generation) # Evaluate all genomes using the user-provided function. fitness_function(list(iteritems(self.population)), self.config) # Gather and report statistics. best = None #idxx = 0 for g in itervalues(self.population): #idxx += 1 #print(g.fitness) #if g.fitness is None: #print(idxx) if best is None or g.fitness > best.fitness: best = g self.reporters.post_evaluate(self.config, self.population, self.species, best) # Track the best genome ever seen. self.best_genome.append((best, best.fitness)) # if self.best_genome is None or best.fitness > self.best_genome.fitness: # # self.best_genome = best if not self.config.no_fitness_termination: # End if the fitness threshold is reached. fv = self.fitness_criterion(g.fitness for g in itervalues(self.population)) if fv >= self.config.fitness_threshold: self.reporters.found_solution(self.config, self.generation, best) break #print(self.generation, self.checkpointer.last_generation_checkpoint, self.checkpointer.generation_interval ) if self.generation - self.checkpointer.last_generation_checkpoint >= \ self.checkpointer.generation_interval: print('save checkpoint') self.checkpointer.end_generation(self.config, self.population, self.species) # Create the next generation from the current generation. self.population = self.reproduction.reproduce(self.config, self.species, self.config.pop_size, self.generation) # Check for complete extinction. if not self.species.species: self.reporters.complete_extinction() # If requested by the user, create a completely new population, # otherwise raise an exception. if self.config.reset_on_extinction: self.population = self.reproduction.create_new(self.config.genome_type, self.config.genome_config, self.config.pop_size) else: raise CompleteExtinctionException() # Divide the new population into species. self.species.speciate(self.config, self.population, self.generation) self.reporters.end_generation(self.config, self.population, self.species) self.generation += 1 if self.config.no_fitness_termination: self.reporters.found_solution(self.config, self.generation, self.best_genome[-1][0]) return self.best_genome
def reproduce(self, config, species, pop_size, generation): """ Handles creation of genomes, either from scratch or by sexual or asexual reproduction from parents. """ # TODO: I don't like this modification of the species and stagnation objects, # because it requires internal knowledge of the objects. # Filter out stagnated species, collect the set of non-stagnated # species members, and compute their average adjusted fitness. # The average adjusted fitness scheme (normalized to the interval # [0, 1]) allows the use of negative fitness values without # interfering with the shared fitness scheme. all_fitnesses = [] remaining_species = [] for stag_sid, stag_s, stagnant in self.stagnation.update( species, generation): if stagnant: self.reporters.species_stagnant(stag_sid, stag_s) else: all_fitnesses.extend(m.fitness for m in itervalues(stag_s.members)) remaining_species.append(stag_s) # The above comment was not quite what was happening - now getting fitnesses # only from members of non-stagnated species. #print("#########################################################################{0}".format(remaining_species.__len__())) # No species left. if not remaining_species: species.species = {} return {} # was [] # Find minimum/maximum fitness across the entire population, for use in # species adjusted fitness computation. min_fitness = min(all_fitnesses) max_fitness = max(all_fitnesses) #print('max_fitness ',max_fitness) # Do not allow the fitness range to be zero, as we divide by it below. # TODO: The ``1.0`` below is rather arbitrary, and should be configurable. fitness_range = max(1.0, max_fitness - min_fitness) for afs in remaining_species: # Compute adjusted fitness. msf = mean([m.fitness for m in itervalues(afs.members)]) af = (msf - min_fitness) / fitness_range afs.adjusted_fitness = af adjusted_fitnesses = [s.adjusted_fitness for s in remaining_species] avg_adjusted_fitness = mean(adjusted_fitnesses) # type: float self.reporters.info( "Average adjusted fitness: {:.3f}".format(avg_adjusted_fitness)) # Compute the number of new members for each species in the new generation. previous_sizes = [len(s.members) for s in remaining_species] min_species_size = self.reproduction_config.min_species_size # Isn't the effective min_species_size going to be max(min_species_size, # self.reproduction_config.elitism)? That would probably produce more accurate tracking # of population sizes and relative fitnesses... doing. TODO: document. min_species_size = max(min_species_size, self.reproduction_config.elitism) spawn_amounts = self.compute_spawn(adjusted_fitnesses, previous_sizes, pop_size, min_species_size) new_population = {} species.species = {} for spawn, s in zip(spawn_amounts, remaining_species): # If elitism is enabled, each species always at least gets to retain its elites. spawn = max(spawn, self.reproduction_config.elitism) assert spawn > 0 # The species has at least one member for the next generation, so retain it. old_members = list(iteritems(s.members)) s.members = {} species.species[s.key] = s # Sort members in order of descending fitness. old_members.sort(reverse=True, key=lambda x: x[1].fitness) # Transfer elites to new generation. if self.reproduction_config.elitism > 0: #print('elitism ', self.reproduction_config.elitism) # i是个体的id, m 是 个体 for i, m in old_members[:self.reproduction_config.elitism]: new_population[i] = m print('fitness ', m.fitness, ' ', i) spawn -= 1 if spawn <= 0: continue # Only use the survival threshold fraction to use as parents for the next generation. repro_cutoff = int( math.ceil(self.reproduction_config.survival_threshold * len(old_members))) # Use at least two parents no matter what the threshold fraction result is. repro_cutoff = max(repro_cutoff, 2) old_members = old_members[:repro_cutoff] # Randomly choose parents and produce the number of offspring allotted to the species. while spawn > 0: spawn -= 1 parent1_id, parent1 = random.choice(old_members) parent2_id, parent2 = random.choice(old_members) # Note that if the parents are not distinct, crossover will produce a # genetically identical clone of the parent (but with a different ID). gid = next(self.genome_indexer) child = config.genome_type(gid) child.configure_crossover(parent1, parent2, config.genome_config) child.mutate(config.genome_config) new_population[gid] = child self.ancestors[gid] = (parent1_id, parent2_id) return new_population