def from_json(config): """Load an instance of the NEAT algorithm from JSON. Arguments: config: the JSON dictionary loaded from file. Returns: an instance of the NEAT algorithm. """ population = Population(None, config['n_pops']) population.species = set( Species.from_json(s_config) for s_config in config['species']) population.creatures = [] for species in population.species: population.creatures += species.members return population
def test_json(self): """Test whether a species can be saved to and loaded from JSON.""" species = Species() species.assign_members([Creature(4, 1) for _ in range(100)]) species.allotted_offspring_quota = 93 dump = json.dumps(species.to_json()) species_load = Species.from_json(json.loads(dump)) self.assertEqual(len(species), len(species_load)) self.assertTrue( species.representative.distance(species_load.representative) < 1e-8 ) self.assertEqual(species.name, species_load.name) self.assertEqual(species.id, species_load.id) self.assertEqual(species.allotted_offspring_quota, species_load.allotted_offspring_quota) self.assertTrue( species.champion.distance(species_load.champion) < 1e-8)