“罗马尼亚地图”问题有没有计算启发式的公式?

2024-04-28 03:11:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习用Python搜索,我现在的问题是众所周知的“罗马尼亚地图”。这个问题的目标是使用A*搜索从阿拉德到布加勒斯特。 This is what the graph looks like, with straight-line distance to the goal.我试图用python实现这一点,但我不知道启发式(在本例中是位置)是如何计算的。这就是“main”代码的外观和启发式。有人能解释一下romania.locations是怎么计算出来的吗?代码取自here

def h(self, node):
        "h function is straight-line distance from a node's state to goal."
        locs = getattr(self.graph, 'locations', None)
        if locs:
            return int(distance(locs[node.state], locs[self.goal]))
        else:
            return infinity
.
.
.
romania = UndirectedGraph(Dict(
    A=Dict(Z=75, S=140, T=118),
    B=Dict(U=85, P=101, G=90, F=211),
    C=Dict(D=120, R=146, P=138),
    D=Dict(M=75),
    E=Dict(H=86),
    F=Dict(S=99),
    H=Dict(U=98),
    I=Dict(V=92, N=87),
    L=Dict(T=111, M=70),
    O=Dict(Z=71, S=151),
    P=Dict(R=97),
    R=Dict(S=80),
    U=Dict(V=142)))
romania.locations = Dict(
    A=( 91, 492),    B=(400, 327),    C=(253, 288),   D=(165, 299),
    E=(562, 293),    F=(305, 449),    G=(375, 270),   H=(534, 350),
    I=(473, 506),    L=(165, 379),    M=(168, 339),   N=(406, 537),
    O=(131, 571),    P=(320, 368),    R=(233, 410),   S=(207, 457),
    T=( 94, 410),    U=(456, 350),    V=(509, 444),   Z=(108, 531))```

Tags: theto代码selfnodeislinedict