我有Ñ陣列與每個4個元素,即(Ñ = 2)的列表:快速(ER)的方法來確定列表中的所有非支配項
l = [[1, 2, 3, 4], [5, 6, 7, 8]]
和很試圖找到列表中「非支配」的所有元素 - 也就是說,它們不受列表中的任何其他元素支配。如果數組中的每個項目小於或等於另一個數組中的相應項目,則數組將支配另一個數組。所以
dominates([1, 2, 3, 4], [5, 6, 7, 8]) == True
as 1 <= 5 and 2 <= 6 and 3 <= 7 and 4 <= 8
。但是
dominates([1, 2, 3, 9], [5, 6, 7, 8]) == False
as 9 > 8
。這個功能是比較容易編寫,例如:
def dominates(a, b):
return all(i <= j for i, j in zip(a, b))
更簡潔,給l = [a1, a2, a3, .., an]
其中a
是長度爲4列,我想找找不受任何其他a in l
主宰一切a
。
我有以下解決方案:
def get_non_dominated(l):
to_remove = set()
for ind, item_1 in enumerate(l):
if item_2 in to_remove:
continue
for item_2 in l[ind + 1:]:
if dominates(item_2, item_1):
to_remove.add(item_1)
break
elif dominates(item_1, item_2):
to_remove.add(item_2)
return [i for i in l if i not in to_remove]
所以get_non_dominated([[1, 2, 3, 4], [5, 6, 7, 8]])
應該返回[[1, 2, 3, 4]]
。同樣get_non_dominated([[1, 2, 3, 9], [5, 6, 7, 8]])
應該返回上述邏輯不變的列表(沒有任何東西支配其他)。
但是這項檢查發生了很多,l
可能相當大。我想知道是否有人有想法來加速這個速度?我的第一個想法是嘗試使用numpy向量化這些代碼,但我對它的經驗相對較少,並且掙扎了一下。你可以假設l
有所有獨特的數組。任何想法都非常感謝!
這是我不清楚你要實現的目標。請更新您的問題,以包含具有示例輸入和輸出的工作代碼。 – kazemakase
我添加了一些額外的細節,希望能稍微澄清一點。 – user8415803
更新了答案以反映您想要的過濾(偶然是我展示的示例)。 –