依旧报错'list' object has no attribute 'state'
时间: 2023-10-09 15:12:33 浏览: 77
非常抱歉,看来我的之前的修正还不够彻底。在这个修正后的代码中,我们需要将起始状态和目标状态分别作为 Node 对象传递给搜索算法,并在搜索算法中使用它们的 state 属性进行比较。以下是修正后的代码:
```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.state
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.state))
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 对象传递给搜索算法,并在搜索算法中使用它们的 state 属性进行比较。如果您还有其他问题,请随时告诉我,我会尽力回答!
阅读全文