def iterate2(n=None, **kwargs): localvars = {} def postIterCallbackFn(nIter, newVArray, currentPolicyArrayList, greedyPolicyList, stoppingResult): (stoppingDecision, diff) = stoppingResult print("iter %d, diff %f" % (nIter, diff)) localvars[0] = nIter bellman.appendIter() bellman.setInLastIter('v', newVArray) bellman.setInLastIter('opt_d', currentPolicyArrayList[0]) bellman.setInLastIter('opt_r', currentPolicyArrayList[1]) v0_array = scipy.zeros((len(g.grid_M), len(g.grid_D))); # initial guess for value function d0_array = scipy.zeros((len(g.grid_M), len(g.grid_D))); # initial guess for policy function for d r0_array = scipy.ones((len(g.grid_M), len(g.grid_D))); # initial guess for policy function for r bellman.resetIters() bellman.appendIter() bellman.setInLastIter('v', v0_array) bellman.setInLastIter('opt_d', None) bellman.setInLastIter('opt_r', None) params = PonziParams() time1 = time.time() result = bellman.grid_policyIteration([g.grid_M, g.grid_D], [d0_array, r0_array], v0_array, params, postIterCallbackFn=postIterCallbackFn, parallel=True) (nIter, currentVArray, currentPolicyArrayList, greedyPolicyList) = result time2 = time.time() nIters = localvars[0] print("total time: %f, avg time: %f" % (time2-time1, (time2-time1)/nIters))
def iterate1(n=None, **kwargs): localvars = {} def postIterCallbackFn(nIter, currentVArray, newVArray, optControls, stoppingResult): (stoppingDecision, diff) = stoppingResult print("iter %d, diff %f" % (nIter, diff)) localvars[0] = nIter bellman.appendIter() bellman.setInLastIter('v', newVArray) bellman.setInLastIter('opt_d', optControls[0]) bellman.setInLastIter('opt_r', optControls[1]) v0_array = scipy.zeros((len(g.grid_M), len(g.grid_D))); # initial guess for value function bellman.resetIters() bellman.appendIter() bellman.setInLastIter('v', v0_array) bellman.setInLastIter('opt_d', None) bellman.setInLastIter('opt_r', None) params = PonziParams() time1 = time.time() result = bellman.grid_valueIteration([g.grid_M, g.grid_D], v0_array, params, postIterCallbackFn=postIterCallbackFn) (nIter, currentVArray, newVArray, optControls) = result time2 = time.time() nIters = localvars[0] print("total time: %f, avg time: %f" % (time2-time1, (time2-time1)/nIters))
def postIterCallbackFn(nIter, newVArray, currentPolicyArrayList, greedyPolicyList, stoppingResult): (stoppingDecision, diff) = stoppingResult print("iter %d, diff %f" % (nIter, diff)) localvars[0] = nIter bellman.appendIter() bellman.setInLastIter('v', newVArray) bellman.setInLastIter('opt_d', currentPolicyArrayList[0]) bellman.setInLastIter('opt_r', currentPolicyArrayList[1])
def postIterCallbackFn(nIter, currentVArray, newVArray, optControls, stoppingResult): (stoppingDecision, diff) = stoppingResult print("iter %d, diff %f" % (nIter, diff)) localvars[0] = nIter bellman.appendIter() bellman.setInLastIter('v', newVArray) bellman.setInLastIter('opt_d', optControls[0]) bellman.setInLastIter('opt_r', optControls[1])