上述代码依旧报错'list' object has no attribute 'state'
时间: 2023-10-11 12:10:07 浏览: 70
非常抱歉,看来我的修正还不够彻底。在之前的代码中,我们将起始状态和目标状态作为 Node 对象进行了定义,但在实际使用中,我们应该将它们作为列表而不是 Node 对象传递给搜索算法。以下是修正后的代码:
```python
from queue import Queue
from collections import deque
class Node:
def __init__(self, state, parent=None, action=None, cost=0):
self.state = state
self.parent = parent
self.action = action
self.cost = cost
def __eq__(self, other):
return self.state == other.state
def __hash__(self):
return hash(str(self.state))
def __str__(self):
return str(self.state)
def expand(self):
successors = []
zero_pos = self.state.index(0)
if zero_pos > 2:
new_state = self.state.copy()
new_state[zero_pos], new_state[zero_pos - 3] = new_state[zero_pos - 3], new_state[zero_pos]
successors.append(Node(new_state, self, 'up', self.cost+1))
if zero_pos < 6:
new_state = self.state.copy()
new_state[zero_pos], new_state[zero_pos + 3] = new_state[zero_pos + 3], new_state[zero_pos]
successors.append(Node(new_state, self, 'down', self.cost+1))
if zero_pos % 3 > 0:
new_state = self.state.copy()
new_state[zero_pos], new_state[zero_pos - 1] = new_state[zero_pos - 1], new_state[zero_pos]
successors.append(Node(new_state, self, 'left', self.cost+1))
if zero_pos % 3 < 2:
new_state = self.state.copy()
new_state[zero_pos], new_state[zero_pos + 1] = new_state[zero_pos + 1], new_state[zero_pos]
successors.append(Node(new_state, self, 'right', self.cost+1))
return successors
def is_goal(self, goal):
return self.state == goal
def bfs(start, goal):
start_node = Node(start)
goal_node = Node(goal)
frontier = Queue()
frontier.put(start_node)
explored = set()
while not frontier.empty():
node = frontier.get()
if node.is_goal(goal_node):
return node
explored.add(node)
for child in node.expand():
if child not in explored and child not in frontier.queue:
frontier.put(child)
def dfs(start, goal):
start_node = Node(start)
goal_node = Node(goal)
frontier = deque()
frontier.append(start_node)
explored = set()
while frontier:
node = frontier.pop()
if node.is_goal(goal_node):
return node
explored.add(node)
for child in node.expand()[::-1]:
if child not in explored and child not in frontier:
frontier.append(child)
def astar(start, goal):
start_node = Node(start)
goal_node = Node(goal)
frontier = []
frontier.append(start_node)
explored = set()
while frontier:
frontier.sort(key=lambda x: x.cost + heuristic(x.state, goal))
node = frontier.pop(0)
if node.is_goal(goal_node):
return node
explored.add(node)
for child in node.expand():
if child not in explored and child not in frontier:
frontier.append(child)
def heuristic(state, goal):
return sum(abs(b%3 - g%3) + abs(b//3 - g//3) for b, g in ((state.index(i), goal.index(i)) for i in range(1, 9)))
# Example usage
start_state = [1, 2, 3, 4, 5, 6, 0, 7, 8]
goal_state = [1, 2, 3, 4, 5, 6, 7, 8, 0]
bfs_result = bfs(start_state, goal_state)
dfs_result = dfs(start_state, goal_state)
astar_result = astar(start_state, goal_state)
print('BFS solution:', [node.action for node in bfs_result.path()])
print('DFS solution:', [node.action for node in dfs_result.path()])
print('A* solution:', [node.action for node in astar_result.path()])
```
在这个修正后的代码中,我们将起始状态和目标状态作为列表传递给搜索算法,并在每个搜索算法中将它们转换为 Node 对象。如果您还有其他问题,请随时告诉我,我会尽力回答!
阅读全文