Поиск маршрута с помощью Best First Search - PullRequest
0 голосов
/ 04 мая 2020

Я должен сделать некоторую работу в Прологе, с которой я не очень знаком. Мне нужно найти путь от одной станции к другой станции лондонского метро со временем в минутах. Мне нужно применить «лучший первый поиск» для достижения цели. Мне нужен старт для этого, так как я никогда раньше не работал в Прологе. Я начал немного, но понятия не имею, как go дальше. Любая помощь будет оценена. Вот мой стартер:

adjacent(waterloo,westminster,jubilee,2).
adjacent(westminster,green_park,jubilee,2).
adjacent(green_park,bond_street,jubilee,2).
adjacent(bond_street,baker_street,jubilee,2).

adjacent(waterloo,embankment,northern,2).
adjacent(embankment,charing_cross,northern,1).
adjacent(charing_cross,leicester_square,northern,1).
adjacent(leicester_square,tottenham_court_road,northern,2).
adjacent(tottenham_court_road,goodge_street,northern,1).
adjacent(goodge_street,warren_street,northern,2).
adjacent(warren_street,euston,northern,1).

adjacent(waterloo,embankment,bakerloo,2).
adjacent(embankment,charing_cross,bakerloo,1).
adjacent(charing_cross,piccadilly_circus,bakerloo,2).
adjacent(piccadilly_circus,oxford_circus,bakerloo,2).
adjacent(oxford_circus,regents_park,bakerloo,2).
adjacent(regents_park,baker_street,bakerloo,2).

go(Start, Goal) :-
empty_set(Closed_set),
empty_pq(Open),
heuristic(Start, Goal, H),
insert_pq([Start, nil, 0, H, H], Open, Open_pq),
path(Open_pq, Closed_set, Goal).

path(Open_pq, _, _) :-
empty_pq(Open_pq),
write('Graph searched; no solution found.').

path(Open_pq, Closed_set, Goal) :-
dequeue_pq([State, Parent, _, _, _], Open_pq, _),
my_equal(State,Goal),
write('The solution path is'), nl,
printsolution([State, Parent, _, _, _], Closed_set).

path(Open_pq, Closed_set, Goal) :-
dequeue_pq([State, Parent, D, H, S], Open_pq, Rest_open_pq),
get_children([State, Parent, D, H, S], Rest_open_pq, Closed_set, Children, Goal),
insert_list_pq(Children, Rest_open_pq, New_open_pq),
my_union([[State, Parent, D, H, S]], Closed_set, New_closed_set),
path(New_open_pq, New_closed_set, Goal),!.

get_children([State, _, D, _, _], Rest_open_pq, Closed_set, Children, Goal) :-
(bagof(Child,
moves([State, _, D, _, _], Rest_open_pq,Closed_set, Child, Goal),
Children);
Children = []).

moves([State, _, Depth, _, _], Rest_open_pq, Closed_set,
[Next, State, New_D, H, S], Goal) :-
move(State, Next),
not(member_pq([Next, _, _, _, _], Rest_open_pq)),
not(member_set2([Next, _, _, _, _], Closed_set, _)),
New_D is Depth + 1,
heuristic(Next, Goal, H),
S is New_D + H.

printsolution([[X, Y, _, _], nil, _, _, _], _) :-
write([X, Y]), nl.

printsolution([State, Parent, _, _, _], Closed_set) :-
member_set2([Parent, _, _, _, _], Closed_set, Grandparent),
printsolution([Parent, Grandparent, _, _, _], Closed_set),
my_write(State).

my_write([X, Y, _, _]) :-
write([X, Y]),nl.

my_equal([X, Y, _, _], [Z, W]) :-
X = Z,
Y = W.



1 Ответ

0 голосов
/ 04 мая 2020

Итеративное углубление позволяет вам делать все возможное, чтобы не попасть в циклы. https://www.swi-prolog.org/pldoc/man?predicate=call_with_depth_limit / 3

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...