def plotF(n, aroundPoint=None, aroundN=None, colorStates=False, **kwargs): opt_d = bellman.getIter(n)['opt_d'] opt_r = bellman.getIter(n)['opt_r'] # k = d + D - M k_fn = lambda x: opt_d(x) + x[1] - x[0] f_fn = lambda x: p_fns.testf(k_fn(x), opt_r(x)) return plotSurface(f_fn, 'f', aroundPoint, aroundN, colorStates=colorStates, colorFn=iterColorFn(n), **kwargs)
def classifyStateUsingIter(M, D, n): opt_d = g.getFnObj(bellman.getIter(n)['opt_d']) opt_r = g.getFnObj(bellman.getIter(n)['opt_r']) # k = d + D - M k_fn = lambda x: opt_d(x) + x[1] - x[0] f_fn = lambda x: p_fns.f(k_fn(x), opt_r(x)) rho_fn = lambda x: rhoFn(x, f_fn, k_fn) return classifyState(M,D, rho_fn, opt_d)
def getNextMD(M, D, n, zState): optdFn = bellman.getIter(n)['opt_d'] optrFn = bellman.getIter(n)['opt_r'] # k = d + D - M k_fn = lambda x: optdFn(x) + x[1] - x[0] f_fn = lambda x: p_fns.testf(k_fn(x), optrFn(x)) inflow = lambda x: f_fn(x) * g.z_space[zState] M_fn = lambda x: (inflow(x) - k_fn(x))/g.z_space[zState] D_fn = lambda x: optrFn(x) * f_fn(x) return (M_fn([M,D]), D_fn([M,D]))
def plotOptR(n, aroundPoint=None, aroundN=None, colorStates=False, **kwargs): # don't plot red states stateArray = getStateArray(n) def filterFn(x): (iM, iD) = getNearestGridPoint(x[0], x[1]) if (stateArray[iM, iD] != g.STATE_RED): return True return False fnObj = linterp.GetLinterpFnObj(g.stateGridList, bellman.getIter(n)['opt_r']) return plot3d.plotSurface(g.grid_M, g.grid_D, fnObj, xlabel="M", ylabel="D", zlabel="opt r", colorFn=iterColorFn(n), filterFn=filterFn, drawEntireRegion=True, **kwargs)
def getNextStateArray(n, zState): opt_d = bellman.getIter(n)['opt_d'] opt_r = bellman.getIter(n)['opt_r'] # k = d + D - M k_fn = lambda x: opt_d(x) + x[1] - x[0] f_fn = lambda x: p_fns.testf(k_fn(x), opt_r(x)) rho_fn = lambda x: rhoFn(x, f_fn, k_fn) def nextStateFn(M,D): (nextM, nextD) = getNextMD(M, D, n, zState) # if current state is A (bankrupt with certainty), then next state has no meaning. color it black currentState = classifyState(M, D, rho_fn, opt_d) if (currentState == g.STATE_RED): return g.STATE_BLACK # if current state is B (bankrupt if zLow occurs) and zLow occurs, next state is in bankruptcy if (currentState == g.STATE_BLUE and zState == 0): return g.STATE_BLACK return classifyState(nextM, nextD, rho_fn, opt_d) nextStateArray = scipy.vectorize(nextStateFn)(g.mesh_M, g.mesh_D) return scipy.transpose(nextStateArray)
def createGraph(n): global g_NodeArray g_NodeArray = [[None]*g.gridsize_D]*g.gridsize_M stateArray = getStateArray(n) opt_d = bellman.getIter(n)['opt_d'] opt_r = bellman.getIter(n)['opt_r'] G = nx.DiGraph() for (iM, M) in enumerate(g.grid_M): for (iD, D) in enumerate(g.grid_D): #newNode = MyNode(iM, M, iD, D) #g_NodeArray[iM][iD] = newNode G.add_node((iM,iD), M=M, D=D, state=stateArray[iM, iD], opt_d=opt_d([M,D]), opt_r=opt_r([M,D])) # bankruptcy node is 0 G.add_node(g.BANKRUPT_NODE, state=g.STATE_BLACK) G.add_edge(g.BANKRUPT_NODE, g.BANKRUPT_NODE) for (iM, M) in enumerate(g.grid_M): for (iD, D) in enumerate(g.grid_D): (nextMLow, nextDLow) = getNextMD(M, D, n, 0) (nextMHigh, nextDHigh) = getNextMD(M, D, n, 1) if (nextMLow < 0): #G.add_edge(g_NodeArray[iM][iD], g.BANKRUPT_NODE, zState=0) G.add_edge((iM,iD), g.BANKRUPT_NODE, zState=0) else: (next_iMLow, next_iDLow) = getNearestGridPoint(nextMLow, nextDLow) #G.add_edge(g_NodeArray[iM][iD], g_NodeArray[next_iMLow][next_iDLow], zState=0) G.add_edge((iM,iD), (next_iMLow, next_iDLow), zState=0) if (nextMHigh < 0): #G.add_edge(g_NodeArray[iM][iD], g.BANKRUPT_NODE, zState=1) G.add_edge((iM, iD), g.BANKRUPT_NODE, zState=1) else: (next_iMHigh, next_iDHigh) = getNearestGridPoint(nextMHigh, nextDHigh) #G.add_edge(g_NodeArray[iM][iD], g_NodeArray[next_iMHigh][next_iDHigh], zState=1) G.add_edge((iM, iD), (next_iMHigh, next_iDHigh), zState=1) return G
def plotK(n, aroundPoint=None, aroundN=None, colorStates=False, **kwargs): opt_d = bellman.getIter(n)['opt_d'] # k = d + D - M k_fn = lambda x: opt_d(x) + x[1] - x[0] return plotSurface(k_fn, 'k = d + D - M', aroundPoint, aroundN, colorStates=colorStates, colorFn=iterColorFn(n), **kwargs)
def plotOptD(n, aroundPoint=None, aroundN=None, colorStates=False, **kwargs): fnObj = linterp.GetLinterpFnObj(g.stateGridList, bellman.getIter(n)['opt_d']) return plot3d.plotSurface(g.grid_M, g.grid_D, fnObj, xlabel="M", ylabel="D", zlabel="opt d", colorFn=iterColorFn(n), **kwargs)