Implement a function compare_policy_evaluation that performs iterative policy evaluation using one of two update strategies: synchronous or in-place.
In policy evaluation, we repeatedly apply the Bellman expectation equation to update state values. Given a fixed policy, the transition dynamics can be summarized as a transition probability matrix P and an expected reward vector R, so the update rule for each state s is:
V(s) = R(s) + gamma * sum over all s' of P(s, s') * V(s')
The two strategies differ in how they apply this update across states during a single sweep:
- Synchronous: All states are updated using values from the previous sweep. The old values are read, and a completely new value array is produced.
- In-place: States are updated sequentially (in order s = 0, 1, ..., n-1), and each newly computed value is immediately available for use by subsequent state updates within the same sweep.
Your function should:
- Accept an initial value function V, transition matrix P, reward vector R, discount factor gamma, number of sweeps n_sweeps, and a method string ("synchronous" or "in_place")
- Return the value function after performing the specified number of sweeps
- Not modify the original input array V