Exemple #1
0
def read_city_graph(filename):
    f = open(filename)
    g = WeightedGraph()
    global vertices
    vertices = {}
    global edges
    edges = {}

    line = f.readline()
    while line:
        fields = line.split(",")

        if fields[0] == "V":
            v = int(fields[1])
            lat = float(fields[2])
            lon = float(fields[3])
            g.add_vertex(v)
            vertices[v] = (int(lat * 100000), int(lon * 100000) )
        elif fields[0] == "E":
            e = (int(fields[1]), int(fields[2]))
            g.add_edge(int(fields[1]),int(fields[2]))
            edges[e] = {"street": fields[3]}
        line = f.readline()

    return g
def read_city_graph(filename):
    f = open(filename)
    g = WeightedGraph()
    global vertecies
    vertecies = {}
    global edges
    edges = {}

    line = f.readline()
    while line:
        fields = line.split(",")

        if fields[0] == "V":
            v = int(fields[1])
            lat = float(fields[2])
            lon = float(fields[3])
            g.add_vertex(v)
            vertecies[v] = (int(lat * 100000), int(lon * 100000))
        elif fields[0] == "E":
            e = (int(fields[1]), int(fields[2]))
            g.add_edge(int(fields[1]), int(fields[2]))
            edges[e] = {"street": fields[3]}
        line = f.readline()

    return g
def run_tournament(game: PDGame) -> None:
    """Run a tournament between all strategies.

    If <show_heatmap> is set, then display a heatmap that shows the match-ups
    between the strategies.
    """

    all_strategies = get_all_strategies()

    graph = WeightedGraph()
    for s1 in all_strategies:
        for s2 in all_strategies:
            new_game = PDGame(game.num_rounds)
            strategy1 = s1.__copy__()
            strategy2 = s2.__copy__()

            player1 = Player(strategy1, 1)
            player2 = Player(strategy2, 2)

            if isinstance(player1.strategy, LearningStrategy):
                player1 = get_trained_learner(player2, game.num_rounds)

            graph.add_vertex(player1.strategy.name)

            graph.add_vertex(player2.strategy.name)

            run_game(new_game, player1, player2)
            if strategy1.name == 'Learning Strategy' and strategy2.name == 'Learning Strategy':
                player1.curr_points, player2.curr_points = 0, 0

            graph.add_edge((player1.strategy.name, player1.curr_points),
                           (player2.strategy.name, player2.curr_points))

    display_heatmap(graph)
Exemple #4
0
def read_weighted_graph_file(path):
    lines = [ [ tuple.split(",") for tuple in line.split() ] \
              for line in open(path).readlines() ]

    graph = WeightedGraph(len(lines))
    for line in lines:
        v = int(line[0][0]) - 1
        for edge in line[1:]:
            w    = int(edge[0]) - 1
            cost = int(edge[1])
            graph.add_edge(v, w, cost)

    return graph
def read_city_graph(filename):
    #set the three variables we are gonna output
    EdGraph = WeightedGraph()
    roadNames = []
    locations = {}
    """
    Goes through each line of the file
    and creates a vertice or an edge
    """
    with open(filename,"r",) as file:
        for line in file:
            line = line.split(",")

            if line[0] == "V":
                EdGraph.add_vertex(int(line[1]))
                #appends the information we need about the vertice
                locations[int(line[1])] = [int(float(line[2])*100000),int(float(line[3])*100000)]
            elif line[0] == "E":
                EdGraph.add_edge(int(line[1]), int(line[2]), cost_distance(locations[int(line[1])],
                                                                           locations[int(line[2])]))
                roadNames.append([int(line[1]), int(line[2]), line[3].strip()])

    return EdGraph, roadNames, locations

def kruskal_algo(graph):
    ds = DisjointSet()
    result = []
    edges = graph.get_edges()
    map(ds.make_set, graph.get_vertex())
    for edge, _ in (x for x in sorted(edges.items(), key=lambda (key, value): value)):
        node1 = ds.find_set(edge[0])
        node2 = ds.find_set(edge[1])
        if node1 == node2:
            continue
        else:
            result.append("{0}{1}".format(*edge))
            ds.union(*edge)
    return result


