马尔可夫链:找到从A点到B点的最可能路径
Markov Chain: Find the most probable path from point A to point B
我有一个使用字典的转换矩阵
{'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 去 hex2
,hex2
有概率 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、return、empty/null内没有路径。此外,如果在到达结束十六进制之前返回到起始十六进制,则删除路径。
例如
find_most_probable_path(hex2, hex3, 5)
>> "hex2,hex4,hex3"
输出可以是十六进制列表或只是连接的字符串。
您可以将马尔可夫链视为有向加权图,并将概率用作图边权重。
从这里开始,您可以使用 Dijkstra 算法求出加权图上两点的最短路径。
我开发了一个算法,但我不知道它的效率,但它运行良好。
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}
我有一个使用字典的转换矩阵
{'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 去 hex2
,hex2
有概率 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、return、empty/null内没有路径。此外,如果在到达结束十六进制之前返回到起始十六进制,则删除路径。
例如
find_most_probable_path(hex2, hex3, 5)
>> "hex2,hex4,hex3"
输出可以是十六进制列表或只是连接的字符串。
您可以将马尔可夫链视为有向加权图,并将概率用作图边权重。
从这里开始,您可以使用 Dijkstra 算法求出加权图上两点的最短路径。
我开发了一个算法,但我不知道它的效率,但它运行良好。
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}