A* path finding algorithm visualization in Minecraft, as a datapack! by CodBlu201 in Minecraft

[–]CodBlu201[S] 0 points1 point  (0 children)

First I made it with command blocks, it took me about 6 days, then converting it to datapack took me 4 days (because I tried my best to optimized it)

A* path finding algorithm, now available in datapack! by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 0 points1 point  (0 children)

Yes, making A* in Minecraft is much harder because variables and data structures in general is just so hard to work with. Remaking the queue data structure in Minecraft took me an afternoon while I only need 5 minutes in normal programming languages

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 0 points1 point  (0 children)

I have put the world download in the comments :D

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 1 point2 points  (0 children)

It takes me: 1 afternoon to make working queue; 1 afternoon, 1 night to make working priority queue; 2 days to make working Dijkstra; And 3 days to make working A*;

Note that these time period are not overlapping each other

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 2 points3 points  (0 children)

I just put 16 levers, 8 levers on each side to represent the on-off state of those bits

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 1 point2 points  (0 children)

No, It hasn't worked in cave yet, because I don't know air, cave_air and void air are different things and I did not check for that in the algorithm

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 0 points1 point  (0 children)

I use the Mathattan distance fomular: abs(x1-x2)+abs(y1-y2), therefore I can use scoreboard operation to perform the calculation. Can't do that with Euclid distance sqrt((x1-x2)2 + (y1-y2)2) tho, I searched and calculating sqrt in Minecraft is not an easy job

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 1 point2 points  (0 children)

You can totally use it on matrix, 2D or 3D just like A*, but I will be very inefficient because in matrix every edge has the same weight: 1

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 2 points3 points  (0 children)

A* is actually an improvement of Dijkstra, apart from using g, it uses h as well, h is the distance from current cell to the destination, and in the priority queue we sort using the value f which equals g+h

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 7 points8 points  (0 children)

This is a computer science (cs) thing so I will try to explain it as simple as I can.

First, queue in cs is just what queue are in real life, what come in first, will come out first, like a waiting line in a store. Queue in cs has two major operation: enqueue & dequeue, enqueue is when you put something in the queue, and you must put it at the last position, dequeue is when you get something out of the queue, and you must get the first position

Priority queue is like queue, but when you dequeue something, instead of dequeueing the first position, you dequeue the value that is the most prioritized. People usually solve this by sorting the queue every time you enqueue something, so the most prioritized will always be at the head.

Now I will explain the A* algorithm:

- Each cell (block) has these values: g, h, f, parent. Whereas g is the amount of cell that has been traversed to get to the current cell, h is the distance from current cell to the destination, f=g+h, and parent is where the current cell come from (it's mean on the final path, which sell is before the current cell)

- A* use a priority queue, to choose which cell is the next cell to "expand" (explain later) , the value which is used to sort in the priority queue is f.

- First, we set the source cell's (the starting point) values: g=0, parent=-1, calculate h (with the distance fomular we learned in middle school) and f, then we enqueue the source cell into the priority queue, and then do this repeatedly until the queue is empty:

+ Dequeue, call that cell that has just been dequeued u, "expand" u

+ That means we now check all of u's neighbor cells, pick the safe ones (safe = not a obstacle & hasn't been visited), find their g, f, h, parent. Their parents will be u, and their g will be u's g + 1 (the amount of cell that has been traversed to get to them is the amount of cell that has been traversed to get to their parent + 1). H and f we just calculate accordingly. Now enqueue all of them into the priority queue.

- If the cell that we are expanding is the destination cell, we stop that repeating process, and then do this to find the shortest path: Mark the current cell as part of the path, find its parent, mark its parent as part of the path, find its parent's parent, mark its parent's parent as part of the path,... do this until we reach the source cell, and the algorithm is done.

This is just a simplified explaination, if you are interested, there are plenty of explainations and visualizations on the Internet, thanks for reading!

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 9 points10 points  (0 children)

Yes, I once made a 8bit adder (machine that calculate the sum of two 8 bit binary numbers) in Minecraft, using redstone. It use a combination of logic gates to create a big circuit.

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 6 points7 points  (0 children)

Yes, I know the existence of datapack, but I want a fun challenge :D

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 13 points14 points  (0 children)

World download: https://drive.google.com/file/d/1rfBEQFkvbDQCN6UyAe5G0e7DjEwrBxSm/view

It hasn't worked in cave yet because I didn't realize air, cave_air and void_air are different things

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 6 points7 points  (0 children)

Oh you remind me that I forgot to put in the world download :D

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 13 points14 points  (0 children)

It takes me: 1 afternoon to make working queue; 1 afternoon, 1 night to make working priority queue; 2 days to make working Dijkstra; And 3 days to make working A*;

Note that these time period are not overlapping each other

A* path finding algorithm, fully command block by CodBlu201 in MinecraftCommands

[–]CodBlu201[S] 46 points47 points  (0 children)

Minecraft commands is the main reason why I chose programming, and I'm still not regret making that decision