How to retrieve species Id in NEAT to plot species over generations?

38 Views Asked by At

I have done evolution of a population artificial creatures over 1000 generations and saved the checkpoint data.

So, now I would like to retrieve the species number that were produced in each generation and plot it. However, it seems like so difficult, even after trying several ways.

From NEAT library, on how to retrieve species ID

I have tried above method, heres the code:

import os
import pickle
import matplotlib.pyplot as plt
import glob
import creature.plot2 as plot
import pybullet as p
import neat

local_dir = os.path.dirname(__file__)
file_path = glob.glob(local_dir + "/*.pkl")[0]

# Load the saved genomes
with open(file_path, "rb") as f:
    Genomes = pickle.load(f)

# Initialize a list to store the number of species at each generation
num_species_over_generations = []

# Create a NEAT population and restore the checkpoint
pop=neat.Checkpointer.restore_checkpoint(os.path.dirname(__file__)+"/neat-checkpoint-999")

# Iterate through the generations
for generation in Genomes:
    # Calculate the number of unique species in the current generation
    unique_species = set()
    
    for genome, _ in generation:
        individual_id = Genomes[0]  # Assuming genome.key represents the genome's ID/key
        species_id = pop.get_species_id(individual_id)
        unique_species.add(species_id)
    
    # Append the count of unique species to the list
    num_species_over_generations.append(len(unique_species))

# Now you have a list 'num_species_over_generations' containing the number of species at each generation

# Plot the number of species over generations
plt.plot(range(len(num_species_over_generations)), num_species_over_generations, marker='o')
plt.xlabel("Generation")
plt.ylabel("Number of Species")
plt.title("Number of Species Over Generations")
plt.grid(True)
plt.show()

but the error says that: 'Population' object has no attribute 'get_species_id' File "/Users/sitiaisyahjaafar/Desktop/niche_evo_single_species/exp_data/radar4.py/20231003_110803/species-gen.py", line 29, in species_id = pop.get_species_id(individual_id)

I have set my population to be the checkpoint of population that have been run over 1000 generations that I saved. And during evolution, the data of reporting.StdOutReporter() were saved in the pop.

I really desperately need to see how the species changes through the generations in my experiment. Has someone ever tried it?

0

There are 0 best solutions below