示例#1
0
def calculate_ranks(data_dict, weights, foundation=None):
    ranks = [x for x in data_dict.keys()]
    if not foundation:
        foundation = [0] * data_dict[ranks[0]].__len__()
    print('Initial:\t%f' % score(data_dict, ranks, weights, foundation))

    ranks = best_first(data_dict, weights, foundation)
    print('BestFirst:\t%f' % score(data_dict, ranks, weights, foundation))

    ranks = two_opt(data_dict, weights, ranks, foundation)
    print('TwoOpt:\t%f' % score(data_dict, ranks, weights, foundation))

    return ranks
示例#2
0
def run():
    data_dict = {}
    ignore_cols = int(sys.argv[2])
    with open(sys.argv[1]) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        line_count = 0
        categories = []

        for row in csv_reader:
            row_data = row[ignore_cols:]
            if line_count == 0:
                categories = row_data
                line_count += 1
                continue
            if line_count == 1:
                for i in range(row_data.__len__()):
                    data_dict[categories[i]] = [float(row_data[i])]
                line_count += 1
                continue

            for i in range(row_data.__len__()):
                data_dict[categories[i]].append(float(row_data[i]))
            line_count += 1

    silhouette = None
    for value in data_dict.values():
        if not silhouette:
            silhouette = value
        else:
            silhouette = add_vectors(silhouette, value)

    weight_exponent = settings.significance
    total_sum = sum([sum([x ** weight_exponent for x in layer]) for layer in data_dict.values()])

    weights = {
        'min_improvement': settings.min_improvement,
        'fda': settings.flatness,
        'sda': settings.straightness,
        'fdr': settings.continuity,
        'bottom_line': settings.bottom_line,
        'middle_line': settings.middle_line,
        'top_line': settings.top_line,
        'weight_exponent': weight_exponent,
        'total_sum': total_sum,
        'silhouette': silhouette,
    }

    line_weight_sum = weights['bottom_line'] + weights['middle_line'] + weights['top_line']
    weights['bottom_line'] /= line_weight_sum
    weights['middle_line'] /= line_weight_sum
    weights['top_line'] /= line_weight_sum

    ranks = calculate_ranks(data_dict, weights)
    foundation = [0] * data_dict[ranks[0]].__len__()

    destination_file = open(sys.argv[3], 'w')
    write_ranks(ranks, destination_file)
    print('Score: %f' % score(data_dict, ranks, weights, foundation))
示例#3
0
def calculate_ranks(data_dict, weights, foundation=None):
    ranks = [x for x in data_dict.keys()]
    if not foundation:
        foundation = [0] * data_dict[ranks[0]].__len__()
    print("%d layers" % len(ranks))
    print('Initial:\t%f' % score(data_dict, ranks, weights, foundation))

    ranks, foundation = upwards_opt(data_dict, weights, ranks, foundation)
    return ranks, foundation
示例#4
0
def upwards_opt(data_dict, weights, ranks, foundation):
    n = len(ranks)
    old_cost = None
    passes = 0
    swaps = 0
    while not old_cost or score(
            data_dict, ranks, weights,
            foundation) < old_cost * (1 - weights['min_improvement']):
        old_cost = score(data_dict, ranks, weights, foundation)
        i = 0
        visited = []
        while i < n:
            if visited.__contains__(i):
                print("Skip layer %d" % i)
                i += 1
                continue
            new_index = find_best_position(i, data_dict, ranks, weights,
                                           foundation)
            if new_index != i:
                # Move the layer
                layer = ranks.pop(i)
                ranks.insert(new_index, layer)
                swaps += 1
                current_score = score(data_dict, ranks, weights, foundation)
                print(
                    'Move layer %d \tto %d\t Swaps: %d\tPasses: %d\tNew cost: %.3f\tImpr.: %.3f'
                    % (i, new_index, swaps, passes, current_score,
                       (1 - (current_score / old_cost)) * 100))
            else:
                print('Stay layer %d' % i)
            if new_index <= i:
                i += 1
            else:
                # Update visited indices accordingly
                for j in range(len(visited)):
                    v = visited[j]
                    if i < v <= new_index:
                        visited[j] -= 1

                # Insert newIndex
                visited.append(new_index)
        passes += 1
    return ranks, foundation
示例#5
0
def two_opt(data_dict, weights, ranks, foundation):
    swaps = 0
    foundations = []
    for rank in ranks:
        foundations.append(foundation)
        foundation = add_vectors(foundation, data_dict[rank])

    best_score = score(data_dict, ranks, weights, foundation) * 2
    while score(data_dict, ranks, weights, foundation) < best_score:
        best_score = score(data_dict, ranks, weights, foundation)
        for i in range(1, len(ranks)):
            current_score = (layer_score(data_dict[ranks[i]], foundations[i], weights) +
                             layer_score(data_dict[ranks[i - 1]], foundations[i - 1], weights))
            swap_foundation = add_vectors(foundations[i - 1], data_dict[ranks[i]])
            swap_score = (layer_score(data_dict[ranks[i - 1]], swap_foundation, weights) +
                          layer_score(data_dict[ranks[i]], foundations[i - 1], weights))

            if swap_score < current_score:
                apply_moves(ranks, foundations, data_dict, {(i, 1): current_score - swap_score})
                swaps += 1
                print("Swappity swap # %d" % swaps)

    return ranks
示例#6
0
def run():
    num_start_cols = int(sys.argv[2])
    start_cols = []
    with open(sys.argv[1]) as csv_file:
        # csv_reader = csv.reader(csv_file, delimiter=',')
        row = list(csv.reader([csv_file.readline()]))[0]
        start_cols.append(row[:num_start_cols])
        line_count = 0
        categories = row[num_start_cols:]
        data_dict = {categories[i]: [] for i in range(len(categories))}

        while row:
            row = list(csv.reader([csv_file.readline()]))[0]
            start_cols.append(row[:num_start_cols])
            row_data = row[num_start_cols:]
            for i in range(len(row_data)):
                data_dict[categories[i]].append(float(row_data[i]))

            line_count += 1

    silhouette = None
    for value in data_dict.values():
        if not silhouette:
            silhouette = value
        else:
            silhouette = add_vectors(silhouette, value)

    weight_exponent = settings.significance
    total_sum = sum([
        sum([x**weight_exponent for x in layer])
        for layer in data_dict.values()
    ])

    weights = {
        'min_improvement': settings.min_improvement,
        'fda': settings.flatness,
        'sda': settings.straightness,
        'fdr': settings.continuity,
        'bottom_line': settings.bottom_line,
        'middle_line': settings.middle_line,
        'top_line': settings.top_line,
        'weight_exponent': weight_exponent,
        'total_sum': total_sum,
        'silhouette': silhouette,
    }

    line_weight_sum = weights['bottom_line'] + weights[
        'middle_line'] + weights['top_line']
    weights['bottom_line'] /= line_weight_sum
    weights['middle_line'] /= line_weight_sum
    weights['top_line'] /= line_weight_sum

    silhouette_max = max(silhouette)
    ranks, foundation = calculate_ranks(data_dict, weights)
    foundation_min = min(foundation)
    foundation = [
        x - foundation_min + silhouette_max * 0.05 for x in foundation
    ]

    destination_file = open(sys.argv[3], 'w')
    write_ranks(ranks, destination_file)
    print('Score: %f' % score(data_dict, ranks, weights, foundation))