【问题标题】:Markov Chain: Find the most probable path from point A to point B马尔可夫链:找到从 A 点到 B 点的最可能路径
【发布时间】:2020-11-04 06:36:25
【问题描述】:

我有一个使用字典的转换矩阵

{'hex1': {'hex2': 1.0},
 'hex2': {'hex4': 0.4, 'hex7': 0.2, 'hex6': 0.2, 'hex1': 0.2},
 'hex4': {'hex3': 1.0},
 'hex3': {'hex6': 0.3333333333333333, 'hex2': 0.6666666666666666},
 'hex6': {'hex1': 0.3333333333333333,
  'hex4': 0.3333333333333333,
  'hex5': 0.3333333333333333},
 'hex7': {'hex6': 1.0},
 'hex5': {'hex3': 1.0}}

显示从某个十六进制到另一个十六进制的概率(例如,hex1 的概率为 1 到 hex2hex2 的概率为 0.4 到 hex4)。

取起点和终点,我想找到概率最高的路径。

代码结构如下所示

def find_most_probable_path(start_hex, end_hex, max_path):
    path = compute for maximum probability path from start_hex to end_hex
    return path

其中 max_path 是要遍历的最大十六进制数。如果 max_path 中没有路径,则返回空/null。此外,如果在到达结束十六进制之前回到起始十六进制,则丢弃路径。

例如

find_most_probable_path(hex2, hex3, 5)
>> "hex2,hex4,hex3"

输出可以是十六进制列表或只是连接的字符串。

【问题讨论】:

    标签: python python-3.7 markov-chains


    【解决方案1】:

    我开发了一种算法,但我不知道它的效率,但效果很好。

    table={'hex1': {'hex2': 1.0},
     'hex2': {'hex4': 0.4, 'hex7': 0.2, 'hex6': 0.2, 'hex1': 0.2},
     'hex4': {'hex3': 1.0},
     'hex3': {'hex6': 0.3333333333333333, 'hex2': 0.6666666666666666},
     'hex6': {'hex1': 0.3333333333333333,
      'hex4': 0.3333333333333333,
      'hex5': 0.3333333333333333},
     'hex7': {'hex6': 1.0},
     'hex5': {'hex3': 1.0}}
    
    def find_most_probable_path(start_hex, end_hex, max_path=0):
        assigned=[start_hex]
        foundTrue=False
        prob=[{"nodes":[start_hex],"prob":1,"length":1}]
        if max_path==0:
            status=False
        else:
            status=True
        while status==True:
            chn=[]
            status=False
            for i in prob:
                if i["length"]<max_path:
                    lastElement=i["nodes"][-1]
                    for j in table[lastElement]:
                        if j not in assigned:
                            temp=i.copy()
                            js=temp["nodes"].copy()
                            js.append(j)
                            temp["nodes"]=js
                            temp["prob"]=temp["prob"]*table[lastElement][j]
                            temp["length"]+=1
                            #print(temp)
                            chn.append(temp)
                            status=True
            maxv=0
            for i in chn:
                if i["prob"]>=maxv:
                    maxv=i["prob"]
                    added=i
            if added["nodes"][-1]==end_hex:
                foundTrue=True
                status=False
            assigned.append(added["nodes"][-1])
            prob.append(added)
        if foundTrue==True:
            return prob[-1]["nodes"]
        else:
            return None
    
    
    print(find_most_probable_path("hex2", "hex3",5))
    

    输出将是:

    ['hex2', 'hex4', 'hex3']
    

    如果你想看路径的概率,你可以改变部分:

    if foundTrue==True:
        return prob[-1]["nodes"]
    

    到:

    if foundTrue==True:
        return prob[-1]
    

    然后程序给出这样的输出:

    {'nodes': ['hex2', 'hex4', 'hex3'], 'prob': 0.4, 'length': 3}
    

    【讨论】:

    • 谢谢。澄清一下,这是一种蛮力方法,通过迭代所有可能的路径组合来找到最可能的路径,对吗?
    • 我受到最短路径的启发并创建了一些类似的算法。这并没有检查所有可能的路径,主要思想是找到与节点列表相邻的最可能的节点。
    • 所以本质上,您将问题视为图形,类似于另一个答案中提到的 Dijkstra?
    • 没错,它与dijkstra算法非常相似,但不是最短路径,而是最可能路径。
    【解决方案2】:

    您可以将马尔可夫链视为有向加权图,并将概率用作图边的权重。

    从这一点开始,您可以使用 Dijkstra 算法从加权图上的两个点得出最短路径。

    https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 2018-03-24
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多