Echidna
Echidna

Reputation: 164

How to reset the max generations in NEAT Python?

I'm currently working on a NEAT-Python AI that learns to play snake and flappy bird. I was able to locate the documentation but I can't seem to figure out how to change the max generation value.

My current NEAT.txt config file that's imported into the program looks like this:

 [NEAT]
fitness_criterion     = max
fitness_threshold     = 10000
pop_size              = 100
reset_on_extinction   = False

[DefaultGenome]
# node activation options
activation_default      = tanh
activation_mutate_rate  = 0.1
activation_options      = tanh

# node aggregation options
aggregation_default     = sum
aggregation_mutate_rate = 0.0
aggregation_options     = sum

# node bias options
bias_init_mean          = 0.0
bias_init_stdev         = 1.0
bias_max_value          = 30.0
bias_min_value          = -30.0
bias_mutate_power       = 0.5
bias_mutate_rate        = 0.7
bias_replace_rate       = 0.1

# genome compatibility options
compatibility_disjoint_coefficient = 1.0
compatibility_weight_coefficient   = 0.5

# connection add/remove rates
conn_add_prob           = 0.5
conn_delete_prob        = 0.5

# connection enable options
enabled_default         = True
enabled_mutate_rate     = 0.01

feed_forward            = True
initial_connection      = full_direct

# node add/remove rates
node_add_prob           = 0.2
node_delete_prob        = 0.2

# network parameters
num_hidden              = 1
num_inputs              = 3
num_outputs             = 1

# node response options
response_init_mean      = 1.0
response_init_stdev     = 0.0
response_max_value      = 30.0
response_min_value      = -30.0
response_mutate_power   = 0.0
response_mutate_rate    = 0.0
response_replace_rate   = 0.0

# connection weight options
weight_init_mean        = 0.0
weight_init_stdev       = 1.0
weight_max_value        = 30
weight_min_value        = -30
weight_mutate_power     = 0.5
weight_mutate_rate      = 0.8
weight_replace_rate     = 0.1

[DefaultSpeciesSet]
compatibility_threshold = 3.0

[DefaultStagnation]
species_fitness_func = max
max_stagnation       = 20
species_elitism      = 2

[DefaultReproduction]
elitism            = 2
survival_threshold = 0.2

Are there any new sections that I'm able to add to this existing code block that would allow me to input the max gen number?

Upvotes: 0

Views: 582

Answers (1)

user3243994
user3243994

Reputation: 76

Per the useful information within the question:

"I can't seem to figure out how to change the max generation value"

This can be accomplished by Defining the max_generations of the p objects run function.

# Run for up to 300 generations.
winner = p.run(eval_genomes, 300)

This is outlined in the working XOR example here

https://github.com/CodeReclaimers/neat-python/blob/c2b79c88667a1798bfe33c00dd8e251ef8be41fa/examples/xor/evolve-feedforward.py#L40

If we dive a little bit further into an example shown on https://neat-python.readthedocs.io/en/latest/xor_example.html We can see the a clear explanation as to how this should be defined.

we set a base configuration (defined in your provided config file)

# Load configuration.
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                     neat.DefaultSpeciesSet, neat.DefaultStagnation,
                     config_file)

Create our p object

# Create the population, which is the top-level object for a NEAT run.
p = neat.Population(config)

Add in some fancy reporting

# Add a stdout reporter to show progress in the terminal.
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)

Add in a checkpointer.

This will save a file every 5 generations. You will be able to find the saved checkpoint as a pickle in the same directory as your script with the name "neat-checkpoint-X"

p.add_reporter(neat.Checkpointer(5))

Finally run our evaluation for 300 generations

# Run for up to 300 generations.
winner = p.run(eval_genomes, 300)

Upvotes: 1

Related Questions