#NEWLINE# def policyIteration( Ts , R , discountFactor , VEvaluator = None , initpolicy = None , maxIters = 20 ) : #NEWLINE# #INDENT# ' Given transition matrices (one per action),\n    produce the optimal policy, using the policy iteration algorithm.\n    \n    A custom function that maps policies to value functions can be provided. ' #NEWLINE# if ( initpolicy is None ) : #NEWLINE# #INDENT# ( policy , T ) = randomPolicy ( Ts ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# policy = initpolicy #NEWLINE# T = collapsedTransitions ( Ts , policy ) #NEWLINE# #UNINDENT# if ( VEvaluator is None ) : #NEWLINE# #INDENT# VEvaluator = ( lambda T : trueValues ( T , R , discountFactor ) ) #NEWLINE# #UNINDENT# while ( T > 0 ) : #NEWLINE# #INDENT# V = VEvaluator ( T ) #NEWLINE# ( newpolicy , T ) = greedyPolicy ( Ts , R , discountFactor , V ) #NEWLINE# if ( sum ( ravel ( abs ( ( newpolicy - policy ) ) ) ) < 0.001 ) : #NEWLINE# #INDENT# return ( policy , T ) #NEWLINE# #UNINDENT# policy = newpolicy #NEWLINE# maxIters -= 1 #NEWLINE# #UNINDENT# return ( policy , T )