def LSPI_policy(fMap, Ts, R, discountFactor, initpolicy=None, maxIters=20):
 if (initpolicy is None):
  (policy, _) = randomPolicy(Ts)
 else:
  policy = initpolicy
 while (maxIters > 0):
  Qs = LSTD_Qvalues(Ts, policy, R, fMap, discountFactor)
  newpolicy = greedyQPolicy(Qs)
  if (sum(ravel(abs((newpolicy - policy)))) < 0.001):
   return (policy, collapsedTransitions(Ts, policy))
  policy = newpolicy
  maxIters -= 1
 return (policy, collapsedTransitions(Ts, policy))

def LSPI_policy ( fMap , Ts , R , discountFactor , initpolicy = None , maxIters = 20 ) : if ( initpolicy is None ) : ( policy , _ ) = randomPolicy ( Ts ) else : policy = initpolicy while ( maxIters > 0 ) : Qs = LSTD_Qvalues ( Ts , policy , R , fMap , discountFactor ) newpolicy = greedyQPolicy ( Qs ) if ( sum ( ravel ( abs ( ( newpolicy - policy ) ) ) ) < 0.001 ) : return ( policy , collapsedTransitions ( Ts , policy ) ) policy = newpolicy maxIters -= 1 return ( policy , collapsedTransitions ( Ts , policy ) )
lspi is like policy iteration .