Completed the insert functions.
This commit is contained in:
parent
7ce4c6c5ae
commit
54477a4266
3 changed files with 85 additions and 2 deletions
|
|
@ -2,9 +2,19 @@
|
||||||
import DRDViz
|
import DRDViz
|
||||||
|
|
||||||
class SearchNode:
|
class SearchNode:
|
||||||
def __init__(self, label, value):
|
def __init__(self, label, pathcost):
|
||||||
self.label = label
|
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:
|
class Searcher:
|
||||||
def __init__(self, mapfile):
|
def __init__(self, mapfile):
|
||||||
|
|
@ -60,9 +70,59 @@ class Searcher:
|
||||||
# need to get the myViz attribute by importing the DRDViz stuff.
|
# need to get the myViz attribute by importing the DRDViz stuff.
|
||||||
self.myViz = DRDViz.DRDViz()
|
self.myViz = DRDViz.DRDViz()
|
||||||
# now, start using the myViz thingy to 'display' a map.
|
# 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):
|
def setStartGoal(self, startNode, endNode):
|
||||||
self.startNode = startNode
|
self.startNode = startNode
|
||||||
self.endNode = endNode
|
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.
|
# (b) Show your program loading in the 30-node sample file.
|
||||||
# s=Searcher("30node.txt")
|
# s=Searcher("30node.txt")
|
||||||
|
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 105 KiB |
23
RouteFinding/interpreter.py
Normal file
23
RouteFinding/interpreter.py
Normal file
|
|
@ -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])
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue