def test_random_pop():
        """Test random_pop method"""

        problem = ContinuousOpt(5, OneMax(), maximize=True,
                                min_val=0, max_val=1, step=1)
        problem.random_pop(100)

        pop = problem.get_population()
        pop_fitness = problem.get_pop_fitness()

        assert (np.shape(pop)[0] == 100 and np.shape(pop)[1] == 5
                and np.sum(pop) > 0 and np.sum(pop) < 500
                and len(pop_fitness) == 100)
Example #2
0
    def test_find_neighbors_range_eq_step():
        """Test find_neighbors method when range equals step size"""

        problem = ContinuousOpt(5, OneMax(), maximize=True,
                                min_val=0, max_val=1, step=1)

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

        problem.find_neighbors()

        neigh = np.array([[1, 1, 0, 1, 0],
                          [0, 0, 0, 1, 0],
                          [0, 1, 1, 1, 0],
                          [0, 1, 0, 0, 0],
                          [0, 1, 0, 1, 1]])

        assert np.array_equal(np.array(problem.neighbors), neigh)