在开放街道地图项目的序言中根据数据库中的事实创建谓词

发布于 2024-10-04 14:18:05 字数 2297 浏览 1 评论 0原文

我从开放街道地图项目下载了一些事实,您可以在这里下载 http://www.mediafire.com/?15pttpp847ld71x 我试图提出的这个程序将帮助用户获得从一个地方到另一个地方的行程,给出可能的最短路线,有人可以告诉我如何实现 Dijkstra 算法来搜索路径,我也想到了这个谓词 -compute_path(User, Start 、End、PathNodes),其中 User 将与 amsterdam.pl 中的用户值一致 我正在尝试添加扩展,也许你可以使用它,例如以下内容: .告诉Prolog我是什么类型的用户(例如行人、骑自行车的人、汽车司机……)。然后 Prolog 在构建适当的路由时会考虑此信息。例如,骑自行车的人不能使用高速公路。 · 可以请求明确访问多个用户指定地点的出发地和到达地址之间的行程(即用户可以指定他想要从 A 经 B 到达 C)。 · 可以向 Prolog 询问诸如“我必须在上午 10:00 几点离开 A 点才能到达阿姆斯特丹 B 点?”之类的信息。 · 使用像您刚刚制作的那样的人类语言界面,以便用户可以使用以下命令与 shell 进行交互 输入如下: o 如何从 阿姆斯特丹“NameA”前往阿姆斯特丹“NameB” 如果您能够实现这一点,请回复我,我将非常感激,我是 Prolog 新手,并试图成为一名快速学习者。

这是我试图想出的代码

:-dynamic(node/3).
:-dynamic(way/2).

% some nodes
node(46315543, 52.35548, 4.84315).
node(46315968, 52.35558, 4.84068).
node(46315971, 52.35531, 4.84986).

% predicate to add a node to a way
add_node_to_way(WayID, NodeID) :-
    way(WayID, NodeList),
    node(NodeID, _, _),
    not(member(NodeID, NodeList)),
    retract(way(WayID, NodeList)),
    append(NodeList, [NodeID], NewNodeList),
    assert(way(WayID, NewNodeList)).

% main menu
menu :-
    write('1. list nodes\n'),
    write('2. list ways\n'),
    write('3. create node\n'),
    write('4. create way\n'),
    write('5. add node to way\n'),
    write('6. exit\n'),
    nl,
    write('your option: '),
    read(Option),
    process(Option).
menu :-
    menu.

process(1) :-
    node(ID, Lat, Long),
    writef('node with ID = %d, lat = %d and long = %d\n', [ID, Lat, Long]),
    fail.

process(2) :-
    way(ID, NodeList),
    writef('way with ID = %d and nodelist = ', [ID, NodeList]),
    write(NodeList),
    nl,
    fail.

process(3) :-
    write('enter node ID: '),
    read(ID),
    not(node(ID, _, _)),
    write('enter lat: '),
    read(Lat),
    write('enter long: '),
    read(Long),
    assert(node(ID, Lat, Long)),
    fail.

process(4) :-
    write('enter way ID: '),
    read(ID),
    not(way(ID, _)),
    assert(way(ID, [])),
    fail.

process(5) :-
    write('enter ID of node to add: '),
    read(NodeID),
    node(NodeID, _, _),
    write('enter ID of way to add to: '),
    read(WayID),
    way(WayID, _),
    add_node_to_way(WayID, NodeID),
    fail.

process(6) :-
    % exit point
    write('bye'). 

