PIXELBANKv9.1.0
Menu

Policy Evaluation by Linear Solve

Problem Statement

For a fixed policy in a finite MDP, the value function satisfies the linear system V = r + gamma P V, which solves exactly to:

V=(I−γP)−1rV = (I - \gamma P)^{-1} r

Here P is the n x n state-to-state transition matrix induced by the policy and r is the length-n expected-reward vector. Implement policy_eval_exact(P, r, gamma) using numpy, returning V as a list of floats.

Example:

Input:
policy_eval_exact([[1.0]], [1.0], 0.9)
Output:
[10.0]
Reasoning:
  • Identify the dimensions and parameters from the input: the state space size is n=1n=1, the transition matrix is P=[1.0]P = [1.0], the reward vector is r=[1.0]r = [1.0], and the discount factor is γ=0.9\gamma = 0.9.
  • Construct the coefficient matrix AA for the linear system using the formula A=I−γPA = I - \gamma P, where II is the 1×11 \times 1 identity matrix [1.0][1.0]. This yields A=1.0−(0.9×1.0)=0.1A = 1.0 - (0.9 \times 1.0) = 0.1.
  • Solve the linear equation AV=rA V = r for the value vector VV by dividing the reward by the coefficient: V=r/A=1.0/0.1=10.0V = r / A = 1.0 / 0.1 = 10.0.
  • The final output is [10.0]

Constraints:

  • P is n x n (each row sums to 1), r length n, 0 <= gamma < 1.
  • Use numpy.linalg.solve (not an explicit inverse) for stability.
  • Return a list of n floats.
solution.py

Test Results

0/0
Run code to see test results.
Policy Evaluation by Linear Solve - Hard | PixelBank