Advertisement
ezNNP

Laba_Ebanaya

Sep 24th, 2020
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 6.46 KB | None | 0 0
  1. :- dynamic был/1, thread_local.
  2. :- dynamic rpath/2.
  3.  
  4. route(вильнюс, брест, 531).
  5. route(витебск, брест, 638).
  6. route(витебск, вильнюс, 360).
  7. route(воронеж, витебск, 869).
  8. route(воронеж, волгоград, 581).
  9. route(волгоград, витебск, 1455).
  10. route(витебск, нижний_новгород, 911).
  11. route(вильнюс, даугавпилс, 211).
  12. route(калининград, брест, 699).
  13. route(калининград, вильнюс, 333).
  14. route(каунас, вильнюс, 102).
  15. route(киев, вильнюс, 734).
  16. route(киев, житомир, 131).
  17. route(житомир, донецк, 863).
  18. route(житомир, волгоград, 1493).
  19. route(кишинев, киев, 467).
  20. route(кишинев, донецк, 812).
  21. route(санкт_петербург, витебск, 602).
  22. route(санкт_петербург, калининград, 739).
  23. route(санкт_петербург, рига, 641).
  24. route(москва, казань, 815).
  25. route(москва, нижний_новгород, 411).
  26. route(москва, минск, 690).
  27. route(москва, донецк, 1084).
  28. route(москва, санкт_петербург, 664).
  29. route(мурманск, санкт_петербург, 1412).
  30. route(мурманск, минск, 2238).
  31. route(орел, витебск, 522).
  32. route(орел, донецк, 709).
  33. route(орел, москва, 368).
  34. route(одесса, киев, 487).
  35. route(рига, каунас, 267).
  36. route(таллин, рига, 308).
  37. route(харьков, киев, 471).
  38. route(харьков, симферополь, 639).
  39. route(ярославль, воронеж, 739).
  40. route(ярославль, минск, 940).
  41. route(уфа, казань, 525).
  42. route(уфа, самара, 461).
  43.  
  44. road(X, Y, R) :- route(X, Y, R).
  45. road(X, Y, R) :- route(Y, X, R).
  46.  
  47. prepend(L, X, [X | L]).
  48.  
  49. bfs([[Goal | Rest] | _], _, Goal, [Goal | Rest]).
  50.  
  51. bfs([[State | Path] | Queue], Visited, Goal, Solution) :-
  52.     findall(X, road(State, X, _), Next),    
  53.     subtract(Next, Visited, Next1),
  54.     maplist(prepend([State | Path]), Next1, Next2),
  55.     append(Queue, Next2, Queue2),            
  56.     append(Next1, Visited, Visited1),          
  57.     bfs(Queue2, Visited1, Goal, Solution).  
  58.  
  59.  
  60. bfs(Start, Goal, Path) :-
  61.     bfs([[Start]], [Start], Goal, Path1),
  62.     reverse(Path1, Path).
  63.  
  64. bds(Start, Goal, Path) :-
  65.     bds_b([[Start]], [[Goal]], [Start], [Goal], Path).
  66.  
  67. bds_check([[BeginState | BeginPath] | BeginQueue], [[EndState | EndPath] | EndQueue], VisitedBegin, VisitedEnd, Solution) :-
  68.     intersection(VisitedBegin, VisitedEnd, Intersection),
  69.     Intersection \== [],
  70.     road(BeginState, EndState, _),
  71.     flatten(BeginPath, BNPath),
  72.     reverse(BNPath, BPath),
  73.     flatten(EndPath, EPath),
  74.     append(BPath, EPath, Solution).
  75.  
  76. bds_b(BeginQueue, EndQueue, VisitedBegin, VisitedEnd, Solution) :-
  77.     bds_check(BeginQueue, EndQueue, VisitedBegin, VisitedEnd, Solution), !.
  78.  
  79. bds_b([[BeginState | BeginPath] | BeginQueue], EndQueue, VisitedBegin, VisitedEnd, Solution) :-
  80.     findall(X, road(BeginState, X, _), NextBegin),
  81.     subtract(NextBegin, VisitedBegin, Next1Begin),
  82.     maplist(prepend([BeginState, BeginPath]), Next1Begin, Next2Begin),
  83.     append(BeginQueue, Next2Begin, BeginQueue2),
  84.     append(Next1Begin, VisitedBegin, VisitedBegin1),
  85.     bds_e(BeginQueue2, EndQueue, VisitedBegin1, VisitedEnd, Solution).
  86.  
  87. bds_e(BeginQueue, EndQueue, VisitedBegin, VisitedEnd, Solution) :-
  88.     bds_check(BeginQueue, EndQueue, VisitedBegin, VisitedEnd, Solution), !.
  89.  
  90. bds_e(BeginQueue, [[EndState | EndPath] | EndQueue], VisitedBegin, VisitedEnd, Solution) :-
  91.     findall(Y, road(EndState, Y, _), NextEnd),
  92.     subtract(NextEnd, VisitedEnd, Next1End),
  93.     maplist(prepend([EndState, EndPath]), Next1End, Next2End),
  94.     append(EndQueue, Next2End, EndQueue2),
  95.     append(Next1End, VisitedEnd, VisitedEnd1),
  96.     bds_b(BeginQueue, EndQueue2, VisitedBegin, VisitedEnd1, Solution).
  97.    
  98.  
  99.  
  100. dfs(Start, Start, _, [Start]).
  101.  
  102. dfs(Start, Goal, Visited, [Start | Path]) :-
  103.     road(Start, V, _),            
  104.     maplist(dif(V), Visited),  
  105.     dfs(V, Goal, [V | Visited], Path).  
  106.  
  107.  
  108.  
  109. dfs(Start, Goal, Path) :- dfs(Start, Goal, [Start], Path).
  110.  
  111. dls(Start, Start, _, [Start], _, _).
  112.  
  113. dls(Start, Goal, Visited, [Start | Path], Depth, Acc) :-
  114.     Depth1 is Acc + 1,
  115.     Depth1 < Depth,
  116.     road(Start, V, _),            
  117.     maplist(dif(V), Visited),  
  118.     dls(V, Goal, [V | Visited], Path, Depth,  Depth1).  
  119.  
  120.  
  121.  
  122. dls(Start, Goal, Depth, Path) :- dls(Start, Goal, [Start], Path, Depth, 0).
  123.  
  124. eds(Start, Start, _, [Start], Depth, Acc) :-
  125.     Depth == Acc.  
  126.  
  127. eds(Start, Goal, Visited, [Start | Path], Depth, Acc) :-
  128.     Depth1 is Acc + 1,
  129.     Depth1 =< Depth,
  130.     road(Start, V, _),            
  131.     maplist(dif(V), Visited),  
  132.     eds(V, Goal, [V | Visited], Path, Depth,  Depth1).  
  133.  
  134. ids(Start, Goal, Path) :-
  135.     ids(Start, Goal, Path, 1).
  136.  
  137. ids(Start, Goal, Path, Acc) :-
  138.     eds(Start, Goal, [Start], Path, Acc, 0).
  139.  
  140. ids(Start, Goal, Path, Acc) :-
  141.     Acc1 is Acc + 1,
  142.     ids(Start, Goal, Path, Acc1).
  143.  
  144. gcs(Start, Goal) :-
  145.     gcs(Start, Goal, [Start], 0).
  146.  
  147. gcs(Goal, Goal, Path, Cost) :-
  148.     writeln(Path),
  149.     writeln(Cost).
  150.  
  151. gcs(Current, Goal, Path, Cost) :-
  152.     findall(C, (road(Current, X, C), not(member(X, Path))), R),
  153.     R \== [],
  154.     sort(R, RS),
  155.     gcs(Current, Goal, Path, Cost, RS).
  156.  
  157. gcs(Current, Goal, Path, Cost, []) :- fail.
  158.  
  159. gcs(Current, Goal, Path, Cost, [H | T]) :-
  160.     CityCost is H,
  161.     road(Current, City, CityCost),
  162.     append(Path, [City], Path1),
  163.     Cost1 is Cost + CityCost,
  164.     (gcs(City, Goal, Path1, Cost1) ; gcs(Current, Goal, Path, Cost, T)).
  165.  
  166. shorterPath([H|Path], Dist) :-             
  167.     rpath([H|T], D), !, Dist < D,          
  168.     retract(rpath([H|_],_)),
  169.     assert(rpath([H|Path], Dist)).
  170.  
  171. shorterPath(Path, Dist) :-             
  172.     assert(rpath(Path,Dist)).
  173.  
  174. traverse(From, Path, Dist) :-          
  175.     road(From, T, D),          
  176.     not(memberchk(T, Path)),       
  177.     Dist1 is Dist + D,
  178.     shorterPath([T,From|Path], Dist1),
  179.     traverse(T,[From|Path],Dist1).     
  180.  
  181. traverse(From) :-
  182.     retractall(rpath(_,_)),          
  183.     traverse(From,[],0).            
  184.  
  185. traverse(_).
  186.  
  187. make_heuristic(From) :-
  188.     traverse(From).                
  189.  
  190. astar(Start, Goal) :-
  191.     make_heuristic(Goal),
  192.     find_rpath(Start, Goal).
  193.  
  194. find_rpath(Start, Goal) :-
  195.     rpath([Start | T], Cost),
  196.     last(T, Goal),
  197.     writeln([Start | T]),
  198.     writeln(Cost).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement