Skip to content

Commit 8accaab

Browse files
Still more refactoring.
Moved genome-related configuration from the Config object to DefaultGenomeConfig. Config now only handles generic high-level configuration explicitly. Component-specific configuration is handled by the user-specified types. Updated memory example so that it runs. Pass around configuration object as necessary (this needs to be cleaned up later, it's too cumbersome now).
1 parent 8b44306 commit 8accaab

11 files changed

+385
-468
lines changed

examples/memory/nn_config

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,53 @@
44
# tasks in the NEAT algorithm. If you use a non-default class here, you
55
# must register it with your Config instance before loading the config file.
66
[Types]
7-
stagnation_type = DefaultStagnation
8-
reproduction_type = DefaultReproduction
7+
genome_type = DefaultGenome
8+
reproduction_type = DefaultReproduction
9+
stagnation_type = DefaultStagnation
910

10-
[phenotype]
11-
input_nodes = 2
12-
hidden_nodes = 0
13-
output_nodes = 1
14-
initial_connection = partial 0.5
15-
max_weight = 30
16-
min_weight = -30
17-
feedforward = 0
18-
activation_functions = clamped sigmoid tanh my_sinc_function
19-
aggregation_functions = sum
20-
weight_stdev = 1.0
11+
# The `NEAT` section specifies parameters particular to the NEAT algorithm
12+
# or the experiment itself.
13+
[NEAT]
14+
pop_size = 150
15+
max_fitness_threshold = -0.05
16+
reset_on_extinction = 0
2117

22-
[genetic]
23-
pop_size = 150
24-
max_fitness_threshold = -0.05
25-
prob_add_conn = 0.5
26-
prob_add_node = 0.5
27-
prob_delete_conn = 0.25
28-
prob_delete_node = 0.25
29-
prob_mutate_bias = 0.8
30-
bias_mutation_power = 0.5
31-
prob_mutate_response = 0.8
32-
response_mutation_power = 0.5
33-
prob_mutate_weight = 0.8
34-
prob_replace_weight = 0.1
35-
weight_mutation_power = 0.5
36-
prob_mutate_activation = 0.002
37-
prob_mutate_aggregation = 0.0
38-
prob_toggle_link = 0.01
39-
reset_on_extinction = 1
40-
41-
[genotype compatibility]
18+
[DefaultGenome]
19+
num_inputs = 2
20+
num_hidden = 0
21+
num_outputs = 1
22+
initial_connection = partial 0.5
23+
feed_forward = 0
24+
activation = clamped sigmoid tanh my_sinc_function
25+
aggregation = sum
26+
conn_add_prob = 0.5
27+
conn_delete_prob = 0.25
28+
node_add_prob = 0.5
29+
node_delete_prob = 0.25
30+
bias_mutate_prob = 0.8
31+
bias_mutate_power = 0.5
32+
response_mutate_prob = 0.8
33+
response_mutate_power = 0.5
34+
weight_max = 30
35+
weight_min = -30
36+
weight_mean = 0.0
37+
weight_stdev = 1.0
38+
weight_mutate_prob = 0.8
39+
weight_replace_prob = 0.1
40+
weight_mutate_power = 0.5
41+
activation_mutate_prob = 0.002
42+
aggregation_mutate_prob = 0.0
43+
link_toggle_prob = 0.01
4244
compatibility_threshold = 3.0
4345
excess_coefficient = 1.0
4446
disjoint_coefficient = 1.0
4547
weight_coefficient = 0.5
4648

4749
[DefaultStagnation]
48-
species_fitness_func = median
49-
max_stagnation = 10
50+
species_fitness = median
51+
max_stagnation = 10
5052

5153
[DefaultReproduction]
52-
elitism = 2
53-
survival_threshold = 0.2
54+
elitism = 2
55+
survival_threshold = 0.2
5456

examples/memory/nn_evolve.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
import os
1313
import random
1414

15-
from neat import nn, population, statistics, parallel, activation_functions
15+
from neat.config import Config
16+
from neat import nn, population, statistics, parallel
1617

1718
# num_tests is the number of random examples each network is tested against.
1819
num_tests = 16
1920
# N is the length of the test sequence.
2021
N = 4
2122

2223

23-
def eval_fitness(genome_id, genome):
24-
net = nn.create_recurrent_phenotype(genome)
24+
def eval_fitness(genome_id, genome, config):
25+
net = nn.create_recurrent_phenotype(genome, config)
2526

2627
error = 0.0
2728
for _ in range(num_tests):
@@ -43,25 +44,30 @@ def eval_fitness(genome_id, genome):
4344
return -(error / (N * num_tests)) ** 0.5
4445

4546

46-
def eval_genomes(genomes):
47+
def eval_genomes(genomes, config):
4748
for genome_id, genome in genomes:
48-
genome.fitness = eval_fitness(genome_id, genome)
49+
genome.fitness = eval_fitness(genome_id, genome, config)
4950

5051

5152
# Demonstration of how to add your own custom activation function.
5253
def sinc(x):
5354
return 1.0 if x == 0 else math.sin(x) / x
5455

55-
# This sinc function will be available if my_sinc_function is included in the
56-
# config file activation_functions option under the pheotype section.
57-
# Note that sinc is not necessarily useful for this example, it was chosen
58-
# arbitrarily just to demonstrate adding a custom activation function.
59-
activation_functions.add('my_sinc_function', sinc)
60-
6156

6257
def run():
58+
59+
# Determine path to configuration file.
6360
local_dir = os.path.dirname(__file__)
64-
pop = population.Population(os.path.join(local_dir, 'nn_config'))
61+
config_path = os.path.join(local_dir, 'nn_config')
62+
config = Config(config_path)
63+
64+
# This sinc function will be available if my_sinc_function is included in the
65+
# config file activation_functions option under the pheotype section.
66+
# Note that sinc is not necessarily useful for this example, it was chosen
67+
# arbitrarily just to demonstrate adding a custom activation function.
68+
config.genome_config.activation_defs.add('my_sinc_function', sinc)
69+
70+
pop = population.Population(config)
6571
#pe = parallel.ParallelEvaluator(4, eval_fitness)
6672
#pop.run(pe.evaluate, 1000)
6773
pop.run(eval_genomes, 1000)

0 commit comments

Comments
 (0)