Ejemplo n.º 1
0
    def test_random_neighbor_range_eq_step():
        """Test random_neighbor method when range equals step size"""

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

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

        neigh = problem.random_neighbor()
        sum_diff = np.sum(np.abs(x - neigh))

        assert (len(neigh) == 5 and sum_diff == 1)
Ejemplo n.º 2
0
    def test_random_neighbor_range_gt_step():
        """Test random_neighbor method when range greater than step size"""

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

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

        neigh = problem.random_neighbor()
        abs_diff = np.abs(x - neigh)
        abs_diff[abs_diff > 0] = 1

        sum_diff = np.sum(abs_diff)

        assert (len(neigh) == 5 and sum_diff == 1)