Пример #1
0
    def expand(self, node):
        south_state = self.environment.apply_action("SOUTH", node.state.copy())
        north_state = self.environment.apply_action("NORTH", node.state.copy())
        west_state = self.environment.apply_action("WEST", node.state.copy())
        east_state = self.environment.apply_action("EAST", node.state.copy())

        return [
            Node(node, south_state, 1, "SOUTH"),
            Node(node, north_state, 1, "NORTH"),
            Node(node, west_state, 1, "WEST"),
            Node(node, east_state, 1, "EAST")
        ]
Пример #2
0
 def search(self):
     self.frontier.append(Node(None, self.state, 0, "Iniciar"))
     while True:
         if len(self.frontier) == 0:
             return None
         node = self.frontier.pop(0)
         #print(node.state)
         if (self.objective_test(node.state)):
             return node.solution()
         children = self.expand(node)
         for c_node in children:
             self.frontier.append(c_node)
Пример #3
0
 def search(self):
     self.frontier.append(Node(None, self.state, 0, "Iniciar"))
     self.frontier_log[genkey(self.state)] = True
     while True:
         if len(self.frontier) == 0:
             return None
         node = self.frontier.pop(0)
         del self.frontier_log[genkey(node.state)]
         if (node.state[0] == self.goal[0]
                 and node.state[1] == self.goal[1]):
             return node.solution()
         self.closed[genkey(node.state)] = True
         children = self.expand(node)
         for c_node in children:
             key = genkey(c_node.state)
             if (not key in self.closed) and (not key in self.frontier_log):
                 self.frontier.append(c_node)
                 self.frontier_log[key] = True
Пример #4
0
 def search(self):
     self.closed = {}
     self.frontier_log = {}
     self.frontier.append(Node(None, self.state, 0, None))
     self.frontier_log[genkey(self.state)] = True
     while True:
         if len(self.frontier) == 0:
             return None
         node = self.frontier.pop(0)
         del self.frontier_log[genkey(node.state)]
         if (self.objective_test(node.state)):
             return node.solution()
         self.closed[genkey(node.state)] = True
         children = self.expand(node)
         for c_node in children:
             key = genkey(c_node.state)
             if (not key in self.closed) and (not key in self.frontier_log):
                 self.frontier.append(c_node)
                 self.frontier_log[key] = True
Пример #5
0
 def expand(self, node):
     esvaziar_3 = [node.state[0], 0]
     esvaziar_4 = [0, node.state[1]]
     d3_4 = min(node.state[1], max(0, 4 - node.state[0]))
     d4_3 = min(node.state[0], max(0, 3 - node.state[1]))
     transferir_3_para_4 = [node.state[0] + d3_4, node.state[1] - d3_4]
     transferir_4_para_3 = [node.state[0] - d4_3, node.state[1] + d4_3]
     encher3 = [node.state[0], 3]
     encher4 = [4, node.state[1]]
     return [
         Node(node, esvaziar_3, 1, "esvaziar 3"),
         Node(node, esvaziar_4, 1, "esvaziar 4"),
         Node(node, transferir_3_para_4, 1, "transferir de 3 para 4"),
         Node(node, transferir_4_para_3, 1, "transferir de 4 para 3"),
         Node(node, encher3, 1, "encher 3"),
         Node(node, encher4, 1, "encher 4")
     ]
Пример #6
0
    def search(self):
        self.closed = {}
        self.frontier_log = {}
        self.frontier.append(Node(None, self.state, 0, None))
        self.frontier_log[genkey(self.state)] = True
        while True:
            if len(self.frontier) == 0:
                return None
            node = self.frontier.pop(0)

            del self.frontier_log[genkey(node.state)]
            if (self.objective_test(node.state)):
                print("SOLUTION")
                print_state(node.state)
                return node.solution()
            self.closed[genkey(node.state)] = True
            children = self.expand(node)
            for c_node in children:
                key = genkey(c_node.state)
                if (not key in self.closed) and (not key in self.frontier_log):
                    c_node.fcost = f(self, c_node)
                    bisect.insort_right(self.frontier, c_node)
                    self.frontier_log[key] = True