Ejemplo n.º 1
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    # MY_CODE
    map_graph = Digraph()
    with open(map_filename, 'r') as f:
        for line in f:
            src, dest, total_dist, outdoor_dist = line.split(' ')
            src = Node(src)
            dest = Node(dest)
            if not map_graph.has_node(src):
                map_graph.add_node(src)
            if not map_graph.has_node(dest):
                map_graph.add_node(dest)
            edge = WeightedEdge(src, dest, int(total_dist), int(outdoor_dist))
            map_graph.add_edge(edge)
    return map_graph
Ejemplo n.º 2
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    # TODO
    # print("Loading map from file...")
    file = open(map_filename, 'r')  # open the file
    g = Digraph()
    for line in file:  # read each line of the file
        line = line.strip('\n')  # remove the \n character
        line = line.split(' ')
        for i in range(0, 2):
            nod = Node(line[i])
            if not g.has_node(nod):
                g.add_node(nod)
        wei_edge = WeightedEdge(Node(line[0]), Node(line[1]), int(line[2]),
                                int(line[3]))
        g.add_edge(wei_edge)
    file.close()
    return g
Ejemplo n.º 3
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    # TODO
    print("Loading map from file...")

    digraph = Digraph()

    nodes = remove_duplicates(get_all_nodes_from_map_data(map_filename))
    edges = get_edges_from_map_data(map_filename)

    for each in nodes:
        digraph.add_node(each)
    for each in edges:
        digraph.add_edge(each)

    return digraph
Ejemplo n.º 4
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    #    print("Loading map from file...")
    inFile = open(map_filename, 'r')
    graph = Digraph()
    for line in inFile:
        linedata = line.split(' ')
        scr = Node(linedata[0])
        des = Node(linedata[1])
        graph.nodes.add(scr)
        graph.nodes.add(des)
        if not scr in graph.edges:
            graph.add_node(scr)
        if not des in graph.edges:
            graph.add_node(des)
        edge = WeightedEdge(scr, des, int(linedata[2]), int(linedata[3]))
        graph.add_edge(edge)
    return graph
Ejemplo n.º 5
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    g = Digraph()
    with open(map_filename) as f:
        data = f.read().strip()
    dataList = data.split('\n')
    for mapEdge in dataList:
        edgeList = mapEdge.split(' ')  # from to TD, DO
        fromN = Node(edgeList[0])
        toN = Node(edgeList[1])
        if not g.has_node(fromN):
            g.add_node(fromN)
        if not g.has_node(toN):
            g.add_node(toN)
        g.add_edge(WeightedEdge(fromN, toN, edgeList[2], edgeList[3]))
    return g
Ejemplo n.º 6
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    inFile = open(map_filename, 'r')
    digraph = Digraph()
    for line in inFile:
        mapEntry = line.split()  #mapEntry(list)
        src = Node(mapEntry[0])
        dest = Node(mapEntry[1])
        total_distance = int(mapEntry[2])
        outdoor_distance = int(mapEntry[3])
        edge = WeightedEdge(src, dest, total_distance, outdoor_distance)

        if not digraph.has_node(src):
            digraph.add_node(src)
        if not digraph.has_node(dest):
            digraph.add_node(dest)
        digraph.add_edge(edge)
    return digraph
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    graph = Digraph()
    with open(map_filename) as file:
        for line in file.readlines():
            el = line.strip('\n').split(' ')

            # Add nodes if they're not already in the graph
            for node in el[:2]:
                if not graph.has_node(node):
                    graph.add_node(node)

            graph.add_edge(WeightedEdge(*el))

    return graph
Ejemplo n.º 8
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    g = Digraph()
    with open(map_filename, "r") as file:
        for line in file:
            (src, dst, tot_dist, outdoor_dist) = line.split(' ')
            tot_dist = int(tot_dist)
            outdoor_dist = int(outdoor_dist)
            if not g.has_node(Node(src)):
                g.add_node(Node(src))
            if not g.has_node(Node(dst)):
                g.add_node(Node(dst))
            g.add_edge(WeightedEdge(src, dst, tot_dist, outdoor_dist))
    return g
