Jump to content

Dijkstra Pathfinding with Python


Recommended Posts

hey guys,

I thought it's time to test my python skills and decided to give the Dijkstra Pathfinding Algorythm a shot.

Some credits actually go to Sam Hancock who inspired me to do this project, I gladly took a peek at his approach before I tackled the problem,

also I have to thank my friend Michael Davis who always throws in some kickass workflow tips.

After a couple hours of work I was able to solve the (too me a big) mystery - as I never dealt with such stuff before it got quite interesting.

The biggest problem I ran into was calculating the distances between connected points (the "neighbours") in Python,

it gets really messy as there is no such thing as an edge in Houdini compared to other packages (I'm not complaining, only a little) -

so I decided to go back to VOPs and use the neighbourCount and neighbour nodes which provided a really fast and flexible workaround.

During my little ride I actually solved the travelling salesman problem (TSP) accidentally, see the attached image :lol:

Anyway, I'm attaching the scenefile for everyone who's interested,

cheers,

Manu

post-5147-13228103881_thumb.jpg

post-5147-13228109971_thumb.jpg

Dijkstra_Pathfinding_Manu_v3.hip

Edited by asnowcappedromance
  • Like 1
Link to comment
Share on other sites

hey guys,

I thought it's time to test my python skills and decided to give the Dijkstra Pathfinding Algorythm a shot.

Some credits actually go to Sam Hancock who inspired me to do this project, I gladly took a peek at his approach before I tackled the problem,

also I have to thank my friend Michael Davis who always throws in some kickass workflow tips.

After a couple hours of work I was able to solve the (too me a big) mystery - as I never dealt with such stuff before it got quite interesting.

The biggest problem I ran into was calculating the distances between connected points (the "neighbours") in Python,

it gets really messy as there is no such thing as an edge in Houdini compared to other packages (I'm not complaining, only a little) -

so I decided to go back to VOPs and use the neighbourCount and neighbour nodes which provided a really fast and flexible workaround.

During my little ride I actually solved the travelling salesman problem (TSP) accidentally, see the attached image :lol:

Anyway, I'm attaching the scenefile for everyone who's interested,

cheers,

Manu

Thank you for sharing!

Link to comment
Share on other sites

terribly sorry, forgot to attach the otl file, without it the scene is useless. My bad!

Here you go!

One thing that I'd like to add is the following link:

http://eoinbailey.com/blog/dijkstras-algorithm-illustrated-explanation

It's a detailed explanation of the algorithm, it helped me a lot to get what's going on under the hood.

SOP_Dijkstra_Pathfinder_PyOp.otl

Link to comment
Share on other sites

terribly sorry, forgot to attach the otl file, without it the scene is useless. My bad!

Here you go!

One thing that I'd like to add is the following link:

http://eoinbailey.co...ted-explanation

It's a detailed explanation of the algorithm, it helped me a lot to get what's going on under the hood.

Hey thanks for the file. I was actually working on a similar problem. So this is great.

As a side note, the group SOP provides a way to select the connected points. It might provide a much more concise way to get a decent list of connected edges. I have been looking into ways to build the list of connected edges and this was one of the ways I came up with, although it is not as sexy as using vop and python that is for sure :)

Link to comment
Share on other sites

I'm glad you like the setup!

It's far from perfect though, haven't had any time to polish it up, going on vacation tomorrow :)

Yeah, I know about the group SOP but it's slow as hell and using VOPs is 10x faster that's for sure and if you're worried about this approach

being concise, believe me it is. Also, VOPs would allow you to use point clouds which is even faster, so if you would limit the search to a certain

radius you could speed it up even more. The python operator kills the speed though, the Dijkstra method is far too slow as is computes every single shortest

path for the whole geometry.

cheers,

Manu

Link to comment
Share on other sites

I'm glad you like the setup!

It's far from perfect though, haven't had any time to polish it up, going on vacation tomorrow :)

Yeah, I know about the group SOP but it's slow as hell and using VOPs is 10x faster that's for sure and if you're worried about this approach

being concise, believe me it is. Also, VOPs would allow you to use point clouds which is even faster, so if you would limit the search to a certain

radius you could speed it up even more. The python operator kills the speed though, the Dijkstra method is far too slow as is computes every single shortest

path for the whole geometry.

cheers,

Manu

Manu, thanks for the heads up. I have not tried the group sop but I just knew that it could create the connection groups.

I have not used the neighbors nodes before and your example explains it very well. Thanks again

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

Hi Asnowcappedromance,

I have to say great work on implementing Dijkstra's algorithm.

I have a question.

I'm currently working on my specialization/graduation (graduation year) college program at IGAD-NHTV.

My project is to find a way to create a generator that can build functional and playable multiplayer levels that can be exported to a game engine.

I've been looking at different methods to make the generator construct logical corridors throughout my levels and a

path-finding solution seems to be the most promising.

I am however no programmer and know nothing when it comes to python script.

Would I be allowed to use your solution in my research and potentially my graduation project?

You will of course be referenced and credited for the method and OTL if I succeed in implementing it into a working solution.

Edited by hyperforce
Link to comment
Share on other sites

Manuel, I'm trying to implement your solution into my level generator and its working fairly well. (I've added in an image)

The 3D grid is invisible in the image, but I've photoshoped in some dark red markers to give you a sense of depth in the image.

cooking times get rather long very fast however and the more complex it gets the higher the chances are one or more lines aren't generated correctly. I'm not sure if this is related to the algorithm or due to the foreach nodes.

I am getting a lot of "breaking while loop" warnings however.

The 3D grid i'm using has roughly 10.000 points with roughly 8-12 connections each.

vi_rus, would it be ok if I try to implement your HDK version?

And if it is, do you have an example file that I can try to implement?

Thank you in advance. Though I'd understand if you refuse.

post-6166-133285329008_thumb.jpg

Edited by hyperforce
Link to comment
Share on other sites

if you are going the hdk route anyway, wouldn´t it be the easiest to use one of the many existing graph-libraries like bgl (boost graph library), igraph, ...?

most libraries provide numerous shortest path algorithms that should be pretty fast for around 10000 points.

petz

Edited by petz
  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...