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)
Exemplo n.º 2
0
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 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")
                    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.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 = "Success"
                            while(child.parent != None):
                                self.path.append(child.action)
                                child = child.parent
                            print(self.result)
                            self.path.reverse()
                            return self.path
                        self.frontiers.put(child)
        else:
            self.frontiers = queue.Queue()
            self.frontiers.put(Node(self.problem.initialState , None , "Nothing"))
            while(True):
                if(self.frontiers.empty()):
                    self.result.changeStatus("Failure")
                    return self.result
                leaf = self.frontiers.get()
                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.addChild(child)
                    if((child not in list(self.frontiers.queue))):
                        if(child.state in self.problem.finalStates):
                            self.result = "Success"
                            while(child.parent != None):
                                self.path.append(child.action)
                                child = child.parent
                            print(self.result)
                            self.path.reverse()
                            return self.path
                        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 DFS(self, limit):
        """Limited Version of DFS"""
        self.frontiers = queue.LifoQueue()
        self.explored = []
        self.frontiers.put(
            Node(self.problem.initialState, None, "Nothing", depth=0))
        while (True):
            if (self.frontiers.empty()):
                self.result.changeStatus("Failure")
                self.result.expandedNodes = self.problem.expandedNodes
                self.result.expandedNodes = child.depth
                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],
                             depth=(leaf.depth + 1))
                if ((child.state not in self.explored)
                        and (child not in list(self.frontiers.queue))
                        and (child.depth != limit)):
                    if (child.state in self.problem.finalStates):
                        self.result.changeStatus("Success")
                        self.result.expandedNodes = self.problem.expandedNodes
                        self.result.depth = child.depth
                        while (child.parent != None):
                            self.result.path.append(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 BiDirectional(self):
        self.initialFrontiers = queue.Queue()
        self.initialExplored = []
        self.goalFrontiers = queue.Queue()
        self.goalExplored = []
        self.initialFrontiers.put(
            Node(self.problem.initialState, None, "Nothing"))
        dummyParent = Node("dummy", None, "Nothing")
        for goalState in self.problem.finalStates:
            goal = Node(goalState, dummyParent, "Nothing")
            dummyParent.addChild(goal)
        for goalState in dummyParent.children:
            self.goalFrontiers.put(goalState)
        while (self.result.status == "N/A"):
            self.initialExpansion()
            self.goalExpansion()
        return self.result

    def initialExpansion(self):
        if (self.initialFrontiers.empty()):
            self.result.changeStatus("Failure")
            return self.result
        leaf = self.initialFrontiers.get()
        self.initialExplored.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])
            if ((child.state not in self.initialExplored)
                    and (child not in list(self.initialFrontiers.queue))):
                for goal in list(self.goalFrontiers.queue):
                    if (child.state == goal.state):
                        self.result.changeStatus("Success")
                        while (child.parent != None):
                            self.result.path.append(child.action)
                            print(child.state)
                            child = child.parent
                        self.result.path.reverse()
                        print(self.result.path)
                        while (goal.parent.state != "dummy"):
                            self.result.path.append(
                                self.problem.flipAction(goal.action))
                            print(goal.state)
                            goal = goal.parent
                        print("succeeees")
                        print(goal.state)
                        return self.result
                self.initialFrontiers.put(child)

    def goalExpansion(self):
        if (self.goalFrontiers.empty()):
            self.result.changeStatus("Failure")
            return self.result
        leaf = self.goalFrontiers.get()
        self.goalExplored.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])
            if ((child.state not in self.goalExplored)
                    and (child not in list(self.goalFrontiers.queue))):
                self.goalFrontiers.put(child)