71 lines
3.3 KiB
Python
71 lines
3.3 KiB
Python
# Define Searcher, hSLD, SearchNode.
|
|
class SearchNode:
|
|
def __init__(self, label, value):
|
|
self.label = label
|
|
self.value = value
|
|
|
|
class Searcher:
|
|
def __init__(self, mapfile):
|
|
self.mapfile = mapfile
|
|
# load the mapfile's structure
|
|
with open(self.mapfile) as mapper:
|
|
mapContents = mapper.read()
|
|
mapContents = mapContents.split("\n")
|
|
for mapIter in range(len(mapContents)):
|
|
mapContents[mapIter] = mapContents[mapIter].split(", ")
|
|
for firstDimMapIter in range(len(mapContents) - 1):
|
|
for secondDimMapIter in range(len(mapContents[firstDimMapIter])):
|
|
mapContents[firstDimMapIter][secondDimMapIter] = mapContents[firstDimMapIter][secondDimMapIter].strip("')(][")
|
|
mapContents.pop(len(mapContents) - 1)
|
|
for tupleIter in range(len(mapContents)):
|
|
mapContents[tupleIter] = tuple(mapContents[tupleIter])
|
|
# print(mapContents) # list of tuples
|
|
# print(mapContents[0]) # tuple of strings (data)
|
|
# [first node, second node, path cost, first node x, first node y, second node x, second node y]
|
|
# 0 1 2 3 4 5 6
|
|
# print(mapContents[0][0]) # data, string format
|
|
# make two dictionaries of nodes, iterating through list of tuples to make note of every node's paths and locations
|
|
nodeLoc = dict()
|
|
nodePaths = dict()
|
|
for nodeIter in range(len(mapContents)):
|
|
# these node locations are fine, because they should technically be in only one place.
|
|
nodeLoc[mapContents[nodeIter][0]] = tuple((mapContents[nodeIter][3], mapContents[nodeIter][4]))
|
|
nodeLoc[mapContents[nodeIter][1]] = tuple((mapContents[nodeIter][5], mapContents[nodeIter][6]))
|
|
# however, this is only taking the first node path it gets to...
|
|
# I probably need a list instead, that gets updated if the node is not in the node's child list.
|
|
nodePaths[mapContents[nodeIter][0]] = tuple((mapContents[nodeIter][1], mapContents[nodeIter][2]))
|
|
print(nodeLoc)
|
|
print(nodePaths)
|
|
|
|
# (b) Show your program loading in the 30-node sample file.
|
|
s=Searcher("30node.txt")
|
|
# (c) Show you program setting start node=U and end node=T.
|
|
s.setStartGoal('U','T')
|
|
# myViz should be a DRDViz instance -> save map to file on disk.
|
|
s.myViz.save("30node.png")
|
|
# (d) Show the one open node.
|
|
[n.showBasic() for n in s.open]
|
|
# (e) Show successors of only open node.
|
|
initial_children = s.successors(s.open.pop(0))
|
|
[n.showBasic() for n in initial_children]
|
|
|
|
# (f) Show three inserts: at the front, and the end, and "in order"
|
|
def reset_insert(where):
|
|
s.reset()
|
|
initial_children = s.successors(s.open.pop(0))
|
|
insert_method = getattr(s, "insert_"+where)
|
|
insert_method(initial_children)
|
|
return [n.showBasic() for n in s.open]
|
|
|
|
reset_insert("front")
|
|
reset_insert("end")
|
|
reset_insert("ordered")
|
|
|
|
# (g) INSERT (K,500), (C,91) and (J,10) and show no duplicates.
|
|
newdata = (("K",500), ("C",91), ("J",10))
|
|
newlist = [SearchNode(label=label, pathcost=pathcost) for label, pathcost in newdata]
|
|
ignored = s.insert_end(newlist)
|
|
[n.showBasic() for n in s.open]
|
|
|
|
# 3. hSLD heuritic function being called on three nodes.
|
|
[hSLD(x, s) for x in ("V", "AC", "J")]
|