예제 #1
0
def train_dom_diff(A_plus, c, a_miss):
    """Find the best subset of criteria to apply the dominance estimator."""
    indices = list(range(len(A_plus[0])))
    missing = [i for i in range(len(a_miss)) if a_miss[i] == NULL]

    k_ss = helpers.powerset(indices)
    for j in missing:
        k_ss = [ss for ss in k_ss if j not in ss and len(ss) > 1
                ]  # to perfom tests on intervals!

    # print(k_ss)
    deltas = [0 for i in range(len(k_ss))]

    for l, ss in enumerate(k_ss):
        delta = estimate_by_dom_with_criteria(A_plus, c, a_miss, ss)[1]
        deltas[l] = delta

    i = 0
    # delta_2 = deltas[:]
    while i < len(deltas):
        if deltas[i] < 0:
            del deltas[i]
            del k_ss[i]
        else:
            i += 1

    try:
        best_ss_index = deltas.index(min(deltas))
    except:
        print('no deltas ...: no dominant neither dominee ...')
        return [i for i in range(len(a_miss)) if a_miss[i] != NULL]

    return k_ss[best_ss_index]
예제 #2
0
def check_train_dom(A):
    """Check to see wheter the training provides the best subset of indices."""
    # A_copy = copy.deepcopy(A)
    n = len(A)
    k = len(A[0])
    av_pos = 0
    # for tests purposis
    # for i in range(1):
    # ai = A[i][:]
    error_total_best_indices = 0
    error_total_all_ss = 0
    total_ss = 0
    total_best_ind = 0
    for i, ai in enumerate(A):
        for c, aic in enumerate(ai):
            # for c in range(1, 2):
            aic = ai[c]
            del A[i]
            best_ind = train_dom(A, c)

            # print('ok')

            all_ind_ss = helpers.powerset(list(range(k)))
            all_ind_ss = [
                ss for ss in all_ind_ss if c not in ss and len(ss) > 0
            ]

            error = []
            estimations = []
            for r, ss in enumerate(all_ind_ss):
                # helpers.printmatrix(A)
                est = estimate_by_dom_with_criteria(A, c, ai, ss)[0]
                estimations.append(est)
                error.append(abs(est - aic))

            error_total_best_indices += error[all_ind_ss.index(best_ind)]
            total_best_ind += 1
            error_total_all_ss += sum(error)
            total_ss += len(error)
            #            print('best indices', best_ind, 'value :', aic)
            #
            #            for ss, est, e in zip(all_ind_ss, estimations, error):
            #                print(ss, '::', est, '::', e)
            #
            all_ind_ss = sorted(all_ind_ss,
                                key=lambda ss: error[all_ind_ss.index(ss)])

            ss_num = len(all_ind_ss)
            #            print(len(all_ind_ss))
            av_pos += all_ind_ss.index(best_ind)

            A.insert(i, ai)

    print('av_pos', av_pos / (n), '/', ss_num)
    print('MSE best ind:', error_total_best_indices / total_best_ind)
    print('MSE all ind:', error_total_all_ss / total_ss)
예제 #3
0
def train_dom_knn(A_plus, c, a_miss, k):
    """Find the best subset of criteria to apply the dominance estimator."""
    # print('start training')
    indices = list(range(len(A_plus[0])))
    missing = [i for i in range(len(a_miss)) if a_miss[i] == NULL]
    # missing.append(c)           # needed for when a_miss has no real NULL

    k_ss = helpers.powerset(indices)
    for j in missing:
        k_ss = [ss for ss in k_ss if j not in ss and len(ss) > 1
                ]  # to perfom tests on intervals!

    i = 0
    while i < len(k_ss):
        ss = k_ss[i]
        if count_dominant_alts(A_plus, ss, a_miss) == 0:
            del k_ss[i]
        else:
            i += 1

    # print(k_ss)
    MSE = [0 for i in range(len(k_ss))]
    samples = [0 for i in range(len(k_ss))]

    for l, ss in enumerate(k_ss):
        for i, ai in enumerate(A_plus):
            old_ev = ai[c]
            ai[c] = NULL
            del A_plus[i]
            new_ev = estimate_by_dom_with_criteria_knn(A_plus, c, ai, ss, k)

            if type(new_ev) != str:
                MSE[l] += (old_ev - new_ev)**2
                samples[l] += 1
            # Restore A_plus as it was initially
            ai[c] = old_ev
            A_plus.insert(i, ai)

    for i, s in enumerate(samples):
        if s == 0:
            MSE[i] = max(MSE) + 1
        else:
            MSE[i] /= s

    # print(MSE)
    best_ss_index = MSE.index(min(MSE))
    # print('stop training')
    return k_ss[best_ss_index]
