def test_random_neighbor_max2(): """Test random_neighbor method when max_val is equal to 2""" problem = DiscreteOpt(5, OneMax(), maximize=True) 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)
def test_random_neighbor_max_gt2(): """Test random_neighbor method when max_val is greater than 2""" problem = DiscreteOpt(5, OneMax(), maximize=True, max_val=5) 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)
def test_find_neighbors_max2(): """Test find_neighbors method when max_val is equal to 2""" problem = DiscreteOpt(5, OneMax(), maximize=True, max_val=2) 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)