如何在Prolog中找到城市之间的距离?

时间:2014-04-26 14:20:19

标签: prolog depth-first-search

如何使用深度优先搜索找到城市之间的可能路线 这是我的代码:

DOMAINS
    s = symbol.
    sList = symbol*.

PREDICATES
    nondeterm link(s, s,integer).
    nondeterm depth_first_search(s, s, sList,integer).

CLAUSES


    link("Erbil","Koysinjaq",12).
    link("Erbil","Kirkuk",15).
    link("Erbil","Shaqlawa",15).
    link("Erbil","Mosul",22).
    link("Shaqlawa","Akre",33).
    link("Mosul","Duhok",44).
    link("Mosul","Akre",55).
    link("Kirkuk","Koysinjaq",66).
    link("Kirkuk","Chamchamal",88).
    link("Chamchamal","Sulaimani",34).
    link("Koysinjaq","Sulaimani",22).
    link("Sulaimani","Ranya",33).
    link("Akre","Duhok",22).

    depth_first_search(X, X, [X],0).
    depth_first_search(X, Y, [X|T],NewDis):-
        link(X, Z , Dis),
        NewDis=NewDis+Dis,
        depth_first_search(Z, Y, T,NewDis).

GOAL
    depth_first_search("Erbil", "Duhok", PathToGoal,Dis).

当我执行时我得到了这个错误:

E;Test_Goal, pos: 652, 707 Free variable in expression

在这里,我希望这些城市之间的所有可能的联系与它们之间的距离 现在该怎么做,谢谢你的帮助

1 个答案:

答案 0 :(得分:3)

看这是你的错误:

depth_first_search(X, X, [X],0).
    depth_first_search(X, Y, [X|T],L):-
        link(X, Z , L1),
        depth_first_search(Z, Y, T,L2),L=L1+L2.

GOAL
    depth_first_search("Erbil", "Duhok", PathToGoal,Dis).

它现在为我工作:)