

def test_generate(self):
    '\n        Ensures that the generate iterable is used to create new generations.\n        '
    start_pop = [1, 2, 3]

    def fitness(genome):
        '\n            For illustrative purposes.\n            '
        return genome

    def halt(population, generation_count):
        '\n            Halts the algorithm after ten populations.\n            '
        return (generation_count == 10)

    class Generate(object):
        '\n            An iterable for producing new offspring.\n            '

        def __init__(self, parent_population):
            '\n                @param parent_population: the parent genomes.\n                '
            self.counter = 0
            self.size = len(parent_population)
            self.parents = parent_population

        def __iter__(self):
            return self

        def next(self):
            '\n                For testing purposes only.\n                '
            if (self.counter < self.size):
                (pos, self.counter) = (self.counter, (self.counter + 1))
                return (self.parents[pos] + 1)
            else:
                raise StopIteration()
    ga = genetic_algorithm(start_pop, fitness, Generate, halt)
    actual = [p for p in ga]
    expected = [[(3 + i), (2 + i), (1 + i)] for i in range(10)]
    self.assertEqual(actual, expected, ('Actual: %r Expected: %r' % (actual, expected)))

dataset/ETHPy150Open ntoll/foox/test/test_ga.py TestGeneticAlgorithm.test_generate/original