Пример #1
0
    def test_get_elites_max_depth(self):
        lineages = build_population(
            [
                [2, 8, 9, 9, 8],
                [9, 3, 8, 9, 7],
                [8, 8, 8, 4, 6],
                [7, 8, 9, 8, 5],
                [7, 6, 8, 7, 5],
                [6, 5, 7, 7, 4],
                [5, 5, 6, 7, 5],
                [4, 4, 5, 8, 5],
                [4, 4, 9, 8, 5],
                [4, 4, 8, 8, 5],
                [4, 4, 7, 8, 5],
                [4, 4, 6, 8, 5],
                [4, 4, 8, 8, 5],
                [4, 4, 9, 8, 5],
            ]
        )

        elites = sorted(lineages.get_elites(0), key=lambda trial: trial.id)
        assert [trial.objective.value for trial in elites] == [2, 8, 9, 9, 8]

        elites = sorted(lineages.get_elites(2), key=lambda trial: trial.id)
        assert [trial.objective.value for trial in elites] == [2, 3, 8, 4, 6]

        elites = sorted(lineages.get_elites(5), key=lambda trial: trial.id)
        assert [trial.objective.value for trial in elites] == [2, 3, 7, 4, 4]
Пример #2
0
    def test_get_elites_various_depths(self):

        lineages = build_population([
            [2, 8, 9, 9, 8],
            [9, 3, 8, 9, 7],
            [8, 8, 8, 4, 6],
            [7, 8, 9, 8, 5],
            [7, 6, 8, 7, 5],
            [6, 5, 7, 7, 4],
            [5, 5, 6, 7, 5],
            [4, 4, 5, 8, 5],
            [4, 4, 9, 8, 5],
            [4, 4, 8, 8, 5],
            [4, 4, 7, 8, 5],
            [4, 4, 6, 8, 5],
            [4, 4, 8, 8, 5],
            [4, 4, 9, 8, 5],
        ])

        elites = sorted(lineages.get_elites(), key=lambda trial: trial.id)
        assert len(elites) == 5
        assert elites[0].id == "lineage-0-0"
        assert elites[0].objective.value == 2

        assert elites[1].id == "lineage-1-1"
        assert elites[1].objective.value == 3

        assert elites[2].id == "lineage-2-7"
        assert elites[2].objective.value == 5

        assert elites[3].id == "lineage-3-2"
        assert elites[3].objective.value == 4

        assert elites[4].id == "lineage-4-5"
        assert elites[4].objective.value == 4
Пример #3
0
 def test_get_trials_at_depth_given_existing_trial(self):
     population_size = 5
     generations = 10
     lineages = build_population([
         list(range(population_size)) for generation in range(generations)
     ])
     for depth in [0, 1, 5, 9]:
         lineage_index = random.choice(range(population_size))
         trial = TrialStub(id=f"lineage-{lineage_index}-{depth}")
         compare_generations(lineages.get_trials_at_depth(trial),
                             population_size, depth)
Пример #4
0
    def test_get_trials_at_depth_given_depth(self):
        population_size = 5
        generations = 10
        lineages = build_population([
            list(range(population_size)) for generation in range(generations)
        ])
        for depth in [0, 1, 5, 9]:
            compare_generations(lineages.get_trials_at_depth(depth),
                                population_size, depth)

        assert lineages.get_trials_at_depth(10) == []