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)
def test_update_state_in_range(): """Test update_state method where all updated values are within the tolerated range""" problem = ContinuousOpt(5, OneMax(), maximize=True, min_val=0, max_val=20, step=1) x = np.array([0, 1, 2, 3, 4]) problem.set_state(x) y = np.array([2, 4, 6, 8, 10]) updated = problem.update_state(y) assert np.array_equal(updated, (x + y))
def test_update_state_outside_range(): """Test update_state method where some updated values are outside the tolerated range""" problem = ContinuousOpt(5, OneMax(), maximize=True, min_val=0, max_val=5, step=1) x = np.array([0, 1, 2, 3, 4]) problem.set_state(x) y = np.array([2, -4, 6, -8, 10]) updated = problem.update_state(y) z = np.array([2, 0, 5, 0, 5]) assert np.array_equal(updated, z)
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)
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)