python - 如何用单个 numpy 数组操作替换这个三重 For 循环?

标签 python arrays numpy graph-theory

对于回合制游戏,我想计算每个玩家可以在 map 的每个区域上移动或生成的最大单位数量。

我需要计算的所有数据都已经存储在几个 numpy 数组中,但我正在努力寻找高级数组索引技术来尽可能快地进行计算。

为了帮助解决这个问题,我用一些 For 循环以最简单的方式重写了该函数:

import numpy as np

def get_max_units_on_zone_per_player(unitCountPerPlayer, zoneOwner, playerAvailableUnits, zoneLinks, blockedMovesPerPlayer):
    """
    Parameters
    ----------
    unitCountPerPlayer: np.array((zoneCount, playerCount), dtype=int)
        How many units each player has on a zone
    zoneOwner: np.array(zoneCount, dtype=int)
        Which player is owning a zone (-1 for none)
    playerAvailableUnits: np.array(playerCount, dtype=int)
        How many units each player can spawn
    zoneLinks: np.array((zoneCount, zoneCount), dtype=int)
        > 0 if zone1 is connected to zone2 (directed and weighted graph)
    blockedMovesPerPlayer: np.array((playerCount, zoneCount, zoneCount), dtype=bool)
        True if player can not move from zone1 to zone2

    Returns
    -------
    np.array((zoneCount, playerCount), dtype=int)
        Maximum count of units each player can have on each zone
    """

    zoneCount, playerCount = unitCountPerPlayer.shape

    # Adding units already on zone
    result = np.zeros((zoneCount, playerCount), dtype=int) + unitCountPerPlayer

    for p in xrange(playerCount):
        for z1 in xrange(zoneCount):

            if zoneOwner[z1] in (-1, p):
                # Player can spawn on neutral or owned zones
                result[z1, p] += playerAvailableUnits[p]

            for z2 in xrange(zoneCount):

                if zoneLinks[z1, z2] > 0 and not blockedMovesPerPlayer[p, z1, z2]:
                    # If z1 and z2 are connected and player can move from z1 to z2, adding units count on z1 to z2
                    result[z2, p] += unitCountPerPlayer[z1, p]
    return result

问题是我无法使用这个函数,每次调用大约需要 30 毫秒,而且我确信它是可重写的,因为一些 numpy 操作的处理时间应该不到 5 毫秒。

有人可以帮我解决这个问题吗?还有一个循序渐进的过程,以便下次我可以自己做吗?我已经多次阅读 numpy 关于数组和索引的文档,但它远非非常清晰,我只是无法弄清楚。

编辑:根据要求,以下是一些可以用作示例的随机数据:

zoneCount=8 ; playerCount=2

unitCountPerPlayer:
[[1 2]
 [1 3]
 [1 3]
 [3 2]
 [1 2]
 [3 2]
 [0 2]
 [3 2]]

zoneOwner:
[ 1  0 -1 -1 -1  0 -1 -1]

playerAvailableUnits:
[2 2]

zoneLinks:
[[0 1 1 1 0 1 0 0]
 [1 0 0 1 0 0 0 1]
 [1 1 1 1 0 1 0 1]
 [0 1 1 1 1 0 1 0]
 [0 0 1 1 1 0 1 1]
 [0 0 1 1 1 1 1 1]
 [1 0 0 0 0 1 0 1]
 [1 1 1 1 0 1 1 1]]

blockedMovesPerPlayer:
[[[False False False False False False False False]
  [ True False False False False False False False]
  [ True False False False False False False False]
  [False False False False False False False False]
  [False False False False False False False False]
  [False False False False False False False False]
  [ True False False False False False False False]
  [ True False False False False False False False]]

 [[False  True False False False  True False False]
  [False False False False False False False False]
  [False  True False False False  True False False]
  [False  True False False False False False False]
  [False False False False False False False False]
  [False False False False False  True False False]
  [False False False False False  True False False]
  [False  True False False False  True False False]]]

get_max_units_on_zone_per_player():
[[ 1 14]
 [11  3]
 [15 18]
 [18 20]
 [10 10]
 [13  2]
 [12 12]
 [14 18]]
