"""
Class to calculate a bellmans discrete time dynamic optimization problem
"""
class BellmansDiscreteTime():
def __init__(self,n):
self.n = n
self.xt = var('xt')
self.yt = var('yt')
self.origObjectiveF = (10 * self.xt) + (-.1 * self.yt**2)
self.objectiveF = self.origObjectiveF
def performCalculation(self):
return self.performCalculation1(self.n - 1)
def performCalculation1(self,r):
print self.objectiveF
f(xt,yt) = self.objectiveF
Dyt = f.derivative(yt)
ytSolved= solve(Dyt,yt,solution_dict=True)
myYt = var('myYt')
for soln in ytSolved: myYt = soln[yt]
if r == 0:
f= f.substitute(xt = 0,yt = myYt)
print f
print 'done with recursion'
else:
f = f.substitute(yt = myYt)
f= f.substitute(xt= (xt + yt))
self.objectiveF = self.origObjectiveF + f
self.performCalculation1(r-1)
return f
test= BellmansDiscreteTime(5)
print "n= %d" % (test.n, )
test.performCalculation()
n= 5
-0.100000000000000*yt^2 + 10*xt
(xt, yt) |--> -0.100000000000000*yt^2 + 20*xt + 10*yt
(xt, yt) |--> -0.100000000000000*yt^2 + 30*xt + 20*yt + 250.000000000000
(xt, yt) |--> -0.100000000000000*yt^2 + 40*xt + 30*yt + 1250.00000000000
(xt, yt) |--> -0.100000000000000*yt^2 + 50*xt + 40*yt + 3500.00000000000
(xt, yt) |--> 7500.00000000000
done with recursion
\newcommand{\Bold}[1]{\mathbf{#1}}\left( \mbox{xt}, \mbox{yt} \right) \ {\mapsto} \ 10 \, \mbox{xt} + 10 \, \mbox{yt}