示例#1
0
def ucs(state):
    # initialize the frontier
    frontier = PriorityQueue()
    node0 = Node(state, None)
    frontier.push(node0)

    # record the visited node
    visited = []
    visited.append(node0.state)

    while True:
        solution = []

        if frontier.isempty():
            print("Failure. There is no solution!")
            return None

        leafnode = frontier.pop()
        if leafnode.state == goal_state:
            print("GOAL!")
            solution.append(leafnode.state)
            while leafnode.parent is not None:
                leafnode = leafnode.parent
                solution.append(leafnode.state)
            return solution

        # add child and traverse the children of leafnode
        for i in range(2, len(leafnode.state)):
            childstate = leafnode.flip(i)
            childnode = Node(childstate, leafnode)
            leafnode.add_child(childnode)
            childnode.calculate_priority()
            childnode.priority = childnode.backCost

            if not frontier.find(childnode):
                if not childnode.state in visited:
                    frontier.push(childnode)
                    visited.append(childnode.state)
            else:
                node_in_queue = frontier.get_node(childnode)
                if node_in_queue.priority > childnode.priority:
                    frontier.delete(node_in_queue)
                    frontier.push(childnode)
示例#2
0
    def getActions(self, start, final):
        print(
            "*****************************************************************************************************"
        )
        print("Cel: " + str(final))
        actions = []
        explored = []
        currentState = start
        currentState.priority = 99
        fringe = PriorityQueue()
        fringe.insert(currentState)

        while not self.testGoal(currentState, final):
            currentState = fringe.delete(currentState, self.mapElements)
            print("Zmieniamy obecny stan")
            print(str(currentState.position), str(currentState.rotation))

            explored.append(currentState)

            if self.testGoal(currentState, final):
                ns = currentState
                while ns.parent is not None:
                    actions = [ns.action] + actions
                    ns = ns.parent
                return actions

            #explored.append(currentState)
            for j in self.getSuccessors(currentState):
                print("Sprawdzam dany następnik:")
                x = copy.deepcopy(j[1])
                x.action = j[0]
                x.parent = copy.deepcopy(currentState)
                x.priority = self.getPriority(x, final)
                print(str(x.position), str(x.rotation), "priorytet: ",
                      str(x.priority))
                if not self.stateValueExists(
                        x, explored) and not self.stateValueExists(
                            x, fringe.queue):
                    fringe.insert(x)
                    print(
                        "Dodano następnik, nie było go ani we fringe ani w explored"
                    )
                elif self.stateValueExists(x, fringe.queue):
                    print("Następnik był we fringe")
                    for i in fringe.queue:
                        if x.position == i.position and x.rotation == i.rotation and x.priority < i.priority:
                            i = x  #copy.deepcopy(x)
            print(
                "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
            )
示例#3
0
class UCS(Algorithm):

    def __init__(self, initial_vertex):
        super().__init__(Graph(initial_vertex))
        self.inistial_vertex = initial_vertex
        self.frontier = PriorityQueue()
        self.frontier.insert(initial_vertex)
        self.explored = []

    def execute(self):
        while True:
            if self.frontier.is_empty():
                return False
            lowest_cost_rubik = self.frontier.delete()
            if self.goal_test(lowest_cost_rubik):
                return True
            self.explored.append(lowest_cost_rubik)
            children = lowest_cost_rubik.children_nodes()
            self.update_number_of_nodes_info_after_expanding(len(children))
            for child_rubik in children:
                child_rubik.path_cost = lowest_cost_rubik.path_cost + 1
                if not self.visited(child_rubik):
                    print(child_rubik.path_cost)
                    self.frontier.insert(child_rubik)
                else:
                    self.nodes_in_memory -= 1
                    for rubik_in_queue in self.frontier.queue:
                        if rubik_in_queue.equal(child_rubik):
                            if rubik_in_queue.path_cost > child_rubik.path_cost:
                                self.frontier.queue.remove(rubik_in_queue)
                                self.frontier.insert(child_rubik)
                            else:
                                break

    def visited(self, rubik):
        if self.inside_queue(self.frontier.queue, rubik):
            return True
        if self.inside_queue(self.explored, rubik):
            return True
        return False