The problem
numpy.array
is not supported by Numba. Numba only supports a subset of the Numpy top-level functions (ie any function you call like numpy.foo
). Here's an identical issue from the Numba bug tracker.
The "solution"
Here's the list of Numpy functions that Numba actually supports. numpy.zeros
is supported, so in an ideal world you could just change the lines in your code that use np.array
to:
depot = np.zeros(4, dtype=np.float32)
depot[2:] = [30, 40]
firstnode = np.zeros(4, dtype=np.float32)
and it would work. However, when targeting cuda
all Numpy functions that allocate memory (including np.zeros
) are disabled. So you'll have to come up with a solution that doesn't involve any array allocation.
Issues with use of vectorize
Also, it looks like vectorize
is not the wrapper function you should be using. Instead, a function like the one you've written requires the use of guvectorize
. Here's the closest thing to your original code that I was able to get to work:
import math
from numba import guvectorize, float32
import numpy as np
@guvectorize([(float32[:,:], float32[:], float32[:])], '(m,n),(p)->()')
def fitness(vrp_data, individual, totaldist):
# The first distance is from depot to the first node of the first route
depot = np.zeros(4, dtype=np.float32)
depot[2:] = [30, 40]
firstnode = np.zeros(4, dtype=np.float32)
firstnode = vrp_data[vrp_data[:,0] == individual[0]][0] if individual[0] !=0 else depot
x1 = depot[2]
x2 = firstnode[2]
y1 = depot[3]
y2 = firstnode[3]
dx = x1 - x2
dy = y1 - y2
totaldist[0] = math.sqrt(dx * dx + dy * dy)
The third argument in the signature is actually the return value, so you call the function like:
vrp_data = np.arange(100, 100 + 4*4, dtype=np.float32).reshape(4,4)
individual = np.arange(100, 104, dtype=np.float32)
fitness(vrp_data, individual)
Output:
95.67131
Better error message in latest Numba
You should probably upgrade your version of Numba. In the current version, your original code raises a somewhat more specific error message:
TypingError: Failed in nopython mode pipeline (step: nopython frontend). Use of unsupported NumPy function 'numpy.array' or unsupported use of the function.