예제 #4
0
def find_minval_for_grand_coalition(game: dict, players):
    '''
    Find the smallest value that the grand coalition has to produce
    in order for a grand coalition to be formed.

    For every missing coalition S in game, it assumes that v(S) = 0
    '''
    min_payoff = 0

    # list of collections that result in the min_payoff
    max_result = []

    for coalition in helpers.powerset(players):
        if coalition not in game.keys():
            game[coalition] = 0

    collections = helpers.powerset_generator(game.keys())
    for collection in collections:
        if () in collection or tuple(players) in collection:
            continue
        print(collection)
        minimal, result = is_minimal_balanced(collection, players)
        if not minimal:
            continue

        sum = 0

        for key, val in result.items():
            coalition = tuple([
                int(x)
                for x in str(key).replace('{', '').replace('}', '').split(',')
            ])
            sum += (N(val) * game[coalition])

        if sum > min_payoff:
            min_payoff = sum
            max_result.clear()
        if sum == min_payoff:
            max_result.append(result)
    return min_payoff, max_result
예제 #5
0
import helpers

indices = list(range(6))

k_ss = helpers.powerset(indices)
k_ss = [ss for ss in k_ss if len(ss) > 1]  # to perfom tests on intervals!

print(k_ss)
예제 #6
0
    
    if len(sys.argv) < 2:
        print('Usage:')
        print('Find subcollections to given collection:\npython {} [1] [2] [1,2] ...\n'.format(sys.argv[0]))
        print('Find subcollections to amount of players:\npython {} -p 4'.format(sys.argv[0]))
        print('Note that anything from 5 and above may take ages to compute')
        sys.exit()
    
    
    if sys.argv[1].lower() == '-p':
        if len(sys.argv) < 3:
            print('Expected number of players after -p')
            sys.exit()
        
        players = [i for i in range(1,int(sys.argv[2])+1)]
        collection = helpers.powerset(players)
        collection.remove(())
        collection.remove(tuple(players))
    else:
        arg = ','.join(sys.argv[1:])
        arg = re.sub('[^\[\]\d,]', '', arg)
        arg = re.sub(',+', ',', arg)
        arg.replace('][', '],[')

        collection = ast.literal_eval(''.join(sys.argv[1:]).replace('][', '],['))
        collection = [tuple(x) for x in collection]

    for item in find_balanced_subcollections(collection):
        keys = [str(x) for x in item.keys()]
        values = [str(x) for x in item.values()]
        print('{{{}}} => ({})'.format(', '.join(keys), ', '.join(values)))
예제 #7
0
                for x in str(key).replace('{', '').replace('}', '').split(',')
            ])
            sum += (N(val) * game[coalition])

        if sum > min_payoff:
            min_payoff = sum
            max_result.clear()
        if sum == min_payoff:
            max_result.append(result)
    return min_payoff, max_result


if __name__ == '__main__':
    player_amount = int(input('Number of players participating: '))
    players = range(1, player_amount + 1)
    powerset = helpers.powerset(players)

    powerset.remove(())
    grand_coalition = tuple(players)
    powerset.remove(grand_coalition)
    print('v() = 0')

    game = {}

    for coalition in powerset:
        while True:
            prompt = 'v({}) = '.format(', '.join([str(x) for x in coalition]))
            try:
                value = float(input(prompt).replace(',', '.'))
                if value != 0:
                    game[coalition] = value