Ejemplo n.º 9
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    print("Loading map from file...")
    graph = Digraph()
    f = open(map_filename, 'r')
    for line in f:
        line = line.split()
        src = Node(line[0])
        dst = Node(line[1])
        for node in [src, dst]:
            if not graph.has_node(node):
                graph.add_node(node)
        edge = WeightedEdge(src, dst, int(line[2]), int(line[3]))
        graph.add_edge(edge)
    f.close()
    return graph
Ejemplo n.º 10
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    graph = Digraph()  # 创建空图
    with open(map_filename) as file:
        for line in file:
            elements = line.split()  # 按空格分割成list
            src = Node(elements[0])
            dest = Node(elements[1])
            total_distance = int(elements[2])    # 数字类型
            outdoor_distance = int(elements[3])  # 数字类型

            if not graph.has_node(src):
                graph.add_node(src)
            if not graph.has_node(dest):
                graph.add_node(dest)
            graph.add_edge(WeightedEdge(src, dest, total_distance, outdoor_distance))

    return graph
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    campus_map = Digraph()
    with open(map_filename, 'r') as file_handle:
        for line in file_handle:  # 32 36 70 0
            info = line.strip().split(
            )  # info = ["32","36","70","0"] strip返回一个去掉前后端的line的副本
            from_node = Node(info[0])
            to_node = Node(info[1])
            if not campus_map.has_node(from_node):
                campus_map.add_node(from_node)
            if not campus_map.has_node(to_node):
                campus_map.add_node(to_node)
            cur_edge = WeightedEdge(from_node, to_node, int(info[2]),
                                    int(info[3]))
            # 假设cur_edge不存在重复的情况,也就是说mit_map.txt没有提供重复的数据
            campus_map.add_edge(cur_edge)
    return campus_map
Ejemplo n.º 12
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    # read map-file
    mapFile = open(map_filename, "r")
    mitGraph = Digraph()
    nodeMap = dict()
    # each entry
    for line in mapFile:
        # [src, dest, total dist, outdoot dist]
        info = line.strip("\n").split(" ")
        for i in range(2):
            if not info[i] in nodeMap:
                thisNode = Node(info[i])
                mitGraph.add_node(thisNode)
                nodeMap[info[i]] = thisNode
        mitGraph.add_edge(
            WeightedEdge(nodeMap[info[0]], nodeMap[info[1]], int(info[2]), int(info[3]))
        )
    return mitGraph
Ejemplo n.º 13
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    d = Digraph()
    with open(map_filename) as f:
        for line in f:
            data = line.split(' ')
            assert (len(data) == 4)

            src = Node(data[0])
            dest = Node(data[1])
            edge = WeightedEdge(src, dest, data[2], data[3])

            if not d.has_node(src):
                d.add_node(src)
            if not d.has_node(dest):
                d.add_node(dest)
            d.add_edge(edge)
        f.close()
    return d
Ejemplo n.º 14
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    graph = Digraph()
    with open(map_filename, 'r') as f1:
        list1 = f1.readlines()
    for item in list1:
        src, dest, total, outdoors = item[0:-1].split(' ')
        if (graph.has_node(Node(src)) == 0):
            graph.add_node(Node(src))
        if (graph.has_node(Node(dest)) == 0):
            graph.add_node(Node(dest))
        graph.add_edge(WeightedEdge(Node(src), Node(dest), total, outdoors))
    # TODO
    return graph
Ejemplo n.º 15
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    print("Loading map from file...")
    f=open(map_filename,'r')
    g=Digraph()
    for line in f:
        src, dest, total_distance, outdoor_distance = line.split()
        src_node=Node(src)
        dest_node=Node(dest)
        src_dest=WeightedEdge(src_node, dest_node, total_distance, outdoor_distance)
        if not g.has_node(src_node):
            g.add_node(src_node)
        if not g.has_node(dest_node):
            g.add_node(dest_node)
        g.add_edge(src_dest)
    f.close()
    return g
