Pages

Sunday, December 2, 2012

Visualization of A* Algorithm - A Java Project

As I mentioned in the last post this semester I'm doing a Java project which is Visualization of A* Algorithm. Because of the exams, I couldn't spare time to improve it. I did some in this weekend. I redesigned the class structure because a class was doing things that it shouldn't do. I added one more class and it is more object-oriented right now.

When I did the first version - before the improvements above - I showed it to my professor. He wanted me to add the spreading of the algorithm in every step. In this way, we are able to see how the algorithm works. To do this, I had to sleep the algorithm for awhile in every step. And I added sleep into the function which finds the path. But it didn't work as I expected. Because GUI slept, too. After a little search, I found that I had to make thread programming to fix that problem. My program was executing in a single thread, when I sleep a function, GUI was sleeping, too. The thing that I have to do was dividing the program into two pieces, first is the piece that shows the GUI elements and the second is the piece that does calculations for algorithm.

I know a little bit of thread programming from my Operating Systems course. We also did a lab to learn better. We were using C, you can find the code for that here. In Java, thread programming was easier than I thought. After doing that, program was working correctly.

Now, I have to study to understand algorithm better. I am little bit confused because of heuristic part of algorithm. The heuristic is directly affecting the algorithm, choosing the right heuristic is the main issue of A*.

Here is some images from the project;


Initial position of starting and destination nodes. Later, users will be able to change their positions.



Spreading of algorithm. Light gray nodes are in closedList. Dark blue nodes are in openList. Calculating g value of neighbors is like this:
currentNode.g + 10 for horizontal and vertical neighbors
currentNode.g + 14 for diagonal neighbors
And Manhattan heuristic is used.

When I finish this project, I'm planning to open the source code. I will probably share the codes on Github.