pop push pop push push pushX Queue New

  • Slides: 120
Download presentation

СТЕК pop push

СТЕК pop push

ОПАШКА pop push

ОПАШКА pop push

Основни операции Ø push - поставяне на елемент push(X, Queue, New. Queue): append(Queue, [X],

Основни операции Ø push - поставяне на елемент push(X, Queue, New. Queue): append(Queue, [X], New. Queue). Ø pop - вземане на елемент pop(Queue, _, _): empty_queue (Queue), write('Empty queue'), nl. pop([X|Queue], X, Queue). Ø empty_queue - проверка дали дадена опашка е празна empty_queue([ ]).

in_stack(X, Stack): + empty_stack(Stack), !, pop(Stack, Y, Rest. Stack), ( X=Y, ! ; in_stack(X,

in_stack(X, Stack): + empty_stack(Stack), !, pop(Stack, Y, Rest. Stack), ( X=Y, ! ; in_stack(X, Rest. Stack) ).

% Graph - насочен граф arc(a, b). arc(b, c). arc(c, d). arc(d, e). arc(b,

% Graph - насочен граф arc(a, b). arc(b, c). arc(c, d). arc(d, e). arc(b, d). arc(c, e). arc(a, d). arc(c, a).

% Graph - ненасочен граф my_arc(a, b). my_arc(b, c). my_ arc(c, d). my_ arc(d,

% Graph - ненасочен граф my_arc(a, b). my_arc(b, c). my_ arc(c, d). my_ arc(d, e). my_ arc(b, d). my_ arc(c, e). my_ arc(a, d). my_ arc(c, a). arc(X, Y): -my_ arc(Y, X).

% Graph g([ [a, [e, c]], [b, [d, a]], [c, [ ]], [d, [a]],

% Graph g([ [a, [e, c]], [b, [d, a]], [c, [ ]], [d, [a]], [e, [c]]]). arc(X, Y): g(Graph), member([X, Nodes], Graph), member(Y, Nodes).

successor(X, S): findall(Y, arc(X, Y), S). offspring(X, S): successor(X, DS), del_all(X, DS 1), succ_list(DS

successor(X, S): findall(Y, arc(X, Y), S). offspring(X, S): successor(X, DS), del_all(X, DS 1), succ_list(DS 1, S 2), append(DS 1, S 2, Temp), compress(Temp, S).

compress([], []). compress([X|Rest], [X|Result]): del_all(X, Rest, New), compress(New, Result). del_all(_, []). del_all(X, [X|L], L

compress([], []). compress([X|Rest], [X|Result]): del_all(X, Rest, New), compress(New, Result). del_all(_, []). del_all(X, [X|L], L 1): - del_all(X, L, L 1). del_all(X, [Y|L 1], [Y|L 2]): X=Y, del_all(X, L 1, L 2).

diff([], _, []). diff(A, [], A). diff([X|A], B, [X|C]): + member(X, B), diff(A, B,

diff([], _, []). diff(A, [], A). diff([X|A], B, [X|C]): + member(X, B), diff(A, B, C). diff([_|A], B, C): -diff(A, B, C).

succ_list([], []). succ_list([A 1|Rest], S): offspring(A 1, S 1), diff(Rest, S 1, R 1),

succ_list([], []). succ_list([A 1|Rest], S): offspring(A 1, S 1), diff(Rest, S 1, R 1), succ_list(R 1, S 2), append(S 1, S 2, S 3), compress(S 3, S).

% Всички възли в графа nodes(N): setof(X, Y^arc(X, Y), S 1), setof(Y, X^arc(X, Y),

% Всички възли в графа nodes(N): setof(X, Y^arc(X, Y), S 1), setof(Y, X^arc(X, Y), S 2), append(S 1, S 2, S), setof(X, member(X, S), N).

way(X, Y, Path): way 1(X, Y, [ ], Path). way 1(X, X, Current. Path,

way(X, Y, Path): way 1(X, Y, [ ], Path). way 1(X, X, Current. Path, Final. Path): reverse([X|Current. Path], Final. Path). way 1(X, Y, Current. Path, Final. Path): arc(X, Z), + member(Z, Current. Path), way 1(Z, Y, [X|Current. Path], Final. Path).

k_size_ways(X, Y, K, Paths): findall( P, ( way(X, Y, P), length(P, K) ), Paths).

k_size_ways(X, Y, K, Paths): findall( P, ( way(X, Y, P), length(P, K) ), Paths).

hamilton(HPaths): nodes(Nodes), length(Nodes, N), findall( HP, ( member(X, Nodes), member(Y, Nodes), X = Y,

hamilton(HPaths): nodes(Nodes), length(Nodes, N), findall( HP, ( member(X, Nodes), member(Y, Nodes), X = Y, k_size_ways(X, Y, N, KP), member(HP, KP) ), HPaths).

is_hamilton(HPath): nodes(Nodes), length(Nodes, N), length(HPath, N), equal(Nodes, HPath). equal([ ], [ ]). equal([X|A], B):

is_hamilton(HPath): nodes(Nodes), length(Nodes, N), length(HPath, N), equal(Nodes, HPath). equal([ ], [ ]). equal([X|A], B): member(X, B), del(X, B, B 1), equal(A, B 1).

% Graph arc(a, b). arc(b, c). arc(c, d). arc(d, e). arc(b, d). arc(c, e).

% Graph arc(a, b). arc(b, c). arc(c, d). arc(d, e). arc(b, d). arc(c, e). arc(a, d). hamilton(HPaths): nodes(N), length(N, K), findall(P 1, (member(X, N), member(Y, N), X = Y, way(X, Y, [], P 1), length(P 1, K) ), HPaths).

%way(X, Y, Path): % way 1(X, Y, [ ], Path). way 2(X, X, Current.

%way(X, Y, Path): % way 1(X, Y, [ ], Path). way 2(X, X, Current. Path, Final. Path): reverse(Current. Path, Final. Path). % [X-Z, Z-T 1, . . . , Tn-Y] way 2(X, Y, Current. Path, Final. Path): arc(X, Z), + member(X-Z, Current. Path), way 2(Z, Y, [X-Z|Current. Path], Final. Path).

arc(a, b). arc(b, c). arc(c, d). arc(d, e). arc(b, d). arc(c, e). arc(a, d).

arc(a, b). arc(b, c). arc(c, d). arc(d, e). arc(b, d). arc(c, e). arc(a, d). all_arcs(A): findall(X-Y, arc(X, Y), A).

euler(EPaths): all_arcs(Arcs), length(Arcs, N), nodes(Nodes), findall( HP, ( member(X, Nodes), member(Y, Nodes), X =

euler(EPaths): all_arcs(Arcs), length(Arcs, N), nodes(Nodes), findall( HP, ( member(X, Nodes), member(Y, Nodes), X = Y, way 2(X, Y, [ ], HP), length(HP, N) ), EPaths).

frontier start node explored nodes unexplored nodes

frontier start node explored nodes unexplored nodes

Представяне на графа arc(Node 1, Node 2). arc(Node 1, Node 2, Cost). arc(a, b).

Представяне на графа arc(Node 1, Node 2). arc(Node 1, Node 2, Cost). arc(a, b). arc(a, d). arc(b, a). arc(b, c). arc(c, e). arc(d, a). arc(d, b). arc(d, c). arc(d, e). arc(e, c). arc(e, d). b a c e d

Построяване на дървото arc(a, b). arc(a, d). arc(b, a). arc(b, c). arc(c, e). arc(d,

Построяване на дървото arc(a, b). arc(a, d). arc(b, a). arc(b, c). arc(c, e). arc(d, a). arc(d, b). arc(d, c). arc(d, e). arc(e, c). arc(e, d). a Start b c d b e b Goal e e c Goal d c a e Goal c d

Търсене в дълбочина 1 Start Goal 2 3 4 12 5 6 13 7

Търсене в дълбочина 1 Start Goal 2 3 4 12 5 6 13 7 8 14 16 15 10 9 Goal 11

Търсене в дълбочина (Depthfirst) depth_first([Path|Stack], Goal, Final. Path) : extend(Path, New. Paths), append(New. Paths,

Търсене в дълбочина (Depthfirst) depth_first([Path|Stack], Goal, Final. Path) : extend(Path, New. Paths), append(New. Paths, Stack, New. Stack), depth_first(New. Stack, Goal, Final. Path).

Търсене в дълбочина (Depthfirst) depth_first([[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path).

Търсене в дълбочина (Depthfirst) depth_first([[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path).

Разширяване на фронта extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New. Node), +

Разширяване на фронта extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New. Node), + member(New. Node, [Node|Path])), New. Paths).

Търсене в дълбочина (Depthfirst) depth_first([[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). depth_first([Path|Stack], Goal, Final.

Търсене в дълбочина (Depthfirst) depth_first([[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). depth_first([Path|Stack], Goal, Final. Path) : extend(Path, New. Paths), append(New. Paths, Stack, New. Stack), depth_first(New. Stack, Goal, Final. Path). extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New. Node), + member(New. Node, [Node|Path])), New. Paths).

Пример arc('Arad', 'Zerind', 75). arc('Arad', 'Sibiu', 140). arc('Arad', 'Timisoara', 118). arc('Bucharest', 'Fagaras', 211). arc('Bucharest',

Пример arc('Arad', 'Zerind', 75). arc('Arad', 'Sibiu', 140). arc('Arad', 'Timisoara', 118). arc('Bucharest', 'Fagaras', 211). arc('Bucharest', 'Pitesti', 101). arc('Bucharest', 'Giurgiu', 90). arc('Bucharest', 'Urziceni', 85). arc('Craiova', 'Dobreta', 120). arc('Craiova', 'Rimnicu', 146). arc('Craiova', 'Pitesti', 138). arc('Dobreta', 'Mehadia', 75). arc('Dobreta', 'Craiova', 120). arc('Eforie', 'Hirsova', 86). arc('Fagaras', 'Sibiu', 99). arc('Fagaras', 'Bucharest', 211). arc('Giurgiu', 'Bucharest', 90). arc('Hirsova', 'Urziceni', 98). arc('Hirsova', 'Eforie', 86). arc('Iasi', 'Neamt', 87). arc('Iasi', 'Vaslui', 92). arc('Lugoj', 'Timisoara', 111). arc('Lugoj', 'Mehadia', 70). arc('Mehadia', 'Lugoj', 70). arc('Mehadia', 'Dobreta', 75). arc('Neamt', 'Iasi', 87). arc('Oradea', 'Zerind', 71). arc('Oradea', 'Sibiu', 151). arc('Pitesti', 'Rimnicu', 97). arc('Pitesti', 'Craiova', 138). arc('Pitesti', 'Bucharest', 101). arc('Rimnicu', 'Sibiu', 80). arc('Rimnicu', 'Pitesti', 97). arc('Rimnicu', 'Craiova', 146). arc('Sibiu', 'Arad', 140). arc('Sibiu', 'Oradea', 151). arc('Sibiu', 'Fagaras', 99). arc('Sibiu', 'Rimnicu', 80). arc('Timisoara', 'Arad', 118). arc('Timisoara', 'Lugoj', 111). arc('Urziceni', 'Bucharest', 85). arc('Urziceni', 'Hirsova', 98). arc('Urziceni', 'Vaslui', 142). arc('Vaslui', 'Iasi', 92). arc('Vaslui', 'Urziceni', 142). arc('Zerind', 'Arad', 75). arc('Zerind', 'Oradea', 71).

Пример | ? - depth_first([['Arad']], 'Craiova', Path). Path = ['Arad', 'Zerind', 'Oradea', 'Sibiu', 'Fagaras',

Пример | ? - depth_first([['Arad']], 'Craiova', Path). Path = ['Arad', 'Zerind', 'Oradea', 'Sibiu', 'Fagaras', 'Bucharest', 'Pitesti', 'Rimnicu', 'Craiova']? ; Path= ['Arad', 'Zerind', 'Oradea', 'Sibiu', 'Fagaras', 'Bucharest', 'Pitesti', 'Craiova']

Start Goal

Start Goal

Start Goal

Start Goal

Търсене в ширина 1 Start 2 3 4 8 15 23 9 16 24

Търсене в ширина 1 Start 2 3 4 8 15 23 9 16 24 6 5 10 17 25 18 Goal 13 12 11 19 7 20 14 21 22

Търсене в дълбочина (Depthfirst) depth_first([Path|Stack], Goal, Final. Path) : extend(Path, New. Paths), append(New. Paths,

Търсене в дълбочина (Depthfirst) depth_first([Path|Stack], Goal, Final. Path) : extend(Path, New. Paths), append(New. Paths, Stack, New. Stack), depth_first(New. Stack, Goal, Final. Path).

Търсене в ширина (Breadthfirst) breadth_first([Path|Stack], Goal, Final. Path): extend(Path, New. Paths), append(New. Paths, Stack,

Търсене в ширина (Breadthfirst) breadth_first([Path|Stack], Goal, Final. Path): extend(Path, New. Paths), append(New. Paths, Stack, New. Stack), breadth_first(New. Stack, Goal, Final. Path).

Търсене в ширина (Breadthfirst) breadth_first([Path|Queue], Goal, Final. Path): extend(Path, New. Paths), append(New. Paths, Queue,

Търсене в ширина (Breadthfirst) breadth_first([Path|Queue], Goal, Final. Path): extend(Path, New. Paths), append(New. Paths, Queue, New. Queue), breadth_first(New. Queue, Goal, Final. Path).

Търсене в ширина (Breadthfirst) breadth_first([Path|Queue], Goal, Final. Path): extend(Path, New. Paths), append(Queue, New. Paths,

Търсене в ширина (Breadthfirst) breadth_first([Path|Queue], Goal, Final. Path): extend(Path, New. Paths), append(Queue, New. Paths, New. Queue), breadth_first(New. Queue, Goal, Final. Path).

Търсене в ширина (Breadthfirst) breadth_first([[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). breadth_first([Path|Queue], Goal, Final.

Търсене в ширина (Breadthfirst) breadth_first([[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). breadth_first([Path|Queue], Goal, Final. Path) : extend(Path, New. Paths), append(Queue, New. Paths, New. Queue), breadth_first(New. Queue, Goal, Final. Path). extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New. Node, _), + member(New. Node, [Node|Path])), New. Paths).

Пример ? - breadth_first([['Arad']], 'Craiova', Path). Path =['Arad', 'Sibiu', 'Rimnicu', 'Craiova']

Пример ? - breadth_first([['Arad']], 'Craiova', Path). Path =['Arad', 'Sibiu', 'Rimnicu', 'Craiova']

Start Goal

Start Goal

Start Goal

Start Goal

Търсене в дълбочина (Depthfirst) depth_first([Path|Stack], Goal, Final. Path) : extend(Path, New. Paths), append(New. Paths,

Търсене в дълбочина (Depthfirst) depth_first([Path|Stack], Goal, Final. Path) : extend(Path, New. Paths), append(New. Paths, Stack, New. Stack), depth_first(New. Stack, Goal, Final. Path).

Търсене в ограничена дълбочина (Depth-bound) depth_bound(Depth, [Path|Stack], Goal, Final. Path): extend(Path, New. Paths), append(New.

Търсене в ограничена дълбочина (Depth-bound) depth_bound(Depth, [Path|Stack], Goal, Final. Path): extend(Path, New. Paths), append(New. Paths, Stack, New. Stack), depth_bound(Depth, New. Stack, Goal, Final. Path).

Търсене в ограничена дълбочина (Depth-bound) depth_bound(Depth, [Path|Stack], Goal, Final. Path): - extend_bound(Depth, Path, New.

Търсене в ограничена дълбочина (Depth-bound) depth_bound(Depth, [Path|Stack], Goal, Final. Path): - extend_bound(Depth, Path, New. Paths), append(New. Paths, Stack, New. Stack), depth_bound(Depth, New. Stack, Goal, Final. Path).

Търсене в ограничена дълбочина (Depth-bound) extend_bound(Depth, Path, New. Paths) : length(Path, Len), Len <

Търсене в ограничена дълбочина (Depth-bound) extend_bound(Depth, Path, New. Paths) : length(Path, Len), Len < Depth, !, extend(Path, New. Paths). extend_bound(_, _, []). extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New. Node, _), + member(New. Node, [Node|Path])), New. Paths).

Търсене в ограничена дълбочина (Depth-bound) depth_bound(Depth, [[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). depth_bound(Depth,

Търсене в ограничена дълбочина (Depth-bound) depth_bound(Depth, [[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). depth_bound(Depth, [Path|Stack], Goal, Final. Path): extend_bound(Depth, Path, New. Paths), append(New. Paths, Stack, New. Stack), depth_bound(Depth, New. Stack, Goal, Final. Path). extend_bound(Depth, Path, New. Paths) : length(Path, Len), Len < Depth, !, extend(Path, New. Paths). extend_bound(_, _, []).

Пример |? - depth_bound(5, [['Arad']], 'Craiova', Path). Path=['Arad', 'Sibiu', 'Rimnicu', 'Pitesti', 'Craiova'] ? ;

Пример |? - depth_bound(5, [['Arad']], 'Craiova', Path). Path=['Arad', 'Sibiu', 'Rimnicu', 'Pitesti', 'Craiova'] ? ; Path=['Arad', 'Sibiu', 'Rimnicu', 'Craiova'] ? ; no

Start Goal

Start Goal

Start Goal

Start Goal

Итеративно търсене по нива Depth=4 1 1 Depth=2 2 2 3 4 5 4

Итеративно търсене по нива Depth=4 1 1 Depth=2 2 2 3 4 5 4 9 3 1 Depth=3 3 2 6 7 5 6 7 10 8 13 11 12 14 15

Итеративно търсене по нива (Iterative deepening) iterative_deepening 1(Depth, Stack, Goal, Path, Max): nl, write(depth=Depth),

Итеративно търсене по нива (Iterative deepening) iterative_deepening 1(Depth, Stack, Goal, Path, Max): nl, write(depth=Depth), depth_bound(Depth, Stack, Goal, Path). iterative_deepening 1(Depth, Stack, Goal, Path, Max): Depth 1 is Depth+1, Depth 1 =< Max, !, iterative_deepening 1(Depth 1, Stack, Goal, Path, Max).

Итеративно търсене по нива (Iterative deepening) iterative_deepening(Stack, Goal, Path) : findall(arc(X, Y, Z), Graph),

Итеративно търсене по нива (Iterative deepening) iterative_deepening(Stack, Goal, Path) : findall(arc(X, Y, Z), Graph), length(Graph, N), iterative_deepening 1(1, Stack, Goal, Path, N). iterative_deepening 1(Depth, Stack, Goal, Path, Max) : nl, write(depth=Depth), depth_bound(Depth, Stack, Goal, Path). iterative_deepening 1(Depth, Stack, Goal, Path, Max) : Depth 1 is Depth+1, Depth 1 =< Max, !, iterative_deepening 1(Depth 1, Stack, Goal, Path, Max).

Пример |? - iterative_deepening([['Arad']], 'Craiova', Path). depth=1 depth=2 depth=3 depth=4 Path = ['Arad', 'Sibiu',

Пример |? - iterative_deepening([['Arad']], 'Craiova', Path). depth=1 depth=2 depth=3 depth=4 Path = ['Arad', 'Sibiu', 'Rimnicu', 'Craiova'] ?

Start Goal

Start Goal

frontier h start node explored nodes unexplored nodes

frontier h start node explored nodes unexplored nodes

Метод на най-доброто спускане (Best-first search) best_first([[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). best_first([Path|List],

Метод на най-доброто спускане (Best-first search) best_first([[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). best_first([Path|List], Goal, Final. Path): extend(Path, New. Paths), append(List, New. Paths, New. List), sort(New. List, Goal, Sorted. List), best_first(Sorted. List, Goal, Final. Path).

Метод на най-доброто спускане (Best-first search) extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node,

Метод на най-доброто спускане (Best-first search) extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New. Node, _), not member(New. Node, [Node|Path])), New. Paths). reverse([], []). reverse([X|Rest], List): reverse(Rest, New. Rest), append(New. Rest, [X], List).

Метод на най-доброто спускане (Best-first search) sort([], Goal, []). sort(List, Goal, [Min. Path|Sorted. List]):

Метод на най-доброто спускане (Best-first search) sort([], Goal, []). sort(List, Goal, [Min. Path|Sorted. List]): min(List, Goal, Min. Path, New. List), sort(New. List, Goal, Sorted. List). min([Path 1|List], Goal, Path 2, [Path 1|New. List]): min(List, Goal, Path 2, New. List), h(Path 1, Goal, H 1), h(Path 2, Goal, H 2), H 2<H 1, !. min([Path|List], Goal, Path, List).

Пример arc('Arad', 'Zerind', 75). arc('Arad', 'Sibiu', 140). arc('Arad', 'Timisoara', 118). arc('Bucharest', 'Fagaras', 211). arc('Bucharest',

Пример arc('Arad', 'Zerind', 75). arc('Arad', 'Sibiu', 140). arc('Arad', 'Timisoara', 118). arc('Bucharest', 'Fagaras', 211). arc('Bucharest', 'Pitesti', 101). arc('Bucharest', 'Giurgiu', 90). arc('Bucharest', 'Urziceni', 85). arc('Craiova', 'Dobreta', 120). arc('Craiova', 'Rimnicu', 146). arc('Craiova', 'Pitesti', 138). arc('Dobreta', 'Mehadia', 75). arc('Dobreta', 'Craiova', 120). arc('Eforie', 'Hirsova', 86). arc('Fagaras', 'Sibiu', 99). arc('Fagaras', 'Bucharest', 211). arc('Giurgiu', 'Bucharest', 90). arc('Hirsova', 'Urziceni', 98). arc('Hirsova', 'Eforie', 86). arc('Iasi', 'Neamt', 87). arc('Iasi', 'Vaslui', 92). arc('Lugoj', 'Timisoara', 111). arc('Lugoj', 'Mehadia', 70). arc('Mehadia', 'Lugoj', 70). arc('Mehadia', 'Dobreta', 75). arc('Neamt', 'Iasi', 87). arc('Oradea', 'Zerind', 71). arc('Oradea', 'Sibiu', 151). arc('Pitesti', 'Rimnicu', 97). arc('Pitesti', 'Craiova', 138). arc('Pitesti', 'Bucharest', 101). arc('Rimnicu', 'Sibiu', 80). arc('Rimnicu', 'Pitesti', 97). arc('Rimnicu', 'Craiova', 146). arc('Sibiu', 'Arad', 140). arc('Sibiu', 'Oradea', 151). arc('Sibiu', 'Fagaras', 99). arc('Sibiu', 'Rimnicu', 80). arc('Timisoara', 'Arad', 118). arc('Timisoara', 'Lugoj', 111). arc('Urziceni', 'Bucharest', 85). arc('Urziceni', 'Hirsova', 98). arc('Urziceni', 'Vaslui', 142). arc('Vaslui', 'Iasi', 92). arc('Vaslui', 'Urziceni', 142). arc('Zerind', 'Arad', 75). arc('Zerind', 'Oradea', 71).

Пример ? - best_first([['Arad']], 'Bucharest', Path). Path = ['Arad', 'Sibiu', 'Fagaras', 'Bucharest']

Пример ? - best_first([['Arad']], 'Bucharest', Path). Path = ['Arad', 'Sibiu', 'Fagaras', 'Bucharest']

Start Goal

Start Goal

Start Goal

Start Goal

Оптималният (най-краткият) път между двата града (Arad и Bucharest) минава през Sibiu, Rimnicu и

Оптималният (най-краткият) път между двата града (Arad и Bucharest) минава през Sibiu, Rimnicu и Pitesti. Neamt Oradea Zerind 71 87 75 151 Arad Оптималният път е (140+80+97+101) = 418 miles 140 118 Sibiu Timisoara 99 Lugoj Faragas Mehadia 75 Dobreta 120 142 211 Rimnicu 70 97 Urziceni 98 Hirsova 86 Pitesti 146 92 Vaslui 80 111 Iasi 101 Bucharest 138 86 90 Craiova Giurgui Eforie

Търсене в лъч (Beam search) beam_search(Beam, [[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). beam_search(Beam,

Търсене в лъч (Beam search) beam_search(Beam, [[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). beam_search(Beam, [Path|List], Goal, Final. Path): extend(Path, New. Paths), append(List, New. Paths, New. List), sort(New. List, Goal, Sorted. List), trim(Beam, Sorted. List, Trimed. List), beam_search(Beam, Trimed. List, Goal, Final. Path).

Търсене в лъч (Beam search) trim(N, [X|Rest], [X|Trim]): N>0, M is N-1, trim(M, Rest,

Търсене в лъч (Beam search) trim(N, [X|Rest], [X|Trim]): N>0, M is N-1, trim(M, Rest, Trim). trim(_, _, []).

Търсене в лъч (Beam search) extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New.

Търсене в лъч (Beam search) extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New. Node, _), not member(New. Node, [Node|Path])), New. Paths). reverse([], []). reverse([X|Rest], List): reverse(Rest, New. Rest), append(New. Rest, [X], List).

Търсене в лъч (Beam search) sort([], Goal, []). sort(List, Goal, [Min. Path|Sorted. List]): min(List,

Търсене в лъч (Beam search) sort([], Goal, []). sort(List, Goal, [Min. Path|Sorted. List]): min(List, Goal, Min. Path, New. List), sort(New. List, Goal, Sorted. List). min([Path 1|List], Goal, Path 2, [Path 1|New. List]): min(List, Goal, Path 2, New. List), h(Path 1, Goal, H 1), h(Path 2, Goal, H 2), H 2<H 1, !. min([Path|List], Goal, Path, List).

Метод на изкачването (Hill Climbing) - 1 hill_climbing(List, Goal, Final. Path): beam_search(1, List, Goal,

Метод на изкачването (Hill Climbing) - 1 hill_climbing(List, Goal, Final. Path): beam_search(1, List, Goal, Final. Path).

Метод на изкачването (Hill Climbing) - 2 hill_climbing([Goal|Path], Goal, Final. Path): reverse([Goal|Path], Final. Path).

Метод на изкачването (Hill Climbing) - 2 hill_climbing([Goal|Path], Goal, Final. Path): reverse([Goal|Path], Final. Path). hill_climbing(Path, Goal, Final. Path): extend(Path, New. Paths), best_path(New. Paths, Goal, Best. Path), hill_climbing(Best. Path, Goal, Final. Path).

Метод на изкачването (Hill Climbing) - 2 extend([Node|Path], New. Paths) : findall([New. Node, Node|Path],

Метод на изкачването (Hill Climbing) - 2 extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New. Node, _), not member(New. Node, [Node|Path])), New. Paths). reverse([], []). reverse([X|Rest], List): reverse(Rest, New. Rest), append(New. Rest, [X], List).

Метод на изкачването (Hill Climbing) - 2 best_path([Path 1|List], Goal, Path 2): best_path(List, Goal,

Метод на изкачването (Hill Climbing) - 2 best_path([Path 1|List], Goal, Path 2): best_path(List, Goal, Path 2), h(Path 1, Goal, H 1), h(Path 2, Goal, H 2), H 2<H 1, !. best_path([Path|List], Goal, Path).

g frontier h start node explored nodes f = g+h unexplored nodes

g frontier h start node explored nodes f = g+h unexplored nodes

A* a_star([[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). a_star([Path|List], Goal, Final. Path) : extend(Path,

A* a_star([[Goal|Path]|_], Goal, Final. Path): reverse([Goal|Path], Final. Path). a_star([Path|List], Goal, Final. Path) : extend(Path, New. Paths), append(List, New. Paths, New. List), sort(New. List, Goal, Sorted. List), a_star(Sorted. List, Goal, Final. Path).

A* sort([], Goal, []). sort(List, Goal, [Min. Path|Sorted. List]): min(List, Goal, Min. Path, New.

A* sort([], Goal, []). sort(List, Goal, [Min. Path|Sorted. List]): min(List, Goal, Min. Path, New. List), sort(New. List, Goal, Sorted. List). min([Path 1|List], Goal, Path 2, [Path 1|New. List]): min(List, Goal, Path 2, New. List), f(Path 1, Goal, F 1), f(Path 2, Goal, F 2), F 2<F 1, !. min([Path|List], Goal, Path, List).

A* h([Node|Path], Goal, H) : stright_line_distance(Node, Goal, H). f(Path, Goal, F) : reverse_path_cost(Path, G),

A* h([Node|Path], Goal, H) : stright_line_distance(Node, Goal, H). f(Path, Goal, F) : reverse_path_cost(Path, G), h(Path, Goal, H), F is G+H. reverse_path_cost([A, B], Cost) : arc(B, A, Cost). reverse_path_cost([A, B|Path], Cost) : arc(B, A, Cost 1), reverse_path_cost([B|Path], Cost 2), Cost is Cost 1+Cost 2.

A* extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New. Node, _), not member(New.

A* extend([Node|Path], New. Paths) : findall([New. Node, Node|Path], (arc(Node, New. Node, _), not member(New. Node, [Node|Path])), New. Paths). reverse([], []). reverse([X|Rest], List): reverse(Rest, New. Rest), append(New. Rest, [X], List).

Оптималният (най-краткият) път между двата града (Arad и Bucharest) минава през Sibiu, Rimnicu и

Оптималният (най-краткият) път между двата града (Arad и Bucharest) минава през Sibiu, Rimnicu и Pitesti. Neamt Oradea Zerind 71 87 75 151 Arad Оптималният път е (140+80+97+101) = 418 miles 140 118 Sibiu Timisoara 99 Lugoj Faragas Mehadia 75 Dobreta 120 142 211 Rimnicu 70 97 Urziceni 98 Hirsova 86 Pitesti 146 92 Vaslui 80 111 Iasi 101 Bucharest 138 86 90 Craiova Giurgui Eforie

Началното състояние е Arad. Цената за достигане до Arad от Arad (стойността на g)

Началното състояние е Arad. Цената за достигане до Arad от Arad (стойността на g) е 0 miles. Разстоянието по права линия от Arad до Bucharest (стойността на h ) е 366 miles. Общата оценка е ( f = g + h ) 366 miles. Zerind Oradea F= 75 + 374 F= 449 F= 291 + 380 F= 671 F= 0 + 366 Arad F= 366 Fagaras Sibiu F= 239 + 178 F= 417 F= 140 + 253 F= 118 + 329 F= 447 Timisoara F= 220 + 193 F= 413 Bucharest(2) F= 393 F= 450 + 0 Rimnicu F= 450 Bucharest F= 418 + 0 Pitesti Craiova F= 366 + 160 F= 526 F= 317 + 98 F= 415 F= 418

8 -puzzle Y 3 1 2 Value / X / Y 3 2 4

8 -puzzle Y 3 1 2 Value / X / Y 3 2 4 5 6 1 7 8 nil 1 2 3 [ 1/1/3, 2/2/3, 3/3/3, 4/1/2, 5/2/2, 6/3/2, 7/1/1, 8/2/1, nil/3/1 ] X

8 -puzzle Y Manhattan Distance 3 1 2 3 2 4 5 6 1

8 -puzzle Y Manhattan Distance 3 1 2 3 2 4 5 6 1 7 8 nil 1 2 3 D= |Xi-Xj| + |Yi-Yj| X

Start Goal

Start Goal