<小时/>

可复制/粘贴数据:

zoneCount = 8
playerCount = 2

unitCountPerPlayer = np.array([[1,2], [1,3], [1,3], [3,2],
                               [1,2], [3,2], [0,2], [3,2]])

zoneOwner = np.array([1, 0, -1, -1, -1, 0, -1, -1])

playerAvailableUnits = np.array([2,2])

zoneLinks = np.array([[0,1,1,1,0,1,0,0], [1,0,0,1,0,0,0,1],
                      [1,1,1,1,0,1,0,1], [0,1,1,1,1,0,1,0],
                      [0,0,1,1,1,0,1,1], [0,0,1,1,1,1,1,1],
                      [1,0,0,0,0,1,0,1], [1,1,1,1,0,1,1,1]])

bmpp = [[[False, False, False, False, False, False, False, False],
         [ True, False, False, False, False, False, False, False],
         [ True, False, False, False, False, False, False, False],
         [False, False, False, False, False, False, False, False],
         [False, False, False, False, False, False, False, False],
         [False, False, False, False, False, False, False, False],
         [ True, False, False, False, False, False, False, False],
         [ True, False, False, False, False, False, False, False]],
        [[False,  True, False, False, False,  True, False, False],
         [False, False, False, False, False, False, False, False],
         [False,  True, False, False, False,  True, False, False],
         [False,  True, False, False, False, False, False, False],
         [False, False, False, False, False, False, False, False],
         [False, False, False, False, False,  True, False, False],
         [False, False, False, False, False,  True, False, False],
         [False,  True, False, False, False,  True, False, False]]]
blockedMovesPerPlayer = np.array(bmpp)

最佳答案

[更新:numpy方式的实现,避免for循环]

这是我的 get_max_units_on_zone_per_player()实现:

def get_max_units_on_zone_per_player(unitCountPerPlayer, zoneOwner, playerAvailableUnits, zoneLinks, blockedMovesPerPlayer):
    result = unitCountPerPlayer.copy()
    result[zoneOwner < 0] += playerAvailableUnits
    _z1 = np.where(zoneOwner >= 0)
    result[_z1, zoneOwner[_z1]] += playerAvailableUnits[zoneOwner[_z1]]
    _p, _z1, _z2 = np.where(np.logical_and(zoneLinks > 0, np.logical_not(blockedMovesPerPlayer)))
    np.add.at(result, [_z2, _p], unitCountPerPlayer[_z1, _p])
    return result

我使用以下设置测试了这两种实现:

zoneCount = 100
playerCount = 1000
maxUnits = 500

unitCountPerPlayer = np.random.randint(0, maxUnits, size=(zoneCount, playerCount))
zoneOwner = np.random.randint(-1, playerCount, size=zoneCount)
playerAvailableUnits = np.random.randint(0, maxUnits, size=playerCount)
zoneLinks = np.random.randint(0, maxUnits, size=(zoneCount, zoneCount))
blockedMovesPerPlayer = np.random.randint(0, 2, size=(playerCount, zoneCount, zoneCount), dtype=bool)

这是测试结果(带有%timeit)

  • fbparis 的原始实现:

    每次循环 7.27 s ± 10 ms(7 次运行的平均值 ± 标准差,每次 1 次循环)

  • 我的实现:

    每次循环 645 ms ± 490 µs(7 次运行的平均值 ± 标准差,每次 1 次循环)

关于python - 如何用单个 numpy 数组操作替换这个三重 For 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48472961/

相关文章:

python - 交替应用于 Pandas

python - 如何使用带有诅咒的终端调色板

python - 使用 OpenCV 时找不到模块 cv2

python - 什么类型的选项适用于 SlugField?

c - 为什么在 C 中释放我重新分配的数组失败?

javascript - 如何创建一个方法来/2数组

python - 使用 numpy 从图像数组的 RGB 值获取 (x,y) 坐标值

python - Django:{{ request.user }} 在模板中没有显示任何内容

java - for with if语句java

python - 列表理解或 map() 或 for 循环访问前一行来操作数组