Ejemplo n.º 16
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    #print("Loading map from file...")
    campus_graph = Digraph()  # 实例化Digraph
    with open(map_filename) as file_object:
        lines = file_object.readlines()
    for line in lines:
        list = line.split()
        if not campus_graph.has_node(Node(list[0])):
            campus_graph.add_node(Node(list[0]))  # 若不在即加入
        if not campus_graph.has_node(Node(list[1])):
            campus_graph.add_node(Node(list[1]))
        campus_graph.add_edge(
            WeightedEdge(  # 将该边加入
                Node(list[0]), Node(list[1]), list[2], list[3]))
    return campus_graph
Ejemplo n.º 17
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    g = Digraph()  # Create a digraph object
    print("Loading map from file...")
    with open(map_filename, "r") as f:
        for passage in f:
            # For each entry, store four info
            From, To, TotalD, OutD = passage.split()
            node_src, node_dest = Node(From), Node(To)
            if not g.has_node(node_src):
                g.add_node(node_src)
            if not g.has_node(node_dest):
                g.add_node(node_dest)

            g.add_edge(
                WeightedEdge(node_src, node_dest, int(TotalD), int(OutD)))
    return g
Ejemplo n.º 18
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    print("\n\nLoading map from file...")

    g = Digraph()
    with open(map_filename) as file:
        for line in file:
            s, t, total, outdoor = line.split()
            s, t = map(Node, (s, t))

            if not g.has_node(s): g.add_node(s)
            if not g.has_node(t): g.add_node(t)

            edge = WeightedEdge(s, t, *map(int, (total, outdoor)))
            g.add_edge(edge)

    return g
Ejemplo n.º 19
0
    def buildGraph(mapEntries):
        g = Digraph()
        nodelist = []

        #createing nodelist and adding nodes
        for eachEntry in mapEntries:
            eachEntrySource = eachEntry[0]
            eachEntryDest = eachEntry[1]
            if eachEntrySource not in nodelist:  #making sure the node is unique
                nodelist.append(eachEntrySource)
                g.add_node(Node(eachEntrySource))
            if eachEntryDest not in nodelist:
                nodelist.append(eachEntryDest)
                g.add_node(Node(eachEntryDest))

        #creating edges
        for eachEntry in mapEntries:
            src = Node(eachEntry[0])  #eachEntrySource Node
            dest = Node(eachEntry[1])  #"eachEntryDest"
            tD = eachEntry[2]  #eachEntryTotalDistance
            oD = eachEntry[3]  #eachEntryOutdoorDistance
            g.add_edge(WeightedEdge(src, dest, tD,
                                    oD))  #Adding the weighted edge kind

        return g
Ejemplo n.º 20
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    print("Loading map from file...")
    openfile = open(map_filename, 'r')
    gph = Digraph()
    for line in openfile:
        NewLine = line.strip('\n').split(" ")
        a, b, c, d = NewLine
        a, b = Node(a), Node(b)
        c, d = int(c), int(d)
        if a not in gph.nodes:
            gph.add_node(a)
        if b not in gph.nodes:
            gph.add_node(b)
        DirEdge = WeightedEdge(a, b, c, d)
        gph.add_edge(DirEdge)
    openfile.close()
    return gph
Ejemplo n.º 21
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    digraph = Digraph()
    print("Loading map from file...")
    with open(map_filename, 'r') as file:
        for line in file:
            parts = line.split()
            src = Node(parts[0])
            dest = Node(parts[1])
            edge = WeightedEdge(src, dest, int(parts[2]), int(parts[3]))
            edge = WeightedEdge(src, dest, int(parts[2]), int(parts[3]))
            if not digraph.has_node(src):
                digraph.add_node(src)
            if not digraph.has_node(dest):
                digraph.add_node(dest)
            digraph.add_edge(edge)

    return digraph
Ejemplo n.º 22
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    digraph = Digraph()
    count = 0
    with open(map_filename, 'r') as file:
        for line in file:
            line_list = line.split(' ')
            count += 1
            src = Node(line_list[0])
            dest = Node(line_list[1])
            if not digraph.has_node(src): digraph.add_node(src)
            if not digraph.has_node(dest): digraph.add_node(dest)
            weighted_edge = WeightedEdge(src, dest, line_list[2], line_list[3])
            digraph.add_edge(weighted_edge)
    return digraph
