anyone has a good C example of simple A* search - Page 1
Forum section: electronics / general

author: legacy
author_rank: Super Contributor
timestamp: September 28, 2013, 04:23:44 pm
content: hi guystoday i am toying with robotics and the dish of day is "path planning". As far as i have understood, in order to perform path planning you'd better go for so called "A star search", which was a common LISP research task in '80.hereit is a python example of "ecursivity-and-a-search" in where theA* algorithmhas been implemented to solve a(n amazing) maze with the specific extra goal of choosing the less expensive path (yeah you can also have learn you have to define the "cost", what is cost ? what evil are you talking about ? guessing about that, i have been thinking that robot use chemical energy to move, so from the point of view of the battery it may be that the less the robot takes moves the better and the the most far it goes).Anyway, i hope you enjoy this article, and .. personally i am looking for something similar written in C.edit:thread title mixup: "anyone has a good C example of simple A* search(recursivity approach)"

author: legacy
author_rank: Super Contributor
timestamp: September 28, 2013, 04:28:14 pm
content: Wiki, aboutA*_search_algorithm

author: senso
author_rank: Frequent Contributor
timestamp: September 28, 2013, 04:33:22 pm
content: If there is also mapping involved, also consider SLAM.

author: iceisfun
author_rank: Regular Contributor
timestamp: September 30, 2013, 12:03:23 am
content: I have some C implementations of this but I would have to dig around in old archives, look in game engines that have been open sourced and you will find them.However I recommend implementing it yourself to help with understanding it, there are a few key building blocks you will needNode list with connected neighbours and possibly a traversal costMethod of moving nodes between open and closed listMethod of sorting the open list based on distance from goalOnce you have those building blocks this task is pretty easy to get going, I suggest doing it in C# first while you understand and debug it instead of fighting against C issues unless your good with C.If you need specific help with part of the A* search ask and I can help.BTW : Recursion is usually the wrong approach, more often something like while(Node != Dest) { ... } then Walk back up list and assemble a path but within one or a few functions but not recursive, the depth of a search can grow quite large depending on the graph.

author: legacy
author_rank: Super Contributor
timestamp: September 30, 2013, 12:33:35 am
content: thank you for your help =)what game's sources should i look at ? any example ?C is not a problem for me.i have implemented a sub optimal search, recursive approach conditioned by an heuristic function which tries to estimate the distance to the goal-exit, it suggests choices -what to do next-'I' entrance'O" exit'.' visited cell'@' dead lock reached (it should not happen with the new approach i am using)' ' free space not visitedCode:|01234567890123456780:|######### #########|1:|# #I# #|2:|# #|3:|# ####### #|4:|# #|5:|# # # #|6:|# #O# #|7:|###################||01234567890123456780:|######### #########|1:|#.......#O#OO.....#|2:|#........OOOOO....#|3:|#.....#######O....#|4:|#........OOOOO....#|5:|#.......#O#.OO....#|6:|#.......#O#.OO....#|7:|###################|Code:|01234567890123456780:|######### #########|1:|# #I# #|2:|# #|3:|# #|4:|# # # #|5:|# # O#|6:|###################||01234567890123456780:|######### #########|1:|# #O# #|2:|# O #|3:|# O #|4:|# #O# #|5:|# #OOOOOOOOO#|6:|###################|Code:|01234567890123456780:|######### #########|1:|# #I# #|2:|# # # # #|3:|# ######### #### #|4:|# # # #|5:|# # # # # O#|6:|###################||01234567890123456780:|######### #########|1:|# #O# OOOOOO#|2:|#OOOOOOOOO#OO# #O#|3:|#O#########OO####O#|4:|#O#....OOOOOO# OO#|5:|#OOOOOOO#.#OO# #OO#|6:|###################|Code:|01234567890123456780:|######### #########|1:|# # ## I ## # #|2:|# # # # # #|3:|# ##### # # ##### #|4:|# O #|5:|# ##### # # ##### #|6:|# # # # # #|7:|# # ## ## # #|8:|###################||01234567890123456780:|######### #########|1:|# # ##.O.##.#...#|2:|# # #OOOOO#...#.#|3:|# #####O#O#O#####.#|4:|# OOOOO......#|5:|# ##### #O#O#####.#|6:|# # # OOO#...#.#|7:|# # ## ##.#...#|8:|###################|Code:/** circular issue, wrong method** XXXXXXXXXIXXXXXXXXX* X...X.XX@@@XX.X...X* X.X...X@@ @@X...X.X* X.XXXXX@X X@XXXXX.X* X......@ O @......X* X.XXXXX@X X@XXXXX.X* X.X...X@@ @@X...X.X* X...X.XX@@@XX.X...X* XXXXXXXXXXXXXXXXXXX*/New heuristic methodCode:|01234567890123456780:|###################|1:|# # ## I ## # #|2:|# # # # # #|3:|# ##### # # ##### #|4:|# O #|5:|# ##### # # ##### #|6:|# # # # # #|7:|# # ## ## # #|8:|###################||01234567890123456780:|###################|1:|# # ## O ## # #|2:|# # # O # # #|3:|# ##### #O# ##### #|4:|# O #|5:|# ##### # # ##### #|6:|# # # # # #|7:|# # ## ## # #|8:|###################|

