Git Product home page Git Product logo

Comments (4)

brean avatar brean commented on August 25, 2024

Very interesting!
I think this can also be solved using a Grid instead of a Graph, the feature of a weighted graphs is already implemented. However we have just one very basic test for it.

Testing this with the advent of code example also does not work:

from pathfinding.core.grid import Grid, build_nodes
from pathfinding.finder.dijkstra import DijkstraFinder

B = '''2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533'''

g = Grid(matrix=B.split('\n'))


finder = DijkstraFinder()
path, _ = finder.find_path(g.node(0, 0), g.node(12, 12), g)
print(g.grid_str(path=path, show_weight=True))

Sadly I don't have time now. I will have a deeper look at it in the new year

from python-pathfinding.

Markopolo141 avatar Markopolo141 commented on August 25, 2024

No worries.

I did a bit of looking, and my impression is that there is something wrong with the way that node.f and node.g are calculated. per finder.py lines 103-112 - but I am not so sure (path-finding isn't my expertise)

        # calculate cost from current node (parent) to the next node (neighbor)
        ng = graph.calc_cost(parent, node, self.weighted)

        if not node.opened or ng < node.g:
            node.g = ng
            node.h = node.h or \
                self.apply_heuristic(node, end) * self.weight
            # f is the estimated total cost from start to goal
            node.f = node.g + node.h
            node.parent = parent

so for each node its 'g' value is the least distance to its parent/s plus one. (I wonder if this is effectively a 'stepest' decent approach) whereas I would normally have thought that 'node.g = parent.g + ng' so that the 'g' value is the smallest sum along the path back to the start node?

from python-pathfinding.

brean avatar brean commented on August 25, 2024

I had a quick moment to look at this. Still needs more thinking but I like to share some first finds:
I wonder why we add the weight that is a fixed value (or 1) in line 109, we also multiply the weight that it costs to move to the next node in the grid.calc_cost function (looks like I forgot to implement this for the graph), so it should be the value from the node we currently look at, I am not sure why we have weight as variable in the finder tbh...
For the f = g + h that's actually how it should be for A*, see for example the pseudo-code at wikipedia or the bit more detailed description here:
https://medium.com/@nicholas.w.swift/easy-a-star-pathfinding-7e6689c7f7b2
For a pure Dijkstra implementation the heuristic would always be 1, thus node.h would also always be 1...
In the calc_cost function of the grid we actually return node_a.g + ng but we don't do that for the graph!
Still this does not explain why it doesn't work for my grid-based example.

Next step for me will be to create more unit-tests for the graph-implementation and taking a deeper look at the calacultion of node.g.

from python-pathfinding.

brean avatar brean commented on August 25, 2024

fixed in 1.0.7, see 2990b64
also take a look at the new unit test for weighted graphs: https://github.com/brean/python-pathfinding/blob/main/test/test_weighted_graph.py

from python-pathfinding.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.