Example #1
0
    def __init__(self, seed_creature=None, n_pops=150):
        """Create a population for NEAT.

        Arguments:
            seed_creature: The creature that will be used to creature the
                           initial generation.
            n_pops: How many creatures should be in the population.
        """
        self.n_pops = n_pops
        self.species = set()

        if seed_creature:
            self.creatures = [seed_creature.copy() for _ in range(n_pops)]

            genesis_species = Species()
            genesis_species.assign_members(self.creatures)
            self.species.add(genesis_species)
        else:
            self.creatures = []
Example #2
0
    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)