author: legacy
author_rank: Super Contributor
timestamp: September 30, 2013, 12:48:49 am
content: Quote from: iceisfun on September 30, 2013, 12:03:23 amthis part is not well understood for me, how to create the tree ?

author: legacy
author_rank: Super Contributor
timestamp: September 30, 2013, 01:06:30 am
content: To handle the connection between nodes i have thought about using a matrixso a an extreme easy maze like thisCode:|012340:|#####|1:|## ##|2:|# #|3:|## ##|4:|#####|looks this waynodes, and their relations, considering 1 step of movement (up, down, left, right)Code:id0, (2, 1)id1, (1, 2)id2, (2, 2)id3, (3, 2)id4, (2, 3)id0->id2 ( 2, 1)->( 2, 2) downid1->id2 ( 1, 2)->( 2, 2) rightid2->id0 ( 2, 2)->( 2, 1) upid2->id1 ( 2, 2)->( 1, 2) leftid2->id3 ( 2, 2)->( 3, 2) rightid2->id4 ( 2, 2)->( 2, 3) downid3->id2 ( 3, 2)->( 2, 2) leftid4->id2 ( 2, 3)->( 2, 2) upmovement matrixCode:|012340|..D..1|..R..2|UL.RD3|..L..4|..U..It appears to me that this information grows up too much

author: IanB
author_rank: Super Contributor
timestamp: September 30, 2013, 01:37:20 am
content: To help code your solution, it may help to write down your maze solving algorithm in English first.For example:A maze solution is any sequence of paths that starts at an entrance and finishes at an exit.A path begins at any point (cell) where there are two or more directions to follow (a fork in the road).A path finishes at any point meeting one of the following conditions:1. It reaches a dead end2. It reaches a point where new paths diverge3. It reaches a point (cell) already visited4. It reaches an exitTo solve a maze:Follow all paths not already followed, backtracking as necessary.Eliminate any paths that reach a point where there are no new paths to follow.Continue until a path is found that reaches an exit.

author: iceisfun
author_rank: Regular Contributor
timestamp: September 30, 2013, 01:38:31 am
content: In the case of a flat X,Y grid the adjacent cells are easy to find, many games device a non-uniform node list that requires each node to maintain a list of connected nodes such as a map in a 3d game might go up and down or in a long straight hall way.On your flat X,Y grid you can assume that each adjacent cell that is not blocked is connected and has a weight of either 1 or sqrt(2) for corners, if you want your path to prefere to go straight instead of taking angles just tweak these weights so going at a angle is more expensive (a longer path) and the algorithm will tend to go to the left, right up and down as these paths will appear "shorter"Most of the game samples I have are in C++ or C# and now I've scrapped most of my stuff in favor of Recast which is a full featured nav mesh system written in C++ (https://github.com/memononen/recastnavigation) for pathing in 3D environments.Can you post your code?

author: iceisfun
author_rank: Regular Contributor
timestamp: September 30, 2013, 02:37:35 am
content: I've been digging for a while now and the only c like implementation I have has a bug, I can't find the cleaned up version.

author: legacy
author_rank: Super Contributor
timestamp: September 30, 2013, 08:24:45 am
content: which bug is it ?and what do you think about combining the "BelmanFord minimal path finder", a typical algorithm used at the mac level of internet working devices (e.g. routers), with the cross matric which describes the maze ?here it is an example of a BelmanFord implementation i did in C at my university (as homework), his cross matrix is described as "cost" with the meaning of "how does it cost to reach node A from node Athought path"Code:[!] showA B C D E F GA * 9 - - 3 - -B 9 * 2 - 1 9 -C - 2 * 1 1 - -D - - 1 * - 3 2E 3 1 1 - * 9 -F - 9 - 3 9 * 4G - - - 2 - 4 *[!] cover_tab showsource = Anode: from cost pathA: A -B: E 4 AEBC: E 4 AECD: C 5 AECDE: A 3 AEF: D 8 AECDFG: D 7 AECDG[!] cover_tab showsource = Bnode: from cost pathA: E 4 BEAB: B -C: E 2 BECD: C 3 BECDE: B 1 BEF: D 6 BECDFG: D 5 BECDG[!] cover_tab showsource = Cnode: from cost pathA: E 4 CEAB: E 2 CEBC: C -D: C 1 CDE: C 1 CEF: D 4 CDFG: D 3 CDG[!] cover_tab showsource = Dnode: from cost pathA: E 5 DCEAB: E 3 DCEBC: D 1 DCD: D -E: C 2 DCEF: D 3 DFG: D 2 DG[!] cover_tab showsource = Enode: from cost pathA: E 3 EAB: E 1 EBC: E 1 ECD: C 2 ECDE: E -F: D 5 ECDFG: D 4 ECDG[!] cover_tab showsource = Fnode: from cost pathA: E 8 FDCEAB: E 6 FDCEBC: D 4 FDCD: F 3 FDE: C 5 FDCEF: F -G: F 4 FG[!] cover_tab showsource = Gnode: from cost pathA: E 7 GDCEAB: E 5 GDCEBC: D 3 GDCD: G 2 GDE: C 4 GDCEF: G 4 GFG: G -