i downloaded some facts from open streetmap project, you can download it here http://www.mediafire.com/?15pttpp847ld71x
This program am trying to come up with will help a user get itinerary from one place to another giving shortest routes possible, someone can tell me how to implement Dijkstra's algorithm to search paths, also i had this predicate in mind -compute_path(User, Start, End, PathNodes) where User will be consistent with the user values from amsterdam.pl
Am trying to add extensions, maybe u can play with it, e.g the following:
.Tell Prolog what kind of user i am (e.g. pedestrian, cyclist,car driver, ...). then Prolog take this information into account when constructing an appropriate route. For example, a cyclist cannot use a highway.
· Make it possible to ask for an itinerary between a departure and an arrival address which explicitly visits a number of user-specified places (i.e. the user can specify that he wants to go from A to C via B).
· Make it possible to ask the Prolog for information such as "At what time do I have to leave Point A,to get to Point B,Amsterdam at 10:00AM?".
· Use human language interface like the one u just made such that the user can interact with the shell using
input like:
o how do I get from "NameA", Amsterdam to "NameB", Amsterdam
kindly get back to me if u were able to implement this, i will appreciate so much, am new in Prolog and trying to be a fast learner.

this is the code i have tried to come up with

:-dynamic(node/3).
:-dynamic(way/2).

% some nodes
node(46315543, 52.35548, 4.84315).
node(46315968, 52.35558, 4.84068).
node(46315971, 52.35531, 4.84986).

% predicate to add a node to a way
add_node_to_way(WayID, NodeID) :-
    way(WayID, NodeList),
    node(NodeID, _, _),
    not(member(NodeID, NodeList)),
    retract(way(WayID, NodeList)),
    append(NodeList, [NodeID], NewNodeList),
    assert(way(WayID, NewNodeList)).

% main menu
menu :-
    write('1. list nodes\n'),
    write('2. list ways\n'),
    write('3. create node\n'),
    write('4. create way\n'),
    write('5. add node to way\n'),
    write('6. exit\n'),
    nl,
    write('your option: '),
    read(Option),
    process(Option).
menu :-
    menu.

process(1) :-
    node(ID, Lat, Long),
    writef('node with ID = %d, lat = %d and long = %d\n', [ID, Lat, Long]),
    fail.

process(2) :-
    way(ID, NodeList),
    writef('way with ID = %d and nodelist = ', [ID, NodeList]),
    write(NodeList),
    nl,
    fail.

process(3) :-
    write('enter node ID: '),
    read(ID),
    not(node(ID, _, _)),
    write('enter lat: '),
    read(Lat),
    write('enter long: '),
    read(Long),
    assert(node(ID, Lat, Long)),
    fail.

process(4) :-
    write('enter way ID: '),
    read(ID),
    not(way(ID, _)),
    assert(way(ID, [])),
    fail.

process(5) :-
    write('enter ID of node to add: '),
    read(NodeID),
    node(NodeID, _, _),
    write('enter ID of way to add to: '),
    read(WayID),
    way(WayID, _),
    add_node_to_way(WayID, NodeID),
    fail.

process(6) :-
    % exit point
    write('bye'). 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2024-10-11 14:18:05

几年前,我正在做类似的所谓 A* 搜索。与 Dijkstra 算法相比,此搜索更适合平面非迷宫类问题。 A* 搜索向 Dijkstra 算法添加当前节点到目标节点的距离估计器,并将其与已存档的最小距离相结合。

当主要道路与较小的道路相比具有不同的权重时,可以获得非常好的结果。因此算法首先搜索主要道路,只有在接近目标或起点时才转向较小的道路。一本非常好的开发 A* 算法的书如下:

Nilsson, NJ (1980)。人工智能原理。
加利福尼亚州帕洛阿尔托:Tioga 出版公司。

此致

Some years ago I was doing something similar with the so called A* search. This search is better suited for planar non-maze like problems than Dijkstra's algorithm. A* search adds to Dijkstra's algorithm a current node to goal node distance estimator and combines it with the already archived minimal distance.

A very good result can be obtained when main roads are differently weighted compared to smaller roads. So that the algorithm first search the main roads, and only diverts into smaller roads when close to the goal or start. A very nice book which develops the A* algorithm is the following:

Nilsson, N. J. (1980). Principles of Artificial Intelligence.
Palo Alto, California: Tioga Publishing Company.

Best Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文