if __name__ == "__main__":
    graph = WeightedGraph(directed=False)
    graph.add_edge('A', 'B', 3)
    graph.add_edge('A', 'D', 1)
    graph.add_edge('B', 'C', 1)
    graph.add_edge('B', 'D', 3)
    graph.add_edge('C', 'D', 1)
    graph.add_edge('C', 'E', 5)
    graph.add_edge('C', 'F', 4)
    graph.add_edge('D', 'E', 6)
    graph.add_edge('F', 'E', 2)
    print kruskal_algo(graph)
Exemple #7
0
g.add_edge(('D', 'E'))
g.add_edge(('E', 'F'))
g.add_edge(('F', 'G'))
g.add_edge(('G', 'H'))
g.add_edge(('H', 'I'))
g.add_edge(('I', 'J'))
g.add_edge(('J', 'A'))
g.add_edge(('C', 'H'))

depth_first_search_traversal(graph=g.graph_dict, starting_vertex='A', goal_vertex='F', verbose=True)

path = breadth_first_search_find_path(graph=g.graph_dict, starting_vertex='A', goal_vertex='F')
print(path)


wg = WeightedGraph()
wg.add_edge(('A', 'B'), 1)
wg.add_edge(('B', 'C'), 6)
wg.add_edge(('C', 'D'), 3)
wg.add_edge(('D', 'E'), 8)
wg.add_edge(('E', 'F'), 2)
wg.add_edge(('F', 'G'), 5)
wg.add_edge(('G', 'H'), 1)
wg.add_edge(('H', 'I'), 8)
wg.add_edge(('I', 'J'), 4)
wg.add_edge(('J', 'A'), 3)
wg.add_edge(('C', 'H'), 2)

cost, path = djikstra(wg.graph_dict, 'A', 'F', True)
print(cost, path)
    g.add_edge(('A', 'B'))
    g.add_edge(('B', 'C'))
    g.add_edge(('C', 'D'))
    g.add_edge(('D', 'E'))
    g.add_edge(('E', 'F'))
    g.add_edge(('F', 'G'))
    g.add_edge(('G', 'H'))
    g.add_edge(('H', 'I'))
    g.add_edge(('I', 'J'))
    g.add_edge(('J', 'A'))
    g.add_edge(('C', 'H'))

    depth_first_search_traversal(graph=g.graph_dict, starting_vertex='A', goal_vertex='F', verbose=True)

    path = breadth_first_search_find_path(graph=g.graph_dict, starting_vertex='A', goal_vertex='F', verbose=True)
    print(path)

    wg = WeightedGraph()
    wg.add_edge(('A', 'B'), 7)
    wg.add_edge(('B', 'C'), 10)
    wg.add_edge(('A', 'C'), 9)
    wg.add_edge(('B', 'D'), 15)
    wg.add_edge(('C', 'D'), 11)
    wg.add_edge(('A', 'E'), 14)
    wg.add_edge(('C', 'E'), 2)
    wg.add_edge(('E', 'F'), 9)
    wg.add_edge(('F', 'D'), 6)

    cost, path = djikstra(wg.graph_dict, 'A', 'F', True)
    print(cost, path)
    hm = HeapMap(data=min_heap_dic)
    while not hm.is_empty_heap():
        hm.build_min_heapify()
        vertex, value = hm.pop_min_element()
        distance[vertex] = value
        for edge in graph[vertex]:
            if edge in min_heap_dic and graph[vertex][edge] + distance.get(vertex, 0) < min_heap_dic[edge]:
                min_heap_dic[edge] = graph[vertex][edge] + distance.get(vertex, 0)
                path[edge] = vertex

    return path, distance


if __name__ == "__main__":
    graph = WeightedGraph(directed=False)
    graph.add_edge(0, 1, 4)
    graph.add_edge(0, 7, 8)
    graph.add_edge(1, 2, 8)
    graph.add_edge(1, 7, 11)
    graph.add_edge(2, 3, 7)
    graph.add_edge(2, 8, 2)
    graph.add_edge(2, 5, 4)
    graph.add_edge(3, 4, 9)
    graph.add_edge(3, 5, 14)
    graph.add_edge(4, 5, 10)
    graph.add_edge(5, 6, 2)
    graph.add_edge(6, 7, 1)
    graph.add_edge(6, 8, 6)
    graph.add_edge(7, 8, 7)

    path, distance = dijkastra_algo(graph.get_graph(), 0)