import numpy as np import pytest from gridworld import GridWorld from value_iteration import q_from_v, value_iteration GAMMA = 0.95 def test_bellman_optimality_holds() -> None: env = GridWorld() v, q, _ = value_iteration(env, GAMMA) # V* is a fixed point of the Bellman optimality backup. assert np.max(np.abs(q_from_v(env, v, GAMMA).max(axis=1) - v)) < 1e-9 def test_hand_computed_values() -> None: env = GridWorld() v, _, _ = value_iteration(env, GAMMA) assert v[env.index[(3, 4)]] == pytest.approx(10.0) # one step from the goal assert v[env.index[(3, 3)]] == pytest.approx(-1 + GAMMA * 10) # two steps # The start is eight steps away: seven step penalties, then the goal. expected = sum(-GAMMA**k for k in range(7)) + GAMMA**7 * 10 assert v[env.start] == pytest.approx(expected) def test_terminal_values_are_zero() -> None: env = GridWorld() v, _, _ = value_iteration(env, GAMMA) for s in range(env.n_states): if env.is_terminal(s): assert v[s] == 0.0 def test_gamma_out_of_range() -> None: with pytest.raises(ValueError): value_iteration(GridWorld(), gamma=1.0)