【问题标题】:Walk through decision tree and capture each node遍历决策树并捕获每个节点
【发布时间】:2020-01-27 06:03:03
【问题描述】:

我正在尝试一次遍历一个节点。

每个节点可以有2-3条路径

在有 3 条路径的节点上,其中一条路径始终为终点,一条有时为终点

我们不能倒退,但可以重新开始

我们可用的功能是

getCurrentNode()        #returns string of current node's path from start (ex. 'A-B-A-A-B')
getCurrentNodePaths()       #returns number of possible paths from this node
startOver()         #puts us back at node 0
takePath(int pathNumber)    #traverse the decision tree down a desired path

我已经编写了这个伪代码,它应该递归遍历每个节点,但仅限于“最左边”的路径

# Start
def walk(pathNumber):
  takePath(pathNumber)
  next_nodes_paths = getCurrentNodePaths()
  if next_nodes_paths.length > 0:
    walk(0)


startOver()
walk(0)

我怎样才能让它跟踪它的位置、重新开始并采取新的路径

【问题讨论】:

  • 你可以给walk添加参数吗?
  • startOver() 是否也会重置 getCurrentNode() 的值?
  • @CalebGoodman 是的,请对walk 进行任何更改。是的,startOver() 重置了 getCurrentNode() 的值
  • 你能有一个全局变量来存储所有检查的路径吗?喜欢从getCurrentNode() 添加每个路径,然后检查它是否已经在?
  • 您可以使用networkx 之类的东西来完成这项工作吗?这样就很容易了。

标签: python functional-programming tree decision-tree


【解决方案1】:

由于我们无法向后移动,您的(递归)深度优先搜索方法可能难以处理:如果没有实际移动到那里,您永远无法知道节点是否是结束节点,一旦到达,您只能从一开始就一直走到前一个节点。

我建议改用breadth-first search(部分采用this example):

def walk(currentNode):
    queue = []
    visited = []
    queue.append(currentNode)
    visited.append(currentNode)
    while queue:
        s = queue.pop(0)
        # go to node from start by following the path
        startOver()
        for p in s:
            takePath(int(p))
        for i in range(getCurrentNodePaths()):
            nextNode = getCurrentNode() + str(i)
            queue.append(nextNode)
            visited.append(nextNode)
        # Use this if you want a list of paths to end points only
        # if getCurrentNodePaths() > 0:
        #     visited.remove(s)
    print(visited)

startOver()
walk(getCurrentNode())

这将为您提供visited 中树中所有节点的路径列表。

一些注意事项:

  • queuevisited 列表中的节点假定由其从起始节点的路径表示为字符串(例如 0101012012)。
  • 因此,只需按照其数字序列即可到达节点。
  • 此外,可以通过以下方式构造后继节点 在range(getCurrentNodePaths()) 中附加数字。

【讨论】:

    【解决方案2】:

    这将创建一个决策树模型。您可以使用 select_path 方法导航到某个节点。 path 是一个类似于 '03231002' 的字符串。您可以使用 apply_function 方法遍历整个树并在每个点应用一个函数。有一个走整棵树的例子。

    def select_path(path):
        startOver()
        for pathNumber in path:
            takePath(int(pathNumber))
    
    class Node:
        def __init__(self,path):
            self.path = path
            self.select()
            self.num_children = getCurrentNodePaths().length
            self.children = [Node(path+str(i)) for i in range(self.num_children)]
    
        def select(self):
            select_path(self.path)
    
        def apply_function(self, func, recursive=False):
            self.select()
            func()
            if recursive:
                for child in self.children:
                    apply_function(self, func, recursive=True)
    
    root = Node('')
    
    #walk whole tree and apply function function:
    #def function:
    #    pass
    #root.apply_function(function, recursive=True)
    

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 2019-01-28
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多