def asignacion_paquetes(U,m,v):
P={}
for u in range(U+1):
P[0,u] =0
for i in range(1,len(m)+1):
P[i,0]=0
for u in range(1,U+1):
P[i,u] = max(P[i-1, u-x]+v[i,x] for x in range(0,min(u,m[i])+1))
return P[len(m),U]
I would like to know where the error is because it tells me IndexError: list index out of range
in the line where I put the arrow - >
This code represents an optimization problem of optimal allocation of resources resolved by dynamic programming U represents the total resources and m is a list that represents the limit of resources that can be assigned to an activity i and v is a dictionary that has as key a tuple i , u that represents the benefit of assigning a resource or an activity i.
All this with the aim of obtaining the greatest benefit.
Hence the formula P[i,u]= max(P[i-1,u-x] + v[i,x]
.
Can someone help me?
U = 12
m = [2,4,2,4,2]
v=dict(((i,u),randrange(100)) for i in range(1,len(m)+1) for u in range(0, U+1))
These are the values of the parameters