def test_find_shortest_path(): nodes = [Node(x=0, y=0), Node(x=0, y=1), Node(x=1, y=1), Node(x=1, y=0)] graph = Graph(nodes) assert graph.find_shortest_path()[1] == 4
i += 1 plt.annotate('({0})'.format(i), xy=(x, y), xytext=(0, -5), textcoords='offset points', xycoords='data', ha='center', va='top') nodes = [Node(z.x, z.y) for z in df.itertuples()] graph = Graph(nodes, alpha=1, beta=5, decay=0.2) # path, distance = graph.find_shortest_path(n=1, m=28) d_list = [] n_list = list(range(0, 1001, 10)) shortest = 100000 for n in n_list: path, distance = graph.find_shortest_path(n=n, m=21) if distance < shortest: shortest = distance path_shortest = path d_list.append(distance) fig2 = plt.figure() plt.plot(n_list, d_list, 'r-') sum = 0 for i in range(len(path_shortest) - 1): x1, y1 = df.loc[path_shortest[i]] x2, y2 = df.loc[path_shortest[i + 1]] real_dis = haversine(x1, y1, x2, y2) sum += real_dis x0, y0 = df.loc[path_shortest[0]] xn, yn = df.loc[path_shortest[-1]]
from ant_colony.graph import Node, Graph nodes = [Node(0, 0), Node(0, 1), Node(1, 1), Node(1, 0)] graph = Graph(nodes) path, distance = graph.find_shortest_path() print("Path:") print(path) print("Distance:") print(distance)