Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6800453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:58:16+00:00 2026-05-26T18:58:16+00:00

This is my implementation of A* in Python class Node: def __init__(self, (x, y),

  • 0

This is my implementation of A* in Python

class Node:
    def __init__(self, (x, y), g, h, parent):
        self.x = x
        self.y = y
        self.g = g
        self.h = h
        self.f = g+h
        self.parent = parent

    def __eq__(self, other):
        if other != None:
            return self.x == other.x and self.y == other.y
        return False

    def __lt__(self, other):
        if other != None:
            return self.f < other.f
        return False

    def __str__(self):
        return "(" + str(self.x) + "," + str(self.y) + ") " + str(self.f)

def mean(lst):
    print sum(lst)/len(lst)

def find_path(start_node, end_node, map, no_diag=True):
    open_list = [start_node]
    closed_list = []
    solution = []
    while len(open_list) > 0:
        open_list.sort()
        current_node = open_list.pop(0)
        if current_node == end_node:
            while current_node != None:
                solution.append(current_node)
                current_node = current_node.parent
            break
        for x,y in [(0,-1), (1,0), (0,1), (-1,0)]:
            touching_node = Node((current_node.x+x, current_node.y+y), 10, (abs(end_node.x-current_node.x) + abs(end_node.y-current_node.y))*10, current_node)
            tile_in_range = touching_node.x >= 0 and touching_node.y >= 0 and touching_node.x < len(map[0]) and touching_node.y < len(map)
            if not tile_in_range or touching_node == current_node.parent or map[touching_node.y][touching_node.x] == 1:
                continue
            if touching_node in open_list:
                n = open_list[open_list.index(touching_node)]
                if n > touching_node:
                    open_list.remove(n)
                else:
                    continue
            if touching_node in closed_list:
                n = closed_list[closed_list.index(touching_node)]
                if n > touching_node:
                    closed_list.remove(n)
            open_list.add(touching_node)
        closed_list.append(current_node)
    return solution


map = [[1,1,0,1,0,0],
       [0,0,0,0,1,0],
       [0,0,1,0,0,0],
       [0,0,0,1,0,0],
       [0,1,0,1,1,1],
       [0,1,0,0,0,0]]

map2 = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
        [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
        [1,0,0,2,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
        [1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1],
        [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
        [1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1],
        [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]     

def test(map, start, end):
    for r in map:
        print r
    res =  find_path(Node(start,1000,1000,None), Node(end,1000,1000,None), map)       
    for n in res:
        print n

test(map, (0,5), (5,0))
test(map2, (3,8), (2,11))

With the first map the path is found straight away. On the second one however even after half an hour the path isn’t found. I tried using a faster list but it made no difference. Can someone please point out where the problem is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:58:16+00:00Added an answer on May 26, 2026 at 6:58 pm

    The problem with your implementation is not performance, but rather that it is actually incorrect – it never finishes for the second map!

    There is an else: continue missing in the part where the node is removed from the closed set – if the node is already part of either set – it must not be added to the open set!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First the code: class myClass(object): def __cmp__(self, other): return cmp(type(self), type(other)) or cmp(self.__something, other.__something)
In other words, is this Singleton implementation thread safe: public class Singleton { private
Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance
# file1.py class _Producer(self): def __init__(self): self.chunksize = 6220800 with open('/dev/zero') as f: self.thing
I'm trying to use an Objective-C class made in Python to do this but
When defining a method on a class in Python, it looks something like this:
Class member functions in Python have to explicitly declare a self parameter which represents
How does len work on Python? Look at this example: class INT(int): pass class
I've already looked at this question: Python iterators – how to dynamically assign self.next
I'm working through Google's Python class exercises . One of the exercises is this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.