gpt4 book ai didi

python - 在 Numba @jitclass 中使用列表

转载 作者:行者123 更新时间:2023-12-01 00:27:55 25 4
gpt4 key购买 nike

我正在模拟一个非常蹩脚的游戏,它基本上会计算玩家在游戏过程中收集的硬币和敌人的数量。该代码包含两个 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


最佳答案

首先:我认为你不应该使用 numba 来做这样的事情。 Numba 是一种专门的工具,非常擅长解决特定类型的问题,但这不是其中之一:

1.1.2. Will Numba work for my code?

This depends on what your code looks like, if your code is numerically orientated (does a lot of math), uses NumPy a lot and/or has a lot of loops, then Numba is often a good choice

[...]


但是,在您的特定情况下,您需要完整输入 jitclass所有属性。这意味着您必须使用 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()

如果您的敌人不是整数,情况可能会复杂得多。然而,我认为在你的情况下这是不值得的,因为这不是 numba 比纯 Python 更有效地解决的问题。

关于python - 在 Numba @jitclass 中使用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58412982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com