Ejemplo n.º 23
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    # TODO
    print("Loading map from file...")
    f = open(map_filename)
    line_list = []
    for line in f:
        clean_line = line.rstrip()
        line_list.append(clean_line)

    f.close()

    src_li = []
    dest_li = []
    weighted_edge_li = []

    for string in line_list:
        param = string.split(' ')
        src_li.append(Node(param[0]))
        dest_li.append(Node(param[1]))
        edge = WeightedEdge(src_li[-1], dest_li[-1], int(param[2]),
                            int(param[3]))
        weighted_edge_li.append(edge)

    map = Digraph()
    for i in range(len(src_li)):
        try:
            map.add_node(src_li[i])
            map.add_node(dest_li[i])
        except ValueError as error:
            print(error)
            continue  # will go next iter of this for-loop directly, skipping any code below it

    for i in range(len(src_li)):
        try:
            map.add_edge(weighted_edge_li[i])
        except ValueError as error1:
            print(error1)
            continue

    return map
Ejemplo n.º 24
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    # TODO
    print("Loading map from file...")
    #     Format for the graph file:
    #     Starting node, destination node, total weight of edge (distance), total distance spent outside
    # So steps:
    # 1. Create graph object
    # For every line:
    #  0. Save all numbers in variables (source, destination, total_distance, outdoor_distance)
    #  1. Create source_node object
    #  2.Create destination node object
    #  3. If graph has not(source_node) object:
    #       add_node to graph
    #     else get node from graph and save in src variable
    #     Do the same for destination object
    # 4. Create weightedEdge object from variables and objects
    # 5. Add edge to graph
    # Problem 2c: Testing load_map
    # Include the lines used to test load_map below, but comment them out
    g = Digraph()
    with open(map_filename) as f:
        for line in f:
            input = line.split()
            source = input[0]
            destination = input[1]
            total_distance = input[2]
            outdoor_distance = input[3]
            src_object = Node(source)
            dest_object = Node(destination)
            if not (g.has_node(src_object)):
                g.add_node(src_object)
            if not (g.has_node(dest_object)):
                g.add_node(dest_object)
            edge_object = WeightedEdge(src_object, dest_object, total_distance,
                                       outdoor_distance)
            if edge_object not in g.get_edges_for_node(src_object):
                g.add_edge(edge_object)

    return g
Ejemplo n.º 25
0
Archivo: ps2.py Proyecto: sco1/6-0002
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    print("Loading map from file...")
    # Open map_filename for reading and parse each line to get the edge parameters
    # Assumes map_filename is properly formatted
    newdigraph = Digraph()  # Initialize new digraph
    with open(map_filename, mode='r') as fID:
        for tline in fID:
            # Split on space
            edge_params = tline.split(' ')

            # edge_params will have the following format:
            #     edge_params[0]: Source Node, string
            #     edge_params[1]: Destination Node, string
            #     edge_params[2]: Total Distance, int
            #     edge_params[3]: Distance Outdoors, int
            for idx, param in enumerate(edge_params):
                if idx < 2:
                    # Node names, staying as strings. Strip newline
                    edge_params[idx] = edge_params[idx].replace('/n', '')
                else:
                    # Distances, cast as integers. This strips the newline
                    edge_params[idx] = int(edge_params[idx])

            # Check to see if our source node is in the digraph we're generating,
            # add if it's not
            if not newdigraph.has_node(edge_params[0]):
                newdigraph.add_node(edge_params[0])

            # Check to see if our destination node is in the digraph we're
            # generating, add if it's not
            if not newdigraph.has_node(edge_params[1]):
                newdigraph.add_node(edge_params[1])

            newdigraph.add_edge(
                WeightedEdge(edge_params[0], edge_params[1], edge_params[2],
                             edge_params[3]))

    return newdigraph
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    
    print("Loading map from file...")

#   Create a new graph
    graph = Digraph()

#   Read in the data from the file
    with open(map_filename, 'r') as map_data:        

        for line in map_data:
            
        # Split the line by '' to isolate the 4 variables of interest                    
            line_data = line.split()
            
            src = Node(line_data[0])
            dest = Node(line_data[1])
            
            total_distance = int(line_data[2])
            outdoor_distance = int(line_data[3])
            
        # Create a weighted edge from src to dest             
            new_wedge = WeightedEdge(src, dest, total_distance, outdoor_distance)
        
        #If src or dest nodes not graph, then create them            
            if not graph.has_node(src):
                graph.add_node(src)
            
            if not graph.has_node(dest):
                graph.add_node(dest)
        
#           Add the new edge to the grapg        
            graph.edges[src].append(new_wedge)
        
    return graph
Ejemplo n.º 27
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    # Create the empty digraph.
    g = Digraph()

    #Creating a node look up dictionary, so that 2 nodes are not created with the name 'a' that look the same
    # but do not set off the duplicate warnings.
    nodes = {}

    print("Loading map from file...")
    loaded_data = []
    with open(map_filename, 'r') as f:
        loaded_data = f.read()

    # Split the loaded data at the newlines.
    loaded_data = loaded_data.split("\n")
    for path in loaded_data:
        if not path == "":  # Avoid any blank lines.
            source, destination, total_dist, outdoor_dist = path.split(' ')
            # Check if we've already made a node for the source and destination
            if not source in nodes.keys():
                nodes[source] = Node(source)
            if not destination in nodes.keys():
                nodes[destination] = Node(destination)

            # If the nodes are not in the nodes list, add them
            if not nodes[source] in g.nodes:
                g.add_node(nodes[source])
            if not nodes[destination] in g.nodes:
                g.add_node(nodes[destination])

            g.add_edge(
                WeightedEdge(nodes[source], nodes[destination], total_dist,
                             outdoor_dist))
    return g
Ejemplo n.º 28
0
def load_map(map_filename):
    g = Digraph()
    infile = open(map_filename, "r")

    for line in infile:
        coord = line.split()
        n1 = Node(coord[0])
        n2 = Node(coord[1])
        e = WeightedEdge(n1, n2, coord[2], coord[3])
        if (not g.has_node(n1)): g.add_node(n1)
        if (not g.has_node(n2)): g.add_node(n2)
        g.add_edge(e)
    return g
Ejemplo n.º 29
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    # initialize a digraph g representing the map
    g = Digraph()
    # open the file contains map data
    with open(map_filename) as file:
        # read data per line and store it into a list
        read_data = file.read().split("\n")
        # the list contains an unnecessary newline character at the end
        read_data = read_data[:-1]
        # loop through each entry in data list
        for entry in read_data:
            # split each entry into source, destination nodes, total_distance and outdoor_distance
            # and store them into a list respectively
            raw_data = entry.split(" ")
            # src = source node, dest = destination, total = total_distance, outdoor = outdoor_distance
            src = Node(raw_data[0])
            dest = Node(raw_data[1])
            total = int(raw_data[2])
            outdoor = int(raw_data[3])
            # check if g already has node src or not
            if not g.has_node(src):
                g.add_node(src)
            # check if g already has node dest or not
            if not g.has_node(dest):
                g.add_node(dest)
            # initialize a weighted edge
            edge = WeightedEdge(src, dest, total, outdoor)
            # add above edge into g
            g.add_edge(edge)
    # close file
    file.close()
    # return the desired directed graph
    return g
Ejemplo n.º 30
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    with open(map_filename) as f:
        mit_digraph = Digraph()
        for line in f:
            #create a list of node names and distances
            if '\n' in line:
                sliced_line = line[0:len(line) - 1]
                node_edge_distance_list = sliced_line.split(' ')
            else:
                node_edge_distance_list = sliced_line.split(' ')

            #create nodes and the edge
            src = Node(node_edge_distance_list[0])
            dest = Node(node_edge_distance_list[1])

            total_distance = int(node_edge_distance_list[2])
            outdoor_distance = int(node_edge_distance_list[3])
            edge = WeightedEdge(src, dest, total_distance, outdoor_distance)

            #add nodes and edge to graph
            try:
                mit_digraph.add_node(src)
            except ValueError:
                pass

            try:
                mit_digraph.add_node(dest)
            except ValueError:
                pass

            mit_digraph.add_edge(edge)

    return mit_digraph