Completed the insert functions.

This commit is contained in:
Charles Descamps 2025-02-15 23:38:41 -07:00
parent 7ce4c6c5ae
commit 54477a4266
3 changed files with 85 additions and 2 deletions

View file

@ -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")