Skip to content
Snippets Groups Projects
Commit 8f99e088 authored by Michael Leuschel's avatar Michael Leuschel
Browse files

add notebook about A* search

parent 6d085e2f
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id:9eb5530a tags:
Empty Prolog Notebook
%% Cell type:code id:40ad6905 tags:
``` prolog
:- consult('prolog_files/roumania.pl').
```
%% Cell type:code id:9c7e889c tags:
``` prolog
?- s(arad,Dist,City).
```
%% Output
%% Cell type:code id:af6a9e4a tags:
``` prolog
jupyter:print_table(s(arad,Dist,City))
```
%% Output
Dist | City |
:- | :- |
75 | zerind |
118 | timisoara |
140 | sibiu |
%% Cell type:code id:55da475c tags:
``` prolog
jupyter:print_table(wkante(City,Dist,arad))
```
%% Output
City | Dist |
:- | :- |
zerind | 75 |
timisoara | 118 |
sibiu | 140 |
%% Cell type:code id:c32206c4 tags:
``` prolog
?- h(zerind,SLDZ), h(timisoara,SLDT), h(sibiu,SLDS).
```
%% Output
%% Cell type:markdown id:3e904005 tags:
The following is the evaluation function we will use in the A* algorithm:
%% Cell type:code id:e942f0d4 tags:
``` prolog
eval_function(G_SoFar,H_EstDist,F) :- F is G_SoFar+H_EstDist. % A*
%eval_function(_G_SoFar,H_EstDist,F) :- F =H_EstDist. % Greedy
%eval_function(G_SoFar,_,F) :- F=G_SoFar. % Dijkstra
```
%% Cell type:code id:242f4141 tags:
``` prolog
?- eval_function(75,374,HZerind), eval_function(140,253,HSibiu),
eval_function(118,329,HTimisoara).
```
%% Output
%% Cell type:code id:78764141 tags:
``` prolog
:- use_module(library(heaps)).
add_states_to_heap([],H,H).
add_states_to_heap([st(G,State)|T],Heap,UpdHeap) :-
(h(State,SH) -> true ; print(err(State)),nl,SH=1000), % compute heuristic function
eval_function(G,SH,F), % Compute weight
add_to_heap(Heap,F,st(G,State),H2),
add_states_to_heap(T,H2,UpdHeap).
```
%% Cell type:code id:b03ac6e7 tags:
``` prolog
?- empty_heap(EH), add_states_to_heap([st(75,zerind),st(140,sibiu),st(118,timisoara)],EH,NewHeap),
heap_to_list(NewHeap,List), format(' A* Heap = ~w~n',[List]).
```
%% Output
%% Cell type:code id:f6f156a3 tags:
``` prolog
astar :- statistics(runtime,_),start(S),
astar(S),
statistics(runtime,[_,T]), print(T),print(' ms'),nl.
astar(S) :- empty_heap(EH),
add_states_to_heap([st(0,S)],EH,StartHeap),
nl,print('Starting A* search'),nl,
asearch(StartHeap,1).
asearch(Heap,StepNr) :-
heap_to_list(Heap,List), format(' ~w: A* Heap = ~w~n',[StepNr,List]),
get_from_heap(Heap,_Heuristic,st(G,State),NewHeap),
%print(StepNr),print(' ==> '), print(st(G,State)),nl,
process_node(G,State,NewHeap,StepNr).
% process_node(DistanceFromStart,State, Heap, StepNr)
process_node(G,State,_,StepNr) :-
ziel(State),
format('GOAL found at step ~w with distance ~w : ~w~n',[StepNr,G,State]).
process_node(G,State,Heap,StepNr) :-
findall(st(G1,SuccState),
(wkante(State,Weight,SuccState),G1 is G+Weight),SList),
% print(succs(SList)),nl,
add_states_to_heap(SList,Heap,UpdatedHeap),
S1 is StepNr+1,
asearch(UpdatedHeap,S1).
```
%% Cell type:code id:9639129f tags:
``` prolog
?-astar.
```
%% Output
%% Cell type:code id:943a1e1c tags:
``` prolog
eval_function(_G_SoFar,H_EstDist,F) :- F =H_EstDist. % Greedy
```
%% Cell type:code id:dab9377c tags:
``` prolog
?-astar.
```
%% Output
%% Cell type:code id:ad76945b tags:
``` prolog
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment