1
我試圖生成一個鄰居數組,每個鄰居在一個隨機洗牌中不同於前一個鄰居,無論是行或列洗牌。只有在特定的行或列的K 1多於一個的情況下才會生成鄰居,如果是,則應該生成一個鄰居,並將該行或列進行混洗。洗牌應該是疊加的,這意味着如果第一行被洗牌,然後是一列;應該生成兩個鄰居,一個具有混洗行,另一個具有混洗行和列的兩個鄰居。多個Numpy隨機洗牌不加起來
但是,似乎該數組只是洗牌兩次,一個用於列循環,另一個用於行循環,而混洗不加起來。
在這個例子中,列索引1和2應該被混洗,並且行索引3和4.下面是一個輸出示例,顯示了生成的鄰居:
[[0 0 0 1 1]
[0 0 1 0 0]
[1 1 0 0 0]
[0 1 1 1 0]
[1 1 1 0 1]]
[[0 0 0 1 1]
[0 0 1 0 0]
[0 0 1 0 1]
[0 1 1 1 0]
[1 1 1 0 1]]
[[0 0 0 1 1]
[0 0 1 0 0]
[0 0 1 0 1]
[0 1 1 1 0]
[1 1 1 0 1]]
[[0 0 0 1 1]
[0 0 1 0 0]
[0 0 1 0 1]
[0 1 1 1 0]
下面是代碼:
k=2
tmp = np.array([
[0, 0, 0, 1, 1]
,[0, 0, 1, 0, 0]
,[1, 1, 0, 0, 0]
,[0, 1, 1, 1, 0]
,[1, 1, 1, 0, 1]
])
board = np.copy(tmp)
column = np.sum(board, axis=0)-k
columns = len(column)
row = np.sum(board, axis=1)-k
rows = len(row)
neighbors = []
for i in range(columns):
if column[i] > 0:
np.random.shuffle(board[:,i])
neighbors.append(np.copy(board))
for i in range(rows):
if row[i] > 0:
np.random.shuffle(board[i,:])
neighbors.append(np.copy(board))
print tmp
print column
print row
for i in neighbors:
print i
感謝