Comments (4)
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.
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.
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.
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)
- Show only adjacent path, not diagonal HOT 5
- Implement Tunnel Discovery for Puzzle Path Planning
- Switch to another CI
- Measure time and show stats
- Add elevators/ladders, steps and portals HOT 2
- switch from tox/travis/coveralls to github actions HOT 1
- Performance and benchmark discussion
- Path smoothening and optimization tools and documentation
- 3D-pathfinding
- Slow Pathfinding for Larger Matrices HOT 1
- New Type Hint Breaking previous Python3 versions HOT 2
- Is it possible to avoid getting too close to a non walkable cell ? HOT 2
- Typo in CONTRIBUTING.md?
- On names and parameter orders of coodinates HOT 1
- Bug when edges are defined from the end HOT 1
- Performance issue with node.py HOT 6
- find_path() giving me complex output rather than list of tuples HOT 5
- How to favor path with less turns? (A*) HOT 4
- Grid does not allow an algorithm to make a diagonal crossing over a border HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-pathfinding.