コード例 #1
0
def test_taking_actions():
    """Does the environment correctly change the state when told to take an action with and without stochasticity?"""
    random.seed()
    env = Gridworld(4, 4, 0.0)

    # Deterministic tests
    assert env.next_state(env.initial_state(), Action.up) == env.state_from_grid_position(GridPosition(0, 1))
    assert env.next_state(env.initial_state(), Action.down) == env.state_from_grid_position(GridPosition(0, 0))
    assert env.next_state(env.initial_state(), Action.left) == env.state_from_grid_position(GridPosition(0, 0))
    assert env.next_state(env.initial_state(), Action.right) == env.state_from_grid_position(GridPosition(1, 0))

    # Stochastic tests
    env.failure_rate = 0.1
    assert ratio_test(lambda state: state == env.state_from_grid_position(GridPosition(0, 0)), partial(env.next_state, env.initial_state(), Action.left), 10000) == 1.0
    ratio = ratio_test(lambda state: state == env.state_from_grid_position(GridPosition(0, 0)), partial(env.next_state, env.initial_state(), Action.up), 10000)
    assert 0.09 < ratio < 0.11
コード例 #2
0
def test_taking_actions():
    """Does the environment correctly change the state when told to take an action with and without stochasticity?"""
    random.seed()
    env = GridworldContinuous(0.05, 0.01)
    start = env.initial_state()
    ratio = ratio_test(lambda state: np.linalg.norm(np.asarray([state[0] - start[0], state[1] - (start[1] + env.move_mean)]), 2) < env.move_sd * 2,
                       partial(env.next_state, start, Action.up), 10000)
    assert 0.7 < ratio
    steps = 0
    s = env.initial_state()
    while not env.is_terminal(s):
        s = env.next_state(s, np.random.randint(4))
        steps += 1
    assert steps < 20000
コード例 #3
0
def test_taking_actions():
    """Does the environment correctly change the state when told to take an action with and without stochasticity?"""
    random.seed()
    env = GridworldContinuous(0.05, 0.01)
    start = env.initial_state()
    ratio = ratio_test(
        lambda state: np.linalg.norm(
            np.asarray(
                [state[0] - start[0], state[1] -
                 (start[1] + env.move_mean)]), 2) < env.move_sd * 2,
        partial(env.next_state, start, Action.up), 10000)
    assert 0.7 < ratio
    steps = 0
    s = env.initial_state()
    while not env.is_terminal(s):
        s = env.next_state(s, np.random.randint(4))
        steps += 1
    assert steps < 20000