Exemplo n.º 1
0
 def test_raises_value_errors(self, params):
     print(params)
     # Assert raises ValueError when parameters are incorrect.
     with self.assertRaises(ValueError):
         genetic.GeneticAlgorithm(self.agent,
                                  random_state=self.random_state,
                                  **params).search()
Exemplo n.º 2
0
    def test_genetic_similarity_raises_error(self):
        ga = genetic.GeneticAlgorithm(
            self.agent, mutation_factor=.5, mutation_probability=1,
            max_evolution_duration=4, min_genetic_similarity=.5,
            random_state=self.random_state)

        with self.assertRaises(RuntimeError):
            ga.genetic_similarity()
Exemplo n.º 3
0
    def test_breed(self):
        ga = genetic.GeneticAlgorithm(self.agent,
                                      population_size=100, n_selected=100)

        (ga.search_start().generate_population().cycle_start()
         .select_for_breeding().breed())
        self.assertEqual(len(ga.population_), 100)
        self.assertEqual(len(ga.offspring_), 50)
Exemplo n.º 4
0
    def test_select_for_breeding(self, method):
        ga = genetic.GeneticAlgorithm(self.agent,
                                      n_selected=20,
                                      breeding_selection=method,
                                      max_evolution_cycles=1)

        (ga.search_start().generate_population().cycle_start()
         .select_for_breeding())
        self.assertEqual(len(ga.selected_), 20)
Exemplo n.º 5
0
    def test_search_duration_constraint(self, params, acceptable_elapsed):
        ga = genetic.GeneticAlgorithm(self.agent,
                                      random_state=self.random_state,
                                      **params)

        elapsed = time.time()
        ga.search()
        elapsed = time.time() - elapsed

        # Assert that the duration constraint was respected.
        self.assertLess(elapsed, acceptable_elapsed)
        self.assertIsNotNone(ga.solution_candidate_)
Exemplo n.º 6
0
    def test_generate_population(self):
        ga = genetic.GeneticAlgorithm(self.agent, max_evolution_cycles=1,
                                      random_state=self.random_state)
        ga.search()
        self.assertEqual(ga.population_size_, 1000)

        # Assert that the arrays necessary for the search were disposed.
        self.assertIsNone(ga.population_)
        self.assertIsNone(ga.selected_)
        self.assertIsNone(ga.offspring_)

        ga = genetic.GeneticAlgorithm(self.agent, population_size=20,
                                      max_evolution_cycles=1,
                                      random_state=self.random_state)
        ga.search()
        self.assertEqual(ga.population_size_, 20)

        ga = genetic.GeneticAlgorithm(self.agent,
                                      max_evolution_cycles=10,
                                      max_evolution_duration=1,
                                      n_jobs=1,
                                      random_state=self.random_state)
        ga.search()
        self.assertGreater(ga.population_size_, 100)
Exemplo n.º 7
0
    def test_search(self, params, expected):
        ga = genetic.GeneticAlgorithm(self.agent,
                                      random_state=self.random_state, **params)
        solution = ga.search().solution_candidate_

        # Attributes were set as expected.
        for key, value in expected.items():
            self.assertEqual(getattr(ga, key), value)

        # Assert clean-up was made.
        self.assertIsNone(ga.offspring_)
        self.assertIsNone(ga.selected_)

        # Assert it eventually finds a solution.
        self.assertIsNotNone(solution)
        self.assertEqual(solution.data, 'hello world')
Exemplo n.º 8
0
    def test_preemption_by_genetic_similarity(self):
        expected_variability = .4

        a = _A(search=genetic.GeneticAlgorithm, environment=_E2())

        ga = genetic.GeneticAlgorithm(
            a, max_evolution_duration=60,
            min_genetic_similarity=expected_variability,
            population_size=50,
            mutation_probability=0,
            random_state=self.random_state,
            debug=True).search()

        # Assert that the last population's variability is smaller
        # than the `min_genetic_similarity` parameter passed.
        self.assertLessEqual(ga.variability_, expected_variability)

        self.assertIsNotNone(ga.solution_candidate_)
        self.assertGreaterEqual(a.utility(ga.solution_candidate_), 7)
Exemplo n.º 9
0
 def test_sanity(self):
     ga = genetic.GeneticAlgorithm(self.agent,
                                   random_state=self.random_state)
     self.assertIsNotNone(ga)