diff --git a/RouteFinding/05_tests.py b/RouteFinding/05_tests.py index dd669d8..d3febba 100644 --- a/RouteFinding/05_tests.py +++ b/RouteFinding/05_tests.py @@ -2,9 +2,19 @@ import DRDViz class SearchNode: - def __init__(self, label, value): + def __init__(self, label, pathcost): self.label = label - self.value = value + self.pathcost = pathcost + self.node = tuple((label, pathcost)) + def showBasic(self): + # print(self.node) # debugging purposes + return self.node + def __eq__(self, object): + return ((self.label) == (object.label)) + def __lt__(self, object): + return ((self.label) < (object.label)) + def __repr__(self): + return repr(self.node) class Searcher: def __init__(self, mapfile): @@ -60,9 +70,59 @@ class Searcher: # need to get the myViz attribute by importing the DRDViz stuff. self.myViz = DRDViz.DRDViz() # now, start using the myViz thingy to 'display' a map. + self.myViz.loadGraphFromFile(mapfile) + self.myViz.plot() + # create an 'open' list + self.open = list() def setStartGoal(self, startNode, endNode): self.startNode = startNode self.endNode = endNode + self.myViz.markStart(self.startNode) + self.myViz.markGoal(self.endNode) + # add the starting node to the 'open' list. + starting = SearchNode(startNode, 0) + self.open.append(starting) + def successors(self, parentNode): + childList = list() + # grab the nodes from the nested dictionary given that the parentNode is a tuple. + nodeChildren = self.nodePaths.get(parentNode.label) + # grab each value from the dictionary, turn it into a SearchNode. + for node in nodeChildren.keys(): + childList.append(SearchNode(node, int(nodeChildren[node]))) + # sort the child node list + childList.sort() + return (childList) + def reset(self): + # clear all the items from the open list + self.open.clear() + # add the start node back into the list. + self.open.append(SearchNode(self.startNode, 0)) + # do I need to reset the visualization too? + def insert_front(self, nodeList): + # backwards "sort" + # loop through each element, and apply it to the beginning of the open list. + for item in nodeList: + if item not in self.open: + self.open.insert(0, item) + return None + def insert_end(self, nodeList): + # forwards "sort" + # loop through each element, and apply it to the end of the open list. + for item in nodeList: + if item not in self.open: + self.open.append(item) + return None + def insert_ordered(self, nodeList): + # sort by the lowest cost after adding all the elements to the open list. + # add the nodes to the list + self.open.extend(nodeList) + # sort via a lambda function key, using the sorted function + sortedList = sorted(self.open, key=lambda item: item.pathcost) + # clear out the open list + self.open.clear() + # replace it with the sorted list by extending the blank list. + self.open.extend(sortedList) + return None # (b) Show your program loading in the 30-node sample file. # s=Searcher("30node.txt") diff --git a/RouteFinding/30node.png b/RouteFinding/30node.png index 17b4ea0..07c0e6a 100644 Binary files a/RouteFinding/30node.png and b/RouteFinding/30node.png differ diff --git a/RouteFinding/interpreter.py b/RouteFinding/interpreter.py new file mode 100644 index 0000000..c6b38b7 --- /dev/null +++ b/RouteFinding/interpreter.py @@ -0,0 +1,23 @@ +import code +import re +class FileConsole(code.InteractiveConsole): + """Emulate python console but use file instead of stdin""" + def raw_input(self, prompt): + line = self.f.readline() + if line=="": + raise EOFError() + no_newline = line.replace("\n", "") + no_indent = re.match(r"^\s", line) is None + print(prompt, no_newline, sep="") + return no_newline + +def run_file(filename): + fc = FileConsole() + fc.f = open(filename) + fc.interact(banner="", exitmsg="") + +if __name__ == "__main__": + import sys + sys.ps1 = "\n>>> " + run_file(sys.argv[1]) +