コード例 #1
0
ファイル: test.py プロジェクト: conorpreid/udacity_dsa
def test(pickle_file, answers):
    """
    Testing function for our shortest path solution.

    Args:
        pickle_file: A given pickle file representing a graph.
        answers: a list of tuples representing the expected output.

    Returns:
        Prints wether the test passed or not.
    """
    print('Testing: ', pickle_file)
    map = load_map(pickle_file)
    correct = 0
    for start, goal, answer_path in answers:
        path = shortest_path.get_shortest_path(map, start, goal)
        if path == answer_path:
            correct += 1
        else:
            print("For start:", start, "Goal:     ", goal, "Your path:", path,
                  "Correct:  ", answer_path)
    if correct == len(answers):
        print("All tests pass! Congratulations!")
    else:
        print("You passed", correct, "/", len(answers), "test cases")
    print('----------------------------------')
コード例 #2
0
def getDistance(station1, station2):
    if station1 in distance_dct and station2 in distance_dct[station1]:
        #print("cache")
        return None, distance_dct[station1][station2]

    if station2 in distance_dct and station1 in distance_dct[station2]:
        #print("cache")
        return None, distance_dct[station2][station1]

    distance = 0
    '''
	if stations_shortest_path_dic is not None:
		stations_shortest_path_dic = sp.get_all_stations_spt_dic_from_file()
		distance, path = sp.get_shortest_path(station1, station2,
			 station_info_dic,stations_shortest_path_dic)
	if distance is None or path is None:
		path, distance = internal_get_spt_from_stat_name(station1, station2)
	'''
    global stations_shortest_path_dic
    if stations_shortest_path_dic is not None:
        distance, path = sp.get_shortest_path(station1, station2,
                                              station_info_dic,
                                              stations_shortest_path_dic)
    else:
        print("stations_shortest_path_dic is None")
        path, distance = internal_get_spt_from_stat_name(station1, station2)
    if station1 not in distance_dct:
        distance_dct[station1] = {}

    distance_dct[station1][station2] = distance
    return None, distance
コード例 #3
0
ファイル: cluster.py プロジェクト: collincr/gravrouting
def computeDisMetrix():
    staion_dic = get_station_dic()
    id_name = {}
    id = 0
    for station in staion_dic:
        id_name[staion_dic[station]['name']] = id
        id += 1

    dis_metrix = [([0] * len(id_name)) for i in range(len(id_name))]

    road_network_dic, station_info_dic = sp.preprocess()
    stations_shortest_path_dic = sp.get_all_stations_spt_dic_from_file()

    for station in id_name:
        for station1 in id_name:
            if station is not station1:
                distance = 0
                print(station + "#" + station1)
                distance, path = sp.get_shortest_path(
                    station, station1, station_info_dic,
                    stations_shortest_path_dic)
                if distance == None:
                    _, distance = internal_get_spt_from_stat_name(
                        station, station1)

                dis_metrix[id_name[station]][id_name[station1]] = distance
                dis_metrix[id_name[station1]][id_name[station]] = distance

    return dis_metrix
コード例 #4
0
    def test_k3(self):
        """ Tests the function for the complete graph with 3 vertices. """
        k3 = {"A": ["B", "C"], "B": ["A", "C"], "C": ["A", "B"]}

        # The shortest path from a node to itself should be just that node.
        self.assertEqual(get_shortest_path(k3, "A", "A"), ["A"])
        self.assertEqual(get_shortest_path(k3, "B", "B"), ["B"])
        self.assertEqual(get_shortest_path(k3, "C", "C"), ["C"])

        # All nodes are connected to each other by a path of length 0.
        self.assertEqual(get_shortest_path(k3, "A", "B"), ["A", "B"])
        self.assertEqual(get_shortest_path(k3, "B", "A"), ["B", "A"])

        self.assertEqual(get_shortest_path(k3, "A", "C"), ["A", "C"])
        self.assertEqual(get_shortest_path(k3, "C", "A"), ["C", "A"])

        self.assertEqual(get_shortest_path(k3, "B", "C"), ["B", "C"])
        self.assertEqual(get_shortest_path(k3, "C", "B"), ["C", "B"])
コード例 #5
0
def compute_matrix_row(departure, provincial_capital_station, edges):
    temp = [departure[0] + ' ' + departure[1]]
    for destination in provincial_capital_station:
        try:
            temp.append(
                format(
                    int(
                        shortest_path.get_shortest_path(
                            edges, departure[1], destination[1])[0]) / 60,
                    '0.1f'))
        except ValueError:
            temp.append('无法到达')
    return temp
コード例 #6
0
def main():
    road_network_dic, station_info_dic = sp.preprocess()
    stations_shortest_path_dic = sp.get_all_stations_spt_dic_from_file()
    stat1 = 'B-14'
    stat2 = 'B-15'
    if stations_shortest_path_dic is not None:
        distance, path = sp.get_shortest_path(stat1, stat2, station_info_dic,
                stations_shortest_path_dic)
        print('Shortest distance (meter)', distance)
        print('Shortest path', end = ": ")
        for road in path:
            print(road_network_dic[road]['coordinates'], end =", ")
    else:
        print('Please run shortest_path.py to generate the file')
コード例 #7
0
    def test_invalid_inputs(self):
        """ Test the case where the graph is empty. """
        # If the graph is empty, the vertices are invalid.
        with self.assertRaises(AssertionError):
            get_shortest_path({}, "a", "b")

        # If we have a singleton, it is still invalid.
        with self.assertRaises(AssertionError):
            get_shortest_path({"A": []}, "A", "B")

        # If we have a graph with multiple nodes, but the vertices are invalid.
        with self.assertRaises(AssertionError):
            get_shortest_path({"A": [], "B": []}, "A", "Z")
コード例 #8
0
    def test_medium_1(self):
        """ Tests the function with a slightly larger graph from wikipedia. """
        # This graph is from: http://en.wikipedia.org/wiki/File:6n-graf.svg
        graph = {1: [5, 2],
                 2: [1, 3, 5],
                 3: [2, 4],
                 4: [3, 5, 6],
                 5: [1, 2, 4],
                 6: [4]}

        # Test some paths of length 0.
        self.assertEqual(get_shortest_path(graph, 1, 1), [1])
        self.assertEqual(get_shortest_path(graph, 5, 5), [5])

        # Test some paths of length 1.
        self.assertEqual(get_shortest_path(graph, 1, 2), [1, 2])
        self.assertEqual(get_shortest_path(graph, 5, 1), [5, 1])
        self.assertEqual(get_shortest_path(graph, 6, 4), [6, 4])

        # Test some paths of length 2.
        self.assertEqual(get_shortest_path(graph, 1, 4), [1, 5, 4])
        self.assertEqual(get_shortest_path(graph, 3, 6), [3, 4, 6])
        self.assertIn(get_shortest_path(graph, 5, 3), [[5, 2, 3], [5, 4, 3]])
        self.assertIn(get_shortest_path(graph, 4, 2), [[4, 5, 2], [4, 3, 2]])

        # Test some paths of length 3.
        self.assertEqual(get_shortest_path(graph, 1, 6), [1, 5, 4, 6])
        self.assertEqual(get_shortest_path(graph, 6, 1), [6, 4, 5, 1])

        self.assertIn(get_shortest_path(graph, 6, 2),
                      [[6, 4, 3, 2], [6, 4, 5, 2]])