Exemplo n.º 1
0
def astar_wait():
    size = 10
    _grid = np.zeros([size, size, size * 2])
    for i in range(int(np.round(pow(size, 2) * .3))):
        _grid[rand_coords(size, size)] = -1

    start = (rand_coords(size, size) + (0, ))
    while _grid[start] == -1:
        start = (rand_coords(size, size) + (0, ))

    goal = (rand_coords(size, size) + (size * 2 - 1, ))
    while _grid[goal] == -1:
        goal = (rand_coords(size, size) + (size * 2 - 1, ))

    startt = datetime.datetime.now()

    try:
        path = astar_grid48con.astar_grid4con(start, goal, _grid)
    except NoPathException as e:
        print("Could not find path")
        return 0, (datetime.datetime.now() - startt).total_seconds()

    t = (datetime.datetime.now() - startt).total_seconds()

    for i in range(len(path) - 1):
        d = np.array(path[i]) - np.array(path[i + 1])
        assert not (d[0] == 0 and d[1] == 0), "Waiting in place is baaaaad"

    return astar_grid48con.path_length(path), t
Exemplo n.º 2
0
        for d in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0),
                  (1, 1)]:
            checking = (d[0] + n[0], d[1] + n[1])
            if G.has_node(checking):
                if not G.has_edge(n, checking):
                    G.add_edge(n, checking)
                G[n][checking]['weight'] = cost(n, checking)

t = timeit.Timer()
t.timeit()

path = nx.astar_path(G, start, goal, cost)

print("computation time:", t.repeat(), "s")

print("length: ", astar_grid48con.path_length(path))

fig, ax = plt.subplots()

ax.imshow(map.T, cmap='Greys', interpolation='nearest')
ax.set_title('astar path')
ax.axis([0, map.shape[0], 0, map.shape[1]])
ax.plot(np.array(np.matrix(path)[:, 0]).flatten(),
        np.array(np.matrix(path)[:, 1]).flatten(),
        c='b',
        lw=2)

n = G.number_of_nodes()
if n < 500:
    plt.figure()
    pos = nx.spring_layout(G, iterations=1000, k=.1 / np.sqrt(n))