A* Search Testbed

-- Peter Brooks, Stuyvesant High School, Aug. 2018

-- This testbed is designed to experiment with the A* Search technique.  We'll be using the technique to find the shortest path between two points in the Netlogo visual area.  In the image of the Netlogo screen below, the two orange/brown spots are the beginning/end points, the dark blue walls are obstacles, and the shortest path in green was calculated by an external Python program. 

-- In this particular case, the user interactively places the start/end points, draws the walls, and  presses the "Run Program" button.  Netlogo writes the begin/end point and wall information into the file Net2Py.txt, then launches the Python program PathFinderAStar.py.  That program reads the Net2Py.txt file, and uses the A* search algorithm to find the shortest path (if a path actually exists), and encodes that result into the file Py2Net.txt and exits.  Netlogo wakes up, reads the Py2Net.txt file and draws the path.

-- You can download the Netlogo program, PathFinderVisual.nlogo shown below.

-- You can also download a simple sample Python program: SimplePathFinder.py that will demonstrate the plumbing necessary to communicate with the Netlogo.  The SimplePathFinder.py program ignores all walls, and simply finds a shortest path between start and end points as if walls were not there.  It does not use the A* search technique.  Download SimplePathFinder.txt (right-click, "Save as...") and then change its suffix to ".py".

-- To set up the system:
----- Create a working directory into which you'll put PathFinderVisual.nlogo, the test program SimplePathFinder.py, and your own Python solution
----- Launch the NetLogo program
----- Put the full path of the working directory into the "WorkingDir" input both
----- Write the full command-line into the "Python-Command-Line" box with either SimplePathFinder.py or your own program
----- Create the start and end points and walls, and finally press the "Run Program" button.
----- You can also save the NetLogo program (Ctrl-S) once you've filled in the "WorkingDir" and "Python-Command-Line" input boxes so that when you reload the NetLogo program, they will be automatically filled in.

-- The two words after the program name are arbitrary filenames that will carry information from Netlogo to your program (Net2Py.txt) and from your program back to NetLogo (Py2Net.txt).

-- Let's talk about the communications between NetLogo and your Python program...

Sample start and end points and wall
-- Suppose we have the simple setup above, with start and end points 4 patches apart and a 3-patch wall between them.
-- The file that transfers information from Netlogo to Python has the starting point's pxcor and pycor coordinates on the first line, followed by the end point's coordinates, and then the sequence of the wall's coordinates:
[-5 1]
[-1 1]
[[-3 1] [-3 0] [-3 2]]
-- All wall patches will be in a single list on the third line, whether the walls are connected to each other or not.
-- Notice that the lists' elements are separated by spaces and not by commas.  This is necessary for NetLogo.
-- Here's some code for your program to read the incoming information for the filename in the third word on the command-line:

import json, sys
filename = sys.argv[1]
fin = open(filename,'r')
slines = fin.read().split('\n')
startpoint = json.loads(slines[0].replace(' ', ', '))
endpoint = json.loads(slines[1].replace(' ',', '))
wallpoints = json.loads(slines[2].replace(' ',', '))
Shortest path
-- Your program will find the shortest path, and then create a list of directions on how to go along the path from the start point to the end point.  The direction commands are "r", "d", "u", "l", for right, down, up and left.
-- The answer generated for the green path above is:
["r" "d" "d" "r" "r" "u" "r" "u"]
-- Note again the list format with spaces instead of commas.
-- This answer can be written using:

filename = sys.argv[2]
fout = open(filename,'w')
outstring = json.dumps(answer).replace(', ',' ')
fout.write(outstring + '\n')
fout.close()

Finally, anything your program prints will be displayed by NetLogo in a user-emssage after you press "Run Program".
PathFinder in Netlogo