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 the probabilities are not changing more than by 0.001, we're done.
        if sum(ravel(abs(newpolicy - policy))) < 1e-3:
            return policy, T
        policy = newpolicy
        maxIters -= 1
    return policy, T
Given transition matrices (one per action),
dataset/ETHPy150Open pybrain/pybrain/pybrain/rl/learners/modelbased/policyiteration.py/policyIteration