我有两个不同的数组处理问题,我想解决AQAP(Q=快速),以确保解决方案在我的过程中不受速率限制(使用NEAT来训练视频游戏机器人)。在一种情况下,我想建立一个惩罚函数,使更大的柱高度,在另一种情况下,我想奖励建设“共同价值的岛屿。你知道吗
操作开始于一个26行x 6列的numpy灰度值数组,背景为黑色/0。你知道吗
我已经为每个已经实现了一些numpy的问题提供了有效的解决方案,但是我想推动这两个问题的完全矢量化方法。你知道吗
import numpy as np,
from scipy.ndimage.measurements import label as sp_label
from math import ceil
这两个问题都是从这样的数组开始的:
img= np.array([[ 0., 0., 0., 12., 0., 0.],
[ 0., 0., 0., 14., 0., 0.],
[ 0., 0., 0., 14., 0., 0.],
[ 0., 0., 0., 14., 0., 0.],
[16., 0., 0., 14., 0., 0.],
[16., 0., 0., 12., 0., 0.],
[12., 0., 11., 0., 0., 0.],
[12., 0., 11., 0., 0., 0.],
[16., 0., 15., 0., 15., 0.],
[16., 0., 15., 0., 15., 0.],
[14., 0., 12., 0., 11., 0.],
[14., 0., 12., 0., 11., 0.],
[14., 15., 11., 0., 11., 0.],
[14., 15., 11., 0., 11., 0.],
[13., 16., 12., 0., 13., 0.],
[13., 16., 12., 0., 13., 0.],
[13., 14., 16., 0., 16., 0.],
[13., 14., 16., 0., 16., 0.],
[16., 14., 15., 0., 14., 0.],
[16., 14., 15., 0., 14., 0.],
[14., 16., 14., 0., 11., 0.],
[14., 16., 14., 0., 11., 0.],
[11., 13., 14., 16., 12., 13.],
[11., 13., 14., 16., 12., 13.],
[12., 12., 15., 14., 15., 11.],
[12., 12., 15., 14., 15., 11.]])
第一个(柱高)问题目前正在通过以下方法解决:
# define valid connection directions for sp_label
c_valid_conns = np.array((0,1,0,0,1,0,0,1,0,), dtype=np.int).reshape((3,3))
# run the island labeling function sp_label
# c_ncomponents is a simple count of the conected columns in labeled
columns, c_ncomponents = sp_label(img, c_valid_conns)
# calculate out the column lengths
col_lengths = np.array([(columns[columns == n]/n).sum() for n in range(1, c_ncomponents+1)])
col_lengths
给我这个数组:[6。2220181444.]
(如果代码始终忽略不“包含”数组底部的标记区域(行索引25/-1),则为奖励)
第二个问题涉及对每个唯一值进行掩蔽,并计算每个掩蔽数组中的相邻实体,以获得相邻实体的大小:
# initial values to start the ball rolling
values = [11, 12, 13, 14, 15, 16]
isle_avgs_i = [1.25, 2, 0, 1,5, 2.25, 1]
# apply filter masks to img to isolate each value
# Could these masks be pushed out into a third array dimension instead?
masks = [(img == g) for g in np.unique(values)]
# define the valid connectivities (8-way) for the sp_label function
m_valid_conns = np.ones((3,3), dtype=np.int)
# initialize islanding lists
# I'd love to do away with these when I no longer need the .append() method)
mask_isle_avgs, isle_avgs = [],[]
# for each mask in the image:
for i, mask in enumerate(masks):
# run the island labeling function sp_label
# m_labeled is the array containing the sequentially labeled islands
# m_ncomponents is a simple count of the islands in m_labeled
m_labeled, m_ncomponents = sp_label(mask, m_valid_conns)
# collect the average (island size-1)s (halving to account for...
# ... y resolution) for each island into mask_isle_avgs list
# I'd like to vectorize this step
mask_isle_avgs.append((sum([ceil((m_labeled[m_labeled == n]/n).sum()/2)-1
for n in range(1, m_ncomponents+1)]))/(m_ncomponents+1))
# add up the mask isle averages for all the islands...
# ... and collect into isle_avgs list
# I'd like to vectorize this step
isle_avgs.append(sum(mask_isle_avgs))
# initialize a difference list for the isle averages (I also want to do away with this step)
d_avgs = []
# evaluate whether isle_avgs is greater for the current frame or the...
# ... previous frame (isle_avgs_i) and append either the current...
# ... element or 0, depending on whether the delta is non-negative
# I want this command vectorized
[d_avgs.append(isle_avgs[j])
if (isle_avgs[j]-isle_avgs_i[j])>=0
else d_avgs.append(0) for j in range(len(isle_avgs))]
d_avgs
给我这个d\u avgs数组:[0,0,0.46785714285714286,1.8678571428571429,0,0]
(如果代码始终忽略不“包含”数组底部(行索引25/-1)的标记区域,则再次给出此数组:
[0,0,0.43452380952380953,1.6345238095238095,0,0])
我希望删除任何列表操作和理解,并将它们移动到完全矢量化的numpy/scipy实现中,得到相同的结果。你知道吗
任何帮助消除这些步骤将不胜感激。你知道吗
以下是我最终解决这个问题的方法:
相关问题 更多 >
编程相关推荐