Last active
December 12, 2016 01:59
-
-
Save sli/9104343ef9abc6ecbc3be002cc683f23 to your computer and use it in GitHub Desktop.
A (kinda bad) MarI/O pool parser.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# state pool file format: | |
# | |
# generation | |
# max_fitness | |
# species count | |
# for all species: | |
# top_fitness | |
# staleness | |
# genome count | |
# for all genomes: | |
# fitness | |
# max_neuron | |
# for all mutations: | |
# mutation | |
# rate | |
# "done" | |
# gene count | |
# for all genes: | |
# into out weight innovation enabled | |
def parse_pool(pool, with_genes=False): | |
p_data = {} | |
p_data['generation'] = int(pool.readline().rstrip()) | |
p_data['max_fitness'] = float(pool.readline().rstrip()) | |
p_data['species_count'] = int(pool.readline().rstrip()) | |
p_data['species'] = [] | |
for species in range(p_data['species_count']): | |
species = {} | |
species['top_fitness'] = pool.readline().rstrip() | |
species['staleness'] = pool.readline().rstrip() | |
species['genome_count'] = int(pool.readline().rstrip()) | |
species['genomes'] = [] | |
for genome in range(species['genome_count']): | |
genome = {} | |
genome['fitness'] = float(pool.readline().rstrip()) | |
genome['max_neuron'] = pool.readline().rstrip() | |
species['genomes'].append(genome) | |
i = 4 | |
while True: | |
i += 1 | |
line = pool.readline().rstrip() | |
if line == 'done': | |
break | |
genome['gene_count'] = int(pool.readline().rstrip()) | |
if with_genes: | |
genome['genes'] = [] | |
for i in range(genome['gene_count']): | |
gene = {} | |
gene_raw = pool.readline().rstrip().split(' ') | |
gene['into'] = gene_raw[0] | |
gene['out'] = gene_raw[1] | |
gene['weight'] = gene_raw[2] | |
gene['innovation'] = gene_raw[3] | |
gene['enabled'] = True if gene_raw[4] == 1 else False | |
genome['genes'].append(gene) | |
else: | |
for i in range(genome['gene_count']): | |
next(pool) | |
p_data['species'].append(species) | |
return p_data | |
if __name__ == '__main__': | |
import sys | |
import pprint | |
if len(sys.argv) < 2: | |
print('Usage: python poolparse.py <state file> [-g]') | |
sys.exit(1) | |
with_genes = True if sys.argv[-1] == '-g' and len(sys.argv) > 2 else False | |
with open(sys.argv[1]) as f: | |
g = parse_pool(f, with_genes=with_genes) | |
pp = pprint.PrettyPrinter(indent=2) | |
pp.pprint(g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment