int'对象没有属性'__getitem__'Python

时间:2016-10-31 01:25:29

标签: python artificial-intelligence

这是树问题

    class TreeProblem(Problem):

    # methods exported to Search class ---------------------------------------------    

        # initial "state" of a tree is just its root    
        def initialState(self):
        return self.root.id

        # goal test is whether goal matches node label
        def goalTest(self, state):
        node = self.nodes[state]
        return node.isgoal

        # successors of a node are its children
        def successorFn(self, state):
        node = self.nodes[state]
        if node.isTerminal():
            return []
        else:
            return [self._contents('l', node.lchild), \
                    self._contents('r', node.rchild)]

        # step cost state--[action]-->result is just the random cost associated with result
        def stepCost(self, state, action, result):
        node = self.nodes[result]
        return node.cost

        # no action is actually taken; we just print out the state for debugging    
        def takeAction(self, state, action): 
        print 'visit ', state
            return 'visit', state

这是Node类

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."
          # state = force_hashable(state)
          self.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.takeAction(self.state, self.action)]
          #print problem
          #return [Node(next, self, act,
           #          problem.stepCost(self.state, act, next[1]))
           #     for (act, next) in problem.successorFn(self.state)]

      def child_node(self, problem, action):
          "Fig. 3.10"
          (act, next) = problem.successorFn(self.state)
          print str(next[1]) + "\n"
          return Node(next, self, action,
                      problem.stepCost(self.state, action, next[1]))

      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 update(self, x, **entries):
          """Update a dict, or an object with slots, according to `entries` dict.
          >>> update({'a': 1}, a=10, b=20)
          {'a': 10, 'b': 20}
          >>> update(Struct(a=1), a=10, b=20)
          Struct(a=10, b=20)
          """
          if isinstance(x, dict):
             x.update(entries)
          else:
             x.__dict__.update(entries)
          return x

      # We want for a queue of nodes in breadth_first_search or
      # astar_search to have no duplicated states, so we treat nodes
      # with the same state as equal. [Problem: this may not be what you
      # want in other contexts.]

      def __eq__(self, other):
         return isinstance(other, Node) and self.state == other.state

      def __hash__(self):
          return hash(str(self.state))

这是dfs_search代码:

def depth_limited_search(self, problem, limit=50):
          return self.recursive_dls(Node(problem.initialState()), problem, limit)

      def recursive_dls(self, node, problem, limit):
          print str(type(node.state)) + "\n"
          new_state=node.state
          print new_state[1]
          if problem.goalTest(node.state):
             return node
          elif node.depth == limit:
             return 'cutoff'
          else:
             cutoff_occurred = False
             for child in node.expand(problem):
                 result = self.recursive_dls(child, problem, limit)
                 if result == 'cutoff':
                     cutoff_occurred = True
                 elif result is not None:
                      return result
             return if_(cutoff_occurred, 'cutoff', None)

这里,node.state类型是元组 有价值('r',2) 当我说node.state [1]

它给int'对象没有属性' getitem ' 以上错误 请在这里指导

0 个答案:

没有答案