我正在模拟一个非常la脚的游戏,该游戏基本上会计算玩家随着游戏的进展而收集的硬币和敌人的数量。该代码包含两个jitclass:一个player
jitclass和一个game
jitclass。
对于player
类,我们有一些属性和一些方法来描述玩家在游戏中的进度。
from numba import jitclass, int64, float64, deferred_type
from numba.typed import List
import random
specs_player = OrderedDict()
specs_player['level'] = int64
specs_player['coins'] = float64
@jitclass(specs_player)
class Player:
def __init__(self):
self.level = 0
self.coins = 0
self.enemies = List()
def pass_level(self):
self.level += 1
def collect_coins(self, c):
self.coins += c
def collect_enemies(self, e):
self.enemies.append(e)
def reset_player(self):
self.level = 0
self.coins = 0
self.enemies = List()
如您所见,属性enemies
是一个列表,该列表获取随着玩家在游戏中进行而附加的值。
game
jitclass使用前两行将player
jitclass作为属性调用:
Player_type = deferred_type()
Player_type.define(Player.class_type.instance_type)
specs_Game = OrderedDict()
specs_Game['last_level'] = int64
specs_Game['diff_threshold'] = float64
specs_Game['player'] = Player_type
class Game:
def __init__(self, l, t):
self.player = Player()
self.last_level = l
self.diff_threshold = t
def play_gameround(self):
random_draw = random.uniform(0, 1)
if random_draw > self.diff_threshold:
# Pass Level
self.player.pass_level()
# Collect coins
coins_earned = 100*(random_draw - self.diff_threshold)
self.player.collect_coins(coins_earned)
#Collect enemies
if coins_earned > 10:
self.player.collect_enemies()
def reset_gameplay(self):
self.player.reset_player()
def continue_playing(self):
condition = self.player.level < self.last_level
return(condition)
最后,一个名为run_one_player
的函数模拟一个玩家的进程并返回包含所有必要数据的数组:
def run_one_player(gameplay):
while gameplay.continue_playing():
gameplay.play_gameround()
player_data = np.array([gameplay.player.level,
gameplay.player.coins,
gameplay.player.enemies])
return (player_data)
要运行代码,请输入:
g = Game(l = 10, t = 0.5)
data_list = run_one_player(g)
但是,这不起作用,Numba返回以下错误,我很确定这是由于我没有为enemies
字段正确定义Numba类型而引起的。
---------------------------------------------------------------------------
TypingError Traceback (most recent call last)
<ipython-input-51-f6340d9cfcd7> in <module>
1 players = 10
----> 2 g = Game(l = 10, t = 0.5)
3
4 data_list = run_one_player(g)
<ipython-input-49-6e42f0104cf1> in __init__(self, l, t)
46
47 def __init__(self, l, t):
---> 48 self.player = Player()
49 self.last_level = l
50 self.diff_threshold = t
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/jitclass/base.py in __call__(cls, *args, **kwargs)
124 bind = cls._ctor_sig.bind(None, *args, **kwargs)
125 bind.apply_defaults()
--> 126 return cls._ctor(*bind.args[1:], **bind.kwargs)
127
128
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
374 e.patch_message(msg)
375
--> 376 error_rewrite(e, 'typing')
377 except errors.UnsupportedError as e:
378 # Something unsupported is present in the user code, add help info
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/dispatcher.py in error_rewrite(e, issue_type)
341 raise e
342 else:
--> 343 reraise(type(e), e, None)
344
345 argtypes = []
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/six.py in reraise(tp, value, tb)
656 value = tp()
657 if value.__traceback__ is not tb:
--> 658 raise value.with_traceback(tb)
659 raise value
660
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Cannot resolve setattr: (instance.jitclass.Player#123b442d0<level:int64,coins:float64>).enemies = ListType[undefined]
File "<ipython-input-49-6e42f0104cf1>", line 18:
def __init__(self):
<source elided>
self.coins = 0
self.enemies = List()
^
[1] During: typing of set attribute 'enemies' at <ipython-input-49-6e42f0104cf1> (18)
File "<ipython-input-49-6e42f0104cf1>", line 18:
def __init__(self):
<source elided>
self.coins = 0
self.enemies = List()
^
[1] During: resolving callee type: jitclass.Player#123b442d0<level:int64,coins:float64>
[2] During: typing of call at <string> (3)
[3] During: resolving callee type: jitclass.Player#123b442d0<level:int64,coins:float64>
[4] During: typing of call at <string> (3)
File "<string>", line 3:
<source missing, REPL/exec in use?>
This is not usually a problem with Numba itself but instead often caused by
the use of unsupported features or an issue in resolving types.
To see Python/NumPy features supported by the latest release of Numba visit:
http://numba.pydata.org/numba-doc/latest/reference/pysupported.html
and
http://numba.pydata.org/numba-doc/latest/reference/numpysupported.html
For more information about typing errors and how to debug them visit:
http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-doesn-t-compile
If you think your code should work with Numba, please report the error message
and traceback, along with a minimal reproducer at:
https://github.com/numba/numba/issues/new
答案 0 :(得分:2)
首先:我不认为您应该使用numba这样的东西。 Numba是一种专用工具,非常擅长解决特定类型的问题,而且不是其中一种:
1.1.2. Will Numba work for my code?
这取决于代码的外观,如果代码是数字定向的(做大量数学运算),使用NumPy很多和/或有很多循环,那么Numba通常是一个不错的选择
[...]
但是,在您的特定情况下,您需要完全输入jitclass
的 all 属性。这意味着您必须使用numba可以理解的类型(受支持的类型之一或另一个jitclass)键入enemies
,否则它将不起作用。
由于您没有提供类型,所以我们假设它是整数:
import numba as nb
specs_player = {}
specs_player['level'] = nb.int64
specs_player['coins'] = nb.float64
specs_player['enemies'] = nb.types.List(nb.int64)
@nb.jitclass(specs_player)
class Player:
def __init__(self):
self.level = 0
self.coins = 0
self.enemies = []
创建新实例时失败,因为numba无法推断出一个空列表的类型(至少在目前是这样)。因此,您将必须使用某种类型进行初始化。除了找到包含项目然后清除它的列表,我没有找到更好的方法:
import numba as nb
specs_player = {}
specs_player['level'] = nb.int64
specs_player['coins'] = nb.float64
specs_player['enemies'] = nb.types.List(nb.int64)
@nb.njit
def empty_int64_list():
l = [nb.int64(10)]
l.clear()
return l
@nb.jitclass(specs_player)
class Player:
def __init__(self):
self.level = 0
self.coins = 0
self.enemies = empty_int64_list()
如果您的enemies
不是整数,则可能会复杂得多。但是,我认为这对您而言并不值得,因为这不是numba比纯Python更有效地解决(显着)的问题。