Exemplo n.º 1
0
def greedy_routing_annealing():
    A = Graph.from_saved('test/data/jazz')
    adj, vert = A.adj, A.vert
    num_targets, num_rounds, num_tries = 100, 10000
    distance = geometry.hyperboloid_distance
    vertex_list = list(vert)
    for _ in tqdm(range(num_rounds)):
        verts = random.sample(vertex_list, k=num_targets+1)
        source, targets = verts[0], verts[1:]
        neighbours = adj[source]
        r_source, theta_source = vert[source]['r'], vert[source]['theta']
        targets_coords = {target: (vert[target]['r'], vert[target]['theta']) for target in targets}
        neighbours_coords = {neighbour: (vert[neighbour]['r'], vert[neighbour]['theta']) for neighbour in neighbours}
        source_dist = {target: distance(target_x, target_y, r_source, theta_source) for target, (target_x, target_y) in targets_coords.items()}
        closest_neighbour_dist = {}
        for target, (target_x, target_y) in targets_coords.items():
            closest_dist = math.inf
            for neighbour, (neigh_x, neigh_y) in neighbours_coords.items():
                neigh_dist = distance(target_x, target_y, neigh_x, neigh_y)
                if neigh_dist < closest_dist:
                    closest_dist = neigh_dist
            closest_neighbour_dist[target] = closest_dist
        good = sum([closest_neighbour_dist[target] < source_dist[target] for target in targets])
        #r_source_new, theta_source_new = normalized_random_angular_move(r_source, theta_source)
        r_source_new, theta_source_new = normalized_random_move(r_source, theta_source)
        source_dist_new = {target: distance(target_x, target_y, r_source_new, theta_source_new) for target, (target_x, target_y) in targets_coords.items()}
        good_new = sum([closest_neighbour_dist[target] < source_dist_new[target] for target in targets])
        if good < good_new:
            A.vert[source]['r'] = r_source_new
            A.vert[source]['theta'] = theta_source_new
    print(utils.greedy_routing_score(A.adj, A.vert, representation='hyperboloid'))
    # 0.30126134440855257
    A.draw()