Exemple #1
0
    def test_eval_fitness_min():
        """Test eval_fitness method for a minimization problem"""

        problem = OptProb(5, OneMax(), maximize=False)
        x = np.array([0, 1, 2, 3, 4])
        fitness = problem.eval_fitness(x)

        assert fitness == -10
Exemple #2
0
    def test_set_state_min():
        """Test set_state method for a minimization problem"""

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

        x = np.array([0, 1, 2, 3, 4])
        problem.set_state(x)

        assert (np.array_equal(problem.get_state(), x)
                and problem.get_fitness() == -10)
Exemple #3
0
    def test_eval_mate_probs_maximize_false():
        """Test eval_mate_probs method"""

        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],
                        [0, 0, 0, 0, 0],
                        [1, 1, 1, 1, 1]])

        problem.set_population(pop)
        problem.eval_mate_probs()

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

        assert np.allclose(problem.get_mate_probs(), probs, atol=0.00001)
Exemple #4
0
    def test_best_child_max():
        """Test best_child method for a maximization problem"""

        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],
                        [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([100, 0, 0, 0, 0]))
Exemple #5
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)
Exemple #6
0
    def test_best_neighbor_min():
        """Test best_neighbor 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.neighbors = pop
        x = problem.best_neighbor()

        assert np.array_equal(x, np.array([0, 0, 0, 0, -50]))
Exemple #7
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))