author: legacy
author_rank: Super Contributor
timestamp: September 30, 2013, 09:44:47 am
content: A goodpathfinderexample.

author: legacy
author_rank: Super Contributor
timestamp: October 02, 2013, 04:46:24 pm
content: UP

author: Bored@Work
author_rank: Super Contributor
timestamp: October 02, 2013, 05:25:45 pm
content: Quote from: legacy on October 02, 2013, 04:46:24 pmWhat did you, yes YOU do the last few days, besides posting "UP"?

author: legacy
author_rank: Super Contributor
timestamp: October 02, 2013, 06:34:50 pm
content: last few days ? i have been implementing algorithm (1) in Cbut my previous questions are still without answers.(1)

author: legacy
author_rank: Super Contributor
timestamp: October 02, 2013, 08:45:55 pm
content: Also foundthis(aStarTutorial)

author: legacy
author_rank: Super Contributor
timestamp: October 02, 2013, 08:59:39 pm
content: interesting python code, designed following the previous (1) flowchartCode:def process(self):# add starting cell to open heap queueheapq.heappush(self.op, (self.start.f, self.start))while len(self.op):# pop cell from heap queuef, cell = heapq.heappop(self.op)# add cell to closed list so we don't process it twiceself.cl.add(cell)# if ending cell, display found pathif cell is self.end:self.display_path()break# get adjacent cells for celladj_cells = self.get_adjacent_cells(cell)for c in adj_cells:if c.reachable and c not in self.cl:if (c.f, c) in self.op:# if adj cell in open list, check if current path is# better than the one previously found for this adj# cell.if c.g > cell.g + 10:self.update_cell(c, cell)else:self.update_cell(c, cell)# add adj cell to open listheapq.heappush(self.op, (c.f, c))

author: legacy
author_rank: Super Contributor
timestamp: October 02, 2013, 09:38:06 pm
content: From the above python code it appears to me one condition is missing: what happens if It reaches a dead end ?More specifically when the adjacent list is empty: you have to roll back your solution to the first one cell in the open list, than start a new search.Code:######### ########## #I# ## | ## |/---->-\## #|# |## #x# O####################x is dead end

author: legacy
author_rank: Super Contributor
timestamp: October 02, 2013, 11:38:07 pm
content: More reading, may be interesting:hereit is an article about "toward more realistic pathfinding"

author: legacy
author_rank: Super Contributor
timestamp: October 02, 2013, 11:45:03 pm
content: Quote from: iceisfun on September 30, 2013, 01:38:31 amtoo complicatedQuote from: iceisfun on September 30, 2013, 01:38:31 amstill proof version, very very proof. In the previous examples i used a recursivity approach, which is working but it is not the best approach at all.

author: legacy
author_rank: Super Contributor
timestamp: October 02, 2013, 11:46:54 pm
content: Quote from: senso on September 28, 2013, 04:33:22 pmany good teaching stuff around ?

author: legacy
author_rank: Super Contributor
timestamp: October 04, 2013, 10:22:55 am
content: what do you think about "wave-front" ?

author: legacy
author_rank: Super Contributor
timestamp: October 19, 2013, 01:07:30 am
content: Code:|01234567890123456780:|###################|1:|# # ## I.## # #|2:|# # # .# # #|3:|# # # .# # #|4:|# # # ### .# # #|5:|# # # .# # #|6:|# # # .# # #|7:|# ##### ###.##### #|8:|# . #|9:|# . #|10:|# . #|11:|# . #|12:|# . #|13:|# . #|14:|# . #|15:|# . #|16:|# . #|17:|# . #|18:|# ###### . #######|19:|# # # . # # #|20:|# # # . # # #|21:|# # # . # # #|22:|# # # O # # #|23:|# # # # # #|24:|# # ### ### # #|25:|###################|Improvement

author: legacy
author_rank: Super Contributor
timestamp: October 19, 2013, 01:28:24 am
content: Also Vector Field Histogram (VFH) Method is using A*!Nice to know!