繁体   English   中英

动态编程python实现问题

[英]Dynamic programming python implementation problem

我正在尝试使用向后迭代来实现动态编程代码。 但是,python for循环给我列出了超出范围的错误。

pi = [65,61,42,35,30,32,38,42,53,57,59,64,72,77,64,62,40,64,55,43,40,55,30,21]

#backward recursion
#number of stages T
T = [i for i in range(1,25)]
#possible states
S = [i for i in range(100,1100,100)]
#decision variable
D = [100,-100,0]

#initilaize the value function
obj_val = [[[0 for i in range(1,25)] for j in range(1,9)] for k in range(1,4)]

for t in range(24,0,-1):  #Step backward through problem stages T
    for s in range(9): #Step through each possible stage t state
        for d in range(4):     #step through each feasible stage t decision D
             if t == 24:  #if it is the last stage
                 obj_val[t][s][d] = pi[t]*D[d]
             else: 
                 obj_val[t][s][d] = pi[t]*D[d]+obj_val[t+1][s][d]

IndexError:列表索引超出范围(对于if和else条件)

obj_val ,最外部的数组只有4个对象。 因此, if为true,则无法获取obj_val[24] 此外,您的任何数组中都没有索引24。 最大为23。

暂无
暂无

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

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