gpt4 book ai didi

python-3.x - 解决8-puzzle的BFS的Python实现需要很长时间才能找到解决方案

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:45:51 25 4
gpt4 key购买 nike

我在 Python 中实现 BFS 来解决 8 难题至少需要 21 分钟才能找到解决方案。我怎样才能改进我的代码以获得更好的时间?我实现的方式非常低效。我想知道有关如何改进它以在可接受的时间内解决问题的任何建议。

class Node():
def __init__(self, board=[]):
self.board=board
self.adjacency_list=[]

def get_adjacency_list(self):
return self.adjacency_list

def set_adjacency_list(self, adjacency_list):
self.adjacency_list = adjacency_list

def add_item_to_adjacency_list(self, item):
self.adjacency_list.append(item)

def generate_adjacency_list(self):
'''
Generates the adjancency list
from a given puzzle 8's board.
'''
adj_lists = []
empty_cell = 0
row_empty_cell = col_empty_cell = 0
tmp_array = None

for array in self.board:
if empty_cell in array:
tmp_array = array
break
row_empty_cell = self.board.index(tmp_array)
col_empty_cell = tmp_array.index(empty_cell)

left = (row_empty_cell, col_empty_cell - 1)
right = (row_empty_cell, col_empty_cell + 1)
up = (row_empty_cell - 1, col_empty_cell)
down = (row_empty_cell + 1, col_empty_cell)
max_bound = 3

for direction in [left, up, right, down]:
(row, col) = direction
if row >= 0 and row < max_bound and col >= 0 and col < max_bound:
adj_list = [r[:] for r in self.board]
adj_list[row_empty_cell][col_empty_cell] = adj_list[row][col]
adj_list[row][col] = empty_cell
self.add_item_to_adjacency_list(Node(adj_list))

def bfs(root_node, goal_node):
'''
Implementation of the Breadth
First Search algorithm.
The problem to be solved by this
algorithm is the Puzzle 8 game.

input: root -- the root node where
the search begins.
goal_node -- The objective to reach.

return:
(path, node) -- A tuple with a
dictionary path whose key node
gives the path backwards to the
objective node.
'''
frontier = [root_node]
path = {root_node : None} # The path where a node came from
level = {root_node : 0}
boards = [root_node.get_board()] # List of boards to check a board was already generated
i = 1
while frontier:
next_frontier = []
for node_parent in frontier:
if node_parent.get_board() == goal_node.get_board():
return (path, node_parent)
node_parent.generate_adjacency_list()
for children in node_parent.get_adjacency_list():
if children.get_board() not in boards:
boards.append(children.get_board())
next_frontier.append(children)
level[children] = i
path[children] = node_parent
frontier = next_frontier
print("Level ", i)
print("Number of nodes ", len(frontier))
i += 1
return (path, root_node)

root_node = Node([[2, 6, 0],
[5, 7, 3],
[8, 1, 4]])

goal_node = Node([[1, 2, 3],
[4, 5, 6],
[7, 8, 0]])

import time
start = time.time()

path, node = bfs(root_node, goal_node)

end = time.time()
print(end - start)

最佳答案

我认为问题出在这一行:

 if children.get_board() not in boards:

这是一个线性搜索,尝试将其更改为二分搜索。

关于python-3.x - 解决8-puzzle的BFS的Python实现需要很长时间才能找到解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57514410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com