n個のリストからすべての可能な組み合わせを計算し、最小二乗差が最小の組み合わせを維持するための非常に効率的な方法を探しています。
私はすでにそれを行うコードを持っていますが、それが数百万の組み合わせになると、物事は遅くなります。
Candidates_lenには、[[500、490、510、600] [300、490、520] [305、497、515]]などの長さのリストのリストが含まれます。candidates_nameに は、[['a'、 ' b'、' c'、' d'] ['mi'、' mu'、' ma'] ['pi'、' pu'、' pa']]
両方のリストにはn個のリストがあります。
# Creating the possible combinations and store the lists of lengths in vector r
r=[[]]
for x in candidates_len:
r = [ i + [y] for y in x for i in r ]
#Storing the names of the combinations and store the lists of identifiers in vector z
z=[[]]
for x in candidates_name:
z = [ i + [y] for y in x for i in z ]
#Calculating distances and storing the minimum one
min_index = 0
min_dist = 0
list_best = []
for index, item in enumerate(r):
n = 0
dist = 0
while n < len(candidates_len):
for i in range(n,len(candidates_len)):
dist = dist + (item[n]-item[i])**2
n=n+1
if index==0:
min_dist = dist
min_index = index
list_best.append(item)
elif dist < min_dist:
min_dist = dist
min_index = index
list_best = []
list_best.append(z[index])
least_combination = min_index
ハードケース: http: //pastebin.com/BkVQTQWK
ここにいくつかのテスト時間があります。1分かそこら以下に入るのが良いでしょう。それが可能かどうかはわかりませんが。
combinations time(s) 77760 1.255663 41184 1.580333 69120 6.214786 8960 1.131834 537600 14.855361 89100 1.264126 16384 3.247404 4199040 666.853284 226800 3.935878 9289728 679.064149