简体   繁体   English

在python中使用递归函数进行动态编程

[英]dynamic programming with recursive function in python

I'm new to python, and I'm struggling to write a recursive function. 我是python的新手,我正在努力编写一个递归函数。 I have these lines of code for a dynamic programming function in Matlab which were given as an example from a professor: 我在Matlab中有这些代码用于动态编程功能,这些代码是作为教授的一个例子给出的:

function [policy,fstar] =StochasticInventoryControl(N,K,c,h,M,g,Dmax,p)
   fstar=zeros(M+1,N+1);
   for n=N:-1:1
      for s=0:M
         for x=0:M-s
            temp=0;
            for d=0:Dmax
               temp=temp+p(d+1)*(g*min(s+x,d)+fstar(1+max(s+x-d,0),n+1));
            end
            f(1+s,1+x)=-K*(x>0)-c*x-h*(s+x)+temp;
         end
      [fstar(1+s,n),policy(1+s,n)]=max(f(1+s,1:M-s+1));
      end
   end
   policy=policy-1; 
end

I'm trying to re-write the same function in python, and I came up with this code: 我正在尝试在python中重写相同的函数,我想出了这段代码:

def StochasticInventoryControl(N, K, c, h, M, g, Dmax, p):
    fstar = zeros(M + 1, N + 1)
    for n in range (N, 1, -1):
        for s in range (0, M):
            for x in range (0, M - s):
                temp = 0
            for d in range (0, Dmax):
                temp = temp + p(d + 1)*(g*min(s + x, d) + fstar(1 + max(s + x - d,0), n + 1))
        f(1 + s, 1 + x).lvalue = -K * (x > 0) - c * x - h * (s + x) + temp
    [fstar(1 + s, n), policy(1 + s, n)] = max(f(1 + s, n in range (1, M - s + 1))

The last line is incorrect, I know I can't define the function's recursive relation this way in python. 最后一行是不正确的,我知道我无法在python中以这种方式定义函数的递归关系。 How should I write the last line in python? 我应该怎么写python中的最后一行?

First, your indexing and ranges are off. 首先,您的索引和范围已关闭。 Like most programming languages, python indexes and ranges start with 0, and ranges are half-open (they exclude the last value), so you need to take that into account in your ranges. 与大多数编程语言一样,python索引和范围从0开始,范围是半开放的(它们排除了最后一个值),因此您需要在范围内考虑这一点。

Second, numpy uses square brackets for indexing, so it would be f[s,x] not f(s,x) . 其次,numpy使用方括号进行索引,因此它将是f[s,x]而不是f(s,x)

Third, you can do slicing in numpy arrays just like in MATLAB, but you need to take into account the indexing differences and the fact that for x:y:z indexing the last element is the step size in Python while in MATLAB the middle element is. 第三,你可以像在MATLAB中那样在numpy数组中进行切片,但你需要考虑索引差异和x:y:z索引最后一个元素的事实是Python中的步长,而在MATLAB中是中间元素是。

Fourth, indentation is significant in Python, so your inner loop needs to be indented one more level to work the same as in MATLAB. 第四,缩进在Python中很重要,因此你的内部循环需要缩进一个级别,以便在MATLAB中工作。

As for your question about the last line last line, there is no single function in numpy that handles both the max value and max index. 至于关于最后一行最后一行的问题,numpy中没有单一函数可以处理最大值和最大索引。 You need to use np.max for the value and np.argmax for the index, like so: 您需要使用np.max的价值和np.argmax为索引,就像这样:

fstar[s, n-1] = np.max(f[s, :M-s+1])
policy[s, n-1] = np.argmax(f[s, :M-s+1])+1

Or better yet use the numpy method approach: 或者更好地使用numpy方法:

fstar[s, n-1] = f[s, :M-s+1].max()
policy[s, n-1] = f[s, :M-s+1].argmax()+1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM