class Search(object): def __init__(self, problem): self.problem = problem self.result = Result("N/A") def BFS(self): self.frontiers = queue.Queue() self.frontiers.put(Node(self.problem.initialState, None, "Nothing")) j = 0 while (True): if (self.frontiers.empty()): self.result.changeStatus("Failure") return self.result leaf = self.frontiers.get() #print(leaf.state) self.problem.expand(leaf.state) for i in range(len(self.problem.nextStates)): child = Node(self.problem.nextStates[i], leaf, self.problem.nextAction[i]) # print(child.state) if ((child not in list(self.frontiers.queue))): if (child.state in self.problem.finalStates): self.result.changeStatus("Success") while (child.parent != None): self.result.addToPath(child.action) child = child.parent return self.result self.frontiers.put(child)
class Search(object): def __init__(self, problem): self.problem = problem self.result = Result("N/A") def IDFS(self, maxDepth): for depth in range(1, maxDepth): self.frontiers = queue.LifoQueue() self.explored = [] self.frontiers.put( Node(self.problem.initialState, None, "Nothing", 0)) while (True): if (self.frontiers.empty()): self.result.changeStatus("Failure") self.result.expandedNodes = self.problem.expandedNodes self.result.depth = depth return self.result leaf = self.frontiers.get() self.explored.append(leaf.state) self.problem.expand(leaf.state) for i in range(len(self.problem.nextStates)): child = Node(self.problem.nextStates[i], leaf, self.problem.nextAction[i], leaf.depth + 1) if ((child.state not in self.explored) and (child not in list(self.frontiers.queue)) and (child.depth <= depth)): if (child.state in self.problem.finalStates): self.result.changeStatus("Success") while (child.parent != None): self.result.addToPath(child.action) child = child.parent self.result.expandedNodes = self.problem.expandedNodes self.result.depth = depth return self.result self.frontiers.put(child)
class Search(object): def __init__(self, problem): self.problem = problem self.result = Result("N/A") def getNodeWithMinimumCost(self, frontiers): minimumLeaf = frontiers[0] for node in frontiers: if (node.costFromStart + node.costToGoal) <= ( minimumLeaf.costFromStart + minimumLeaf.costToGoal): minimumLeaf = node return minimumLeaf def Astar(self): self.frontiers = [] self.explored = [] self.frontiers.append( Node(self.problem.initialState, None, "None", 0, self.problem.getCostToGoal(self.problem.initialState))) while (True): if len(self.frontiers) == 0: self.result.changeStatus("Failure") self.result.expandedNodes = self.problem.expandedNodes return self.result leaf = self.getNodeWithMinimumCost(self.frontiers) self.frontiers.remove(leaf) self.explored.append(leaf.data) self.problem.expand(leaf.data) for i in range(len(self.problem.nextStates)): child = Node( self.problem.nextStates[i], leaf, self.problem.nextAction[i], leaf.costFromStart + 1, self.problem.getCostToGoal(self.problem.nextStates[i])) if (not ((child.data in self.explored)) and (child not in self.frontiers)): leaf.append(child) if (child.data in self.problem.finalStates): print(child.data) self.result.changeStatus("Success") while (child.data != self.problem.initialState): self.result.addToPath(child.action) child = child.parent self.result.expandedNodes = self.problem.expandedNodes return self.result self.frontiers.append(child)
class Search(object): def __init__(self, problem): self.problem = problem self.result = Result("N/A") def BFS(self, graphSearch=None): """BFS Search""" if (graphSearch != None): self.frontiers = queue.Queue() self.explored = [] self.frontiers.put(Node(self.problem.initialState, None, "Nothing")) while (True): if (self.frontiers.empty()): self.result.changeStatus("Failure") self.result.expandedNodes = self.problem.expandedNodes return self.result leaf = self.frontiers.get() self.explored.append(leaf.state) self.problem.expand(leaf.state) self.result.expansions = self.result.expansions + 1 for i in range(len(self.problem.nextStates)): child = Node(self.problem.nextStates[i], leaf, self.problem.nextAction[i]) leaf.addChild(child) if ((child.state not in self.explored) and (child not in list(self.frontiers.queue))): if (child.state in self.problem.finalStates): self.result.changeStatus("Success") while (child.parent != None): self.result.addToPath(child.action) child = child.parent self.result.expandedNodes = self.problem.expandedNodes return self.result self.frontiers.put(child) if (len(list(self.frontiers)) >= self.result.maxFrontiers): self.result.maxFrontiers = len(list( self.frontiers)) else: self.frontiers = queue.Queue() self.frontiers.put(Node(self.problem.initialState, None, "Nothing")) while (True): if (self.frontiers.empty()): self.result.changeStatus("Failure") self.result.expandedNodes = self.problem.expandedNodes return self.result leaf = self.frontiers.get() self.problem.expand(leaf.state) self.result.expansions = self.result.expansions + 1 for i in range(len(self.problem.nextStates)): child = Node(self.problem.nextStates[i], leaf, self.problem.nextAction[i]) leaf.addChild(child) if ((child not in list(self.frontiers.queue))): if (child.state in self.problem.finalStates): self.result.changeStatus("Success") while (child.parent != None): self.result.addToPath(child.action) child = child.parent self.result.expandedNodes = self.problem.expandedNodes return self.result self.frontiers.put(child)