0

I am getting the error from the title, but can't find AT ALL where is it. Can someone please help me?

def addintarray(int, array):
    array[0] = array[0] + int
    return array

def DP(items_0, capacity, idx_used):
    if(not items_0.any()):
        return np.array([0, idx_used])
    elif(items_0[len(items_0)-1][1] <= capacity):
        return np.maximum( DP(items_0=items_0[:-1], capacity=capacity, idx_used=np.insert(idx_used, 0, [0])), addintarray(int=items_0[len(items_0)-1][0], array=DP(items_0=items_0[:-1], capacity=capacity-items_0[len(items_0)-1][1], idx_used=np.insert(idx_used, 0, [1])) ) )
    else:
        return DP(items_0=items_0[:-1], capacity=capacity, idx_used=np.insert(idx_used, 0, [0]))

items = [Item(index=0, value=8, weight=4), Item(index=1, value=10, weight=5), Item(index=2, value=15, weight=8), Item(index=3, value=4, weight=3)]

DP(items_0 = np.array([x[1:3] for x in items]), capacity = 11, idx_used = np.array([]))

The output:

Traceback (most recent call last):
File "<input>", line 2, in <module>
File "<input>", line 8, in DP
File "<input>", line 8, in DP
File "<input>", line 8, in DP
[Previous line repeated 1 more time]
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
daniellga
  • 1,142
  • 6
  • 16
  • I suspect `elif(items_0[len(items_0)-1][1] <= capacity):` is to blame. Without a [mre] it's impossible to tell for sure. But if `items_0` has 2 or more dimensions then this will happen. – Andras Deak -- Слава Україні Nov 06 '19 at 00:41
  • The repeated `DP` in the traceback means this is happening several layers down in the recursive function. That recursion makes it hard to trace your code just by sight. Most likely it's that `elif` condition that's getting a multi-item boolean array. You need to verify that (with some `prints`), and decide from that whether it needs another `any`, or if something is happening that you didn't anticipate. – hpaulj Nov 06 '19 at 02:57

0 Answers0