In [287]:
import numpy as np
import itertools
from numpy import unravel_index
In [205]:
def subset_sum(numbers, target, length, result, partial=[]):
s = sum(partial)
#print(partial)
# check if the partial sum is equals to target
if round(s,15) == target and len(partial) == length:
result.append([round(x, 15) for x in partial])
if s > target or len(partial)> length:
return # if we reach the number why bother to continue
for i in range(len(numbers)):
n = numbers[i]
subset_sum(numbers, target, length, result, partial + [n])
In [206]:
def weight_gen(M, H):
weight_comps = list()
weight_comps.append(0)
for i in range(H):
weight_comps.append(weight_comps[-1] + 1)
#print(weight_comps)
permu = list()
subset_sum(weight_comps, H, M, permu)
# for perm in permu:
# perm.append(round(1-sum(perm),15))
print("total:%d" % len(permu))
return permu
In [366]:
def get_best_solutions(W_vecs, data):
best_result = list()
for vec in W_vecs:
result = np.array(vec*data.T)
tmp = result.reshape((result.shape[1],))
tmp = tmp[::-1]
#print(np.argmax(tmp), len(tmp))
max_idx = len(tmp) - np.argmax(tmp) - 1
best_result.append(max_idx)
print(W_vecs[0], best_result[0])
return data[np.array(best_result),:]
In [371]:
M = 5
H = 16
data = np.matrix(np.loadtxt('dtlz2_%dobj.txt' % M))
wv = weight_gen(M, H)
In [372]:
get_best_solutions(wv, data)
Out[372]:
In [ ]: