AttributeError:节点实例没有属性'__getitem__'

时间:2014-01-30 14:40:59

标签: python attributes instance

首先我想说我搜索了我的问题,但由于问题的性质+我必须使用的代码,我没有找到解决方案。因此,这是我的问题:

Traceback (most recent call last):
  File "AlienTilesMain.py", line 28, in <module>
    goal = astar_search(prob, prob.h1)
  File "/home/chris/Desktop/search.py", line 274, in astar_search
    return best_first_graph_search(problem, lambda n: n.path_cost + h(n))
  File "/home/chris/Desktop/search.py", line 213, in best_first_graph_search
    frontier.append(node)
  File "/home/chris/Desktop/utils.py", line 738, in append
    bisect.insort(self.A, (self.f(item), item))
  File "/home/chris/Desktop/utils.py", line 612, in memoized_fn
    val = fn(obj, *args)
  File "/home/chris/Desktop/search.py", line 274, in <lambda>
    return best_first_graph_search(problem, lambda n: n.path_cost + h(n))
  File "/home/chris/Desktop/utils.py", line 612, in memoized_fn
    val = fn(obj, *args)
  File "/home/chris/Desktop/AlienTiles.py", line 57, in h1
   n = len(state[0])
AttributeError: Node instance has no attribute '__getitem__'

我的代码就是这个:

def h1(self, state):
    n = len(state[0])
.
.
return value

在我的主文件中:

goal = astar_search(prob, prob.h1)

以下是问题,节点类和A *:

class Problem(object):
    def __init__(self, initial, goal):
        """The constructor specifies the initial state, and possibly a goal
        state, if there is a unique goal.  Your subclass's constructor can add
        other arguments."""
        self.initial = initial; self.goal = goal

    def actions(self, state):
        """Return the actions that can be executed in the given
        state. The result would typically be a list, but if there are
        many actions, consider yielding them one at a time in an
        iterator, rather than building them all at once."""
        abstract

    def result(self, state, action):
        """Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state)."""
        abstract
class Node:
"""A node in a search tree. Contains a pointer to the parent (the node
that this is a successor of) and to the actual state for this node. Note
that if a state is arrived at by two paths, then there are two nodes with
the same state.  Also includes the action that got us to this state, and
the total path_cost (also known as g) to reach the node.  Other functions
may add an f and h value; see best_first_graph_search and astar_search for
an explanation of how the f and h values are handled. You will not need to
subclass this class."""

def __init__(self, state, parent=None, action=None, path_cost=0):
    "Create a search tree Node, derived from a parent by an action."
    update(self, state=state, parent=parent, action=action,
           path_cost=path_cost, depth=0)
    if parent:
        self.depth = parent.depth + 1

def __repr__(self):
    return "<Node %s>" % (self.state,)

def expand(self, problem):
    "List the nodes reachable in one step from this node."
    return [self.child_node(problem, action)
            for action in problem.actions(self.state)]

def child_node(self, problem, action):
    "Fig. 3.10"
    next = problem.result(self.state, action)
    return Node(next, self, action,
                problem.path_cost(self.path_cost, self.state, action, next))

def solution(self):
    "Return the sequence of actions to go from the root to this node."
    return [node.action for node in self.path()[1:]]

def path(self):
    "Return a list of nodes forming the path from the root to this node."
    node, path_back = self, []
    while node:
        path_back.append(node)
        node = node.parent
    return list(reversed(path_back))

def astar_search(problem, h=None):
    """A* search is best-first graph search with f(n) = g(n)+h(n).
    You need to specify the h function when you call astar_search, or
    else in your Problem subclass."""
    h = memoize(h or problem.h, 'h')
    return best_first_graph_search(problem, lambda n: n.path_cost + h(n))

0 个答案:

没有答案