def policyIteration(Ts, R, discountFactor, VEvaluator=None, initpolicy=None, maxIters=20):
 if (initpolicy is None):
  (policy, T) = randomPolicy(Ts)
 else:
  policy = initpolicy
  T = collapsedTransitions(Ts, policy)
 if (VEvaluator is None):
  VEvaluator = (lambda T: trueValues(T, R, discountFactor))
 while (maxIters > 0):
  V = VEvaluator(T)
  (newpolicy, T) = greedyPolicy(Ts, R, discountFactor, V)
  if (sum(ravel(abs((newpolicy - policy)))) < 0.001):
   return (policy, T)
  policy = newpolicy
  maxIters -= 1
 return (policy, T)

def policyIteration ( Ts , R , discountFactor , VEvaluator = None , initpolicy = None , maxIters = 20 ) : if ( initpolicy is None ) : ( policy , T ) = randomPolicy ( Ts ) else : policy = initpolicy T = collapsedTransitions ( Ts , policy ) if ( VEvaluator is None ) : VEvaluator = ( lambda T : trueValues ( T , R , discountFactor ) ) while ( maxIters > 0 ) : V = VEvaluator ( T ) ( newpolicy , T ) = greedyPolicy ( Ts , R , discountFactor , V ) if ( sum ( ravel ( abs ( ( newpolicy - policy ) ) ) ) < 0.001 ) : return ( policy , T ) policy = newpolicy maxIters -= 1 return ( policy , T )
given transition matrices .