Hi everyone,
I have a simple object-oriented python + njit program, which also has a funny error - it is a TypingError, but I have no idea how to solve it!
Detail: I want to index a list based on a number i, 0 or 1. The choice is based on a condition (a function); if I used the return i to index to list (R[i]), it does not work! But if I say: if i == 0: R[0], works!
Error triggered:
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Cannot getitem on a literal list, return type cannot be statically determined.
If someone can figure out why…! See the code below!
import numpy as np
import matplotlib.pyplot as plt
import time
from numba import jit
from numba.experimental import jitclass
from numba import types, typed, int64
from numba.types import ListType
from numba.typed.typedlist import List
import random
spec = [('detail', int64),]
@jitclass(spec)
class Species:
def __init__(self, detail = 0):
self.detail = int(detail)
@jitclass
class Actions:
def __init__(self):
pass
def OptionA(self, A, B):
A.detail -= 1
B.detail += 2
return A, B
def OptionB(self, A, B, C):
A.detail -= 1
B.detail -= 1
C.detail += 1
return A, B, C
@jit(nopython=True)
def cumsum(x, target):
total = 0
for i, x_i in enumerate(x):
total += x_i
if total >= target:
return i
raise IndexError('list index out of range')
@jit(nopython=True)
def TestNumba(species, s_actions):
SpecieA, SpecieB, SpecieC = species
r = [[s_actions.OptionA, (SpecieA, SpecieB)],
[s_actions.OptionB, (SpecieA, SpecieB, SpecieC)]]
actions_options = np.array([SpecieA.detail*0.5, SpecieA.detail*SpecieB.detail*2.3])
t = 0
tend = 10
while t < tend:
actions_options_sum = np.sum(actions_options)
rnd = np.random.uniform(0.0, 1.0)*actions_options_sum
i = cumsum(actions_options, rnd)
r[i][0](*r[i][1])
## Like this works:
# if i == 0:
# r[0][0](*r[0][1])
# elif i == 1:
# r[1][0](*r[1][1])
t += 1
print(SpecieA.detail, SpecieB.detail, SpecieC.detail)
return
SpecieA = Species(50)
SpecieB = Species(200)
SpecieC = Species()
species = [SpecieA, SpecieB, SpecieC]
s_actions = Actions()
startime = time.time()
TestNumba(species, s_actions)
endtime = time.time() - startime ```