问题
def exploring_nodes(node):
print("Exploring Nodes")
actions = ["down", "up", "left", "right"]
goal_node = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 0]])
node_q = [node]
final_nodes = []
visited = []
final_nodes.append(node_q[0].data.tolist()) # Only writing data of nodes in seen
node_counter = 0 # To define a unique ID to all the nodes formed
while node_q:
current_root = node_q.pop(0) # Pop the element 0 from the list
if current_root.data.tolist() == goal_node.tolist():
print("Goal reached")
return current_root, final_nodes, visited
for move in actions:
temp_data = move_tile(move, current_root.data)
if temp_data is not None:
node_counter += 1
child_node = Node(node_counter, np.array(temp_data), current_root, move, 0) # Create a child node
if child_node.data.tolist() not in final_nodes: # Add the child node data in final node list
node_q.append(child_node)
final_nodes.append(child_node.data.tolist())
visited.append(child_node)
if child_node.data.tolist() == goal_node.tolist():
print("Goal_reached")
return child_node, final_nodes, visited
return None, None, None # return statement if the goal node is not reached
I am trying to study the 8 puzzle problem implementation using brute force search method. However I ma having troule a few lines of code. What does the
(node_q[0].data.tolist())
and
current_root.data.tolist() == goal_node.tolist()
do?
I know in the first one something is being appended to final_nodes but what exactly? node_q[0] corresponds to the first element in node_q but what is the next data.tolist() part doing? I'm basically having trouble understanding whenever there is something like current_root.data.tolist()
OR node_q[0].data.tolist()
OR child_node.data.tolist()
Also, data is 3x3 numpy array.
来源:https://stackoverflow.com/questions/60235401/8-puzzle-problem-using-brute-force-search