Explore Library
Code QuizIntermediate

Value Iteration Init

Find the initialization bug that breaks value iteration with negative rewards.

Codejavascript
function valueIteration(states, actions, R, P, gamma, iterations) {
  const V = {};
  for (const s of states) V[s] = 0;
  for (let i = 0; i < iterations; i++) {
    for (const s of states) {
      let best = 0;
      for (const a of actions) {
        let q = R[s][a];
        for (const sp of states) q += gamma * P[s][a][sp] * V[sp];
        best = Math.max(best, q);
      }
      V[s] = best;
    }
  }
  return V;
}

What is the bug in this value iteration update?