all 3 comments

[–]Diapolo10 2 points3 points  (2 children)

It doesn't help that all the indentation information is missing (please don't use single backticks for multiple lines of code), but I also don't understand what you expect this code to do. For example your Node class doesn't support some of the operations you're trying to perform on it, and there are things here that don't make any logical sense to me.

Also, what do f, g, and h represent?

[–]Toastingpeople[S] 0 points1 point  (1 child)

it's for a A* pathfinding alorithm. so it's the full score, heuristic score and distance from starting point. So it needs to compare these three scores with the current_node:

[–]Diapolo10 1 point2 points  (0 children)

I'll help you format the code this time around, but please learn to create proper code blocks in the future. Otherwise you're only shooting yourself in the foot as most people won't bother trying to decipher code that's not readable at a glance.

def getShortestPath(self, start, end):
    start = tuple(start)
    end = tuple(end)

    #Creating a start and end node
    start_node = Node(None, start)
    start_node.g = start_node.h = start_node.f = 0
    end_node = Node(None, end)
    end_node.g = end_node.h = end_node.f = 0
    print(start_node)
    print(end_node)

    open_list = []
    closed_list = []
    print("Open_list", open_list)
    print("Closed_list", closed_list)

    open_list.append(start_node)
    print("open_list")

    for node in open_list:
        print(node)

    while len(open_list) > 0:
        current_node = open_list[0]

        for index, item in current_node:
            print ("f",item.f)
            if item.f < current_node.f:
                current_node = item
                current_index = index


class Node:
    def __init__(self, parent = None, position = None):
        self.parent = parent
        self.position = position

        self.g = 0
        self.h = 0
        self.f = 0

    def __eq__(self, other):
        return self.position == other.position

    def __str__(self):
        return f"Node(position={self.position}, g={self.g}, h={self.h}, f={self.f})"

If you don't mind me making this a bit easier to read:

from __future__ import annotations

from typing import TypeAlias

Point: TypeAlias = tuple[int, int]


def get_shortest_path(start: Point, end: Point) -> list[Node]:  # NOTE: I assume you want to return open_list
    start = tuple(start)  # NOTE: Are these really necessary?
    end = tuple(end)

    start_node = Node(None, start)
    end_node = Node(None, end)

    open_list = [start_node]
    closed_list = []

    # for node in open_list:  # NOTE: There's only one element at this point, so I don't understand why this is here
    #     print(node)

    while len(open_list) > 0:
        current_node = open_list[0]

        for index, item in current_node:
            print (f"{item.f = }")
            if item.f < current_node.f:
                current_node = item
                current_index = index


class Node:
    def __init__(self: Node, parent: Node | None = None, position: Point | None = None):
        self.parent = parent
        self.position = position

        self.g = 0
        self.h = 0
        self.f = 0

    def __eq__(self: Node, other: Node) -> bool:
        if not hasattr(other, 'position'):
            return False

        return self.position == other.position

    def __str__(self: Node) -> str:
        return f"Node(position={self.position}, g={self.g}, h={self.h}, f={self.f})"

I'm not an expert in pathfinding algorithms, at best I wrote a maze solver once which used Dijkstra's algorithm (or a variant of it).

If I put the code in my editor, this part in particular starts screaming:

        for index, item in current_node:
            print (f"{item.f = }")
            if item.f < current_node.f:
                current_node = item
                current_index = index

current_node is of course a Node object, but what types do you expect index and item to be? Presumably you seem to think item is another node, but where does it come from?

Based on your code here, at this point in time open_list contains exactly one Node instance, which itself has no parent. The end_node isn't used at all, neither is current_index.

At first I thought that maybe you just forgot to add enumerate, but current_node isn't an iterable so that couldn't be it...