Scalar Evaluation

Utility Functions


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),:]

Testing


In [371]:
M = 5
H = 16
data = np.matrix(np.loadtxt('dtlz2_%dobj.txt' % M))
wv = weight_gen(M, H)


total:4845

In [372]:
get_best_solutions(wv, data)


[0, 0, 0, 0, 16] 98023
Out[372]:
matrix([[  1.02408000e-04,   9.68864000e-05,   2.69867000e-04,
           2.34156000e-04,   1.00000000e+00],
        [  8.64720000e-04,   4.89150000e-04,   8.56516000e-04,
           6.61660000e-02,   9.97808000e-01],
        [  4.01750000e-04,   1.75677000e-03,   4.52356000e-04,
           1.41268000e-01,   9.89970000e-01],
        ..., 
        [  9.89623000e-01,   4.75986000e-02,   2.72736000e-02,
           3.41016000e-02,   1.28349000e-01],
        [  9.89261000e-01,   1.00543000e-01,   2.10164000e-02,
           1.03308000e-01,   1.17884000e-02],
        [  9.89623000e-01,   4.75986000e-02,   2.72736000e-02,
           3.41016000e-02,   1.28349000e-01]])

In [ ]: