diff --git a/logic_programming/9_AStar_Search.ipynb b/logic_programming/9_AStar_Search.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..f1d09d42054ae6ea21d5923d0b11387157bd0549 --- /dev/null +++ b/logic_programming/9_AStar_Search.ipynb @@ -0,0 +1,441 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "9eb5530a", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "source": [ + "Empty Prolog Notebook" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "40ad6905", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [], + "source": [ + ":- consult('prolog_files/roumania.pl')." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "9c7e889c", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[1mDist = 75,\n", + "City = zerind" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "?- s(arad,Dist,City)." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "af6a9e4a", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/markdown": [ + "Dist | City | \n", + ":- | :- | \n", + "75 | zerind | \n", + "118 | timisoara | \n", + "140 | sibiu | " + ], + "text/plain": [ + "Dist | City | \n", + ":- | :- | \n", + "75 | zerind | \n", + "118 | timisoara | \n", + "140 | sibiu | " + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "\u001b[1mtrue" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "jupyter:print_table(s(arad,Dist,City))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "55da475c", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/markdown": [ + "City | Dist | \n", + ":- | :- | \n", + "zerind | 75 | \n", + "timisoara | 118 | \n", + "sibiu | 140 | " + ], + "text/plain": [ + "City | Dist | \n", + ":- | :- | \n", + "zerind | 75 | \n", + "timisoara | 118 | \n", + "sibiu | 140 | " + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "\u001b[1mtrue" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "jupyter:print_table(wkante(City,Dist,arad))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c32206c4", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[1mSLDZ = 374,\n", + "SLDT = 329,\n", + "SLDS = 253" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "?- h(zerind,SLDZ), h(timisoara,SLDT), h(sibiu,SLDS)." + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "3e904005", + "metadata": {}, + "source": [ + "The following is the evaluation function we will use in the A* algorithm:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "e942f0d4", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [], + "source": [ + "eval_function(G_SoFar,H_EstDist,F) :- F is G_SoFar+H_EstDist. % A*\n", + "%eval_function(_G_SoFar,H_EstDist,F) :- F =H_EstDist. % Greedy\n", + "%eval_function(G_SoFar,_,F) :- F=G_SoFar. % Dijkstra" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "242f4141", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[1mHZerind = 449,\n", + "HSibiu = 393,\n", + "HTimisoara = 447" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "?- eval_function(75,374,HZerind), eval_function(140,253,HSibiu),\n", + " eval_function(118,329,HTimisoara)." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "78764141", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [], + "source": [ + "\n", + ":- use_module(library(heaps)).\n", + "\n", + "add_states_to_heap([],H,H).\n", + "add_states_to_heap([st(G,State)|T],Heap,UpdHeap) :-\n", + " (h(State,SH) -> true ; print(err(State)),nl,SH=1000), % compute heuristic function\n", + " eval_function(G,SH,F), % Compute weight\n", + " add_to_heap(Heap,F,st(G,State),H2),\n", + " add_states_to_heap(T,H2,UpdHeap)." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "b03ac6e7", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + " A* Heap = [393-st(140,sibiu),447-st(118,timisoara),449-st(75,zerind)]" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "\u001b[1mEH = heap(nil,0),\n", + "NewHeap = heap(t(st(140,sibiu),393,[t(st(118,timisoara),447,[]),t(st(75,zerind),449,[])]),3),\n", + "List = [393-st(140,sibiu),447-st(118,timisoara),449-st(75,zerind)]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "?- empty_heap(EH), add_states_to_heap([st(75,zerind),st(140,sibiu),st(118,timisoara)],EH,NewHeap),\n", + " heap_to_list(NewHeap,List), format(' A* Heap = ~w~n',[List])." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "f6f156a3", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [], + "source": [ + "astar :- statistics(runtime,_),start(S), \n", + " astar(S), \n", + " statistics(runtime,[_,T]), print(T),print(' ms'),nl.\n", + "\n", + "astar(S) :- empty_heap(EH),\n", + " add_states_to_heap([st(0,S)],EH,StartHeap),\n", + " nl,print('Starting A* search'),nl, \n", + " asearch(StartHeap,1).\n", + "\n", + "asearch(Heap,StepNr) :- \n", + " heap_to_list(Heap,List), format(' ~w: A* Heap = ~w~n',[StepNr,List]),\n", + " get_from_heap(Heap,_Heuristic,st(G,State),NewHeap),\n", + " %print(StepNr),print(' ==> '), print(st(G,State)),nl,\n", + " process_node(G,State,NewHeap,StepNr).\n", + "\n", + "% process_node(DistanceFromStart,State, Heap, StepNr)\n", + "process_node(G,State,_,StepNr) :-\n", + " ziel(State),\n", + " format('GOAL found at step ~w with distance ~w : ~w~n',[StepNr,G,State]).\n", + "process_node(G,State,Heap,StepNr) :-\n", + " findall(st(G1,SuccState),\n", + "\t (wkante(State,Weight,SuccState),G1 is G+Weight),SList),\n", + "\t% print(succs(SList)),nl,\n", + " add_states_to_heap(SList,Heap,UpdatedHeap),\n", + " S1 is StepNr+1,\n", + " asearch(UpdatedHeap,S1)." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "9639129f", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\n", + "'Starting A* search'\n", + " 1: A* Heap = [366-st(0,arad)]\n", + " 2: A* Heap = [393-st(140,sibiu),447-st(118,timisoara),449-st(75,zerind)]\n", + " 3: A* Heap = [413-st(220,rimnicu_vilcea),417-st(239,fagaras),447-st(118,timisoara),449-st(75,zerind),646-st(280,arad),671-st(291,oradea)]\n", + " 4: A* Heap = [415-st(317,pitesti),417-st(239,fagaras),447-st(118,timisoara),449-st(75,zerind),526-st(366,craiova),553-st(300,sibiu),646-st(280,arad),671-st(291,oradea)]\n", + " 5: A* Heap = [417-st(239,fagaras),418-st(418,bucharest),447-st(118,timisoara),449-st(75,zerind),526-st(366,craiova),553-st(300,sibiu),607-st(414,rimnicu_vilcea),615-st(455,craiova),646-st(280,arad),671-st(291,oradea)]\n", + " 6: A* Heap = [418-st(418,bucharest),447-st(118,timisoara),449-st(75,zerind),450-st(450,bucharest),526-st(366,craiova),553-st(300,sibiu),591-st(338,sibiu),607-st(414,rimnicu_vilcea),615-st(455,craiova),646-st(280,arad),671-st(291,oradea)]\n", + "GOAL found at step 6 with distance 418 : bucharest\n", + "0' ms'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "\u001b[1mtrue" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "?-astar." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "943a1e1c", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [], + "source": [ + "eval_function(_G_SoFar,H_EstDist,F) :- F =H_EstDist. % Greedy" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "dab9377c", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\n", + "'Starting A* search'\n", + " 1: A* Heap = [366-st(0,arad)]\n", + " 2: A* Heap = [253-st(140,sibiu),329-st(118,timisoara),374-st(75,zerind)]\n", + " 3: A* Heap = [178-st(239,fagaras),193-st(220,rimnicu_vilcea),329-st(118,timisoara),366-st(280,arad),374-st(75,zerind),380-st(291,oradea)]\n", + " 4: A* Heap = [0-st(450,bucharest),193-st(220,rimnicu_vilcea),253-st(338,sibiu),329-st(118,timisoara),366-st(280,arad),374-st(75,zerind),380-st(291,oradea)]\n", + "GOAL found at step 4 with distance 450 : bucharest\n", + "0' ms'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "\u001b[1mtrue" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "?-astar." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad76945b", + "metadata": { + "vscode": { + "languageId": "prolog" + } + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Prolog", + "language": "prolog", + "name": "prolog_kernel" + }, + "language_info": { + "codemirror_mode": "prolog", + "file_extension": ".pl", + "mimetype": "text/x-prolog", + "name": "Prolog" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}