Exemplo n.º 1
0
    def test_best_child_min():
        """Test best_child method for a minimization problem"""

        problem = OptProb(5, OneMax(), maximize=False)

        pop = np.array([[0, 0, 0, 0, 1], [1, 0, 1, 0, 1], [1, 1, 1, 1, 0],
                        [1, 0, 0, 0, 1], [100, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                        [1, 1, 1, 1, 1], [0, 0, 0, 0, -50]])

        problem.set_population(pop)
        x = problem.best_child()

        assert np.array_equal(x, np.array([0, 0, 0, 0, -50]))
Exemplo n.º 2
0
    def test_eval_mate_probs():
        """Test eval_mate_probs method"""

        problem = OptProb(5, OneMax(), maximize=True)
        pop = np.array([[0, 0, 0, 0, 1], [1, 0, 1, 0, 1], [1, 1, 1, 1, 0],
                        [1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [1, 1, 1, 1, 1]])

        problem.set_population(pop)
        problem.eval_mate_probs()

        probs = np.array([0.06667, 0.2, 0.26667, 0.13333, 0, 0.33333])

        assert np.allclose(problem.get_mate_probs(), probs, atol=0.00001)
Exemplo n.º 3
0
    def test_eval_mate_probs_all_zero():
        """Test eval_mate_probs method when all states have zero fitness"""

        problem = OptProb(5, OneMax(), maximize=True)
        pop = np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])

        problem.set_population(pop)
        problem.eval_mate_probs()

        probs = np.array(
            [0.16667, 0.16667, 0.16667, 0.16667, 0.16667, 0.16667])

        assert np.allclose(problem.get_mate_probs(), probs, atol=0.00001)
Exemplo n.º 4
0
    def test_set_population_min():
        """Test set_population method for a minimization problem"""

        problem = OptProb(5, OneMax(), maximize=False)

        pop = np.array([[0, 0, 0, 0, 1], [1, 0, 1, 0, 1], [1, 1, 1, 1, 0],
                        [1, 0, 0, 0, 1], [100, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                        [1, 1, 1, 1, 1], [0, 0, 0, 0, -50]])

        pop_fit = -1.0 * np.array([1, 3, 4, 2, 100, 0, 5, -50])

        problem.set_population(pop)

        assert (np.array_equal(problem.get_population(), pop)
                and np.array_equal(problem.get_pop_fitness(), pop_fit))