Simply Logical Chapter 5 p 102 searchAgenda Goal

  • Slides: 19
Download presentation
Simply Logical – Chapter 5 p. 102 % search(Agenda, Goal) <- Goal is a

Simply Logical – Chapter 5 p. 102 % search(Agenda, Goal) <- Goal is a goal node, and a % descendant of one of the nodes % on the Agenda search(Agenda, Goal): next(Agenda, Goal, Rest), goal(Goal). search(Agenda, Goal): next(Agenda, Current, Rest), children(Current, Children), add(Children, Rest, New. Agenda), search(New. Agenda, Goal). Agenda-based search © Peter Flach, 2000

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 103 -6 search_df([Goal|Rest], Goal):

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 103 -6 search_df([Goal|Rest], Goal): goal(Goal). search_df([Current|Rest], Goal): children(Current, Children), append(Children, Rest, New. Agenda), search_df(New. Agenda, Goal). search_bf([Goal|Rest], Goal): goal(Goal). search_bf([Current|Rest], Goal): children(Current, Children), append(Rest, Children, New. Agenda), search_bf(New. Agenda, Goal). children(Node, Children): findall(C, arc(Node, C), Children). Depth-first vs. breadth-first search

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 103 -6 +Breadth-first search

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 103 -6 +Breadth-first search 3 agenda = queue (first-in first-out) 3 complete: guaranteed to find all solutions 3 first solution founds along shortest path 3 requires O(Bn) memory +Depth-first search 3 agenda = stack (last-in first-out) 3 incomplete: may get trapped in infinite branch 3 no shortest-path property 3 requires O(B n) memory Depth-first vs. breadth-first search

Simply Logical – Chapter 5 p. 104 % depth-first search with loop detection search_df_loop([Goal|Rest],

Simply Logical – Chapter 5 p. 104 % depth-first search with loop detection search_df_loop([Goal|Rest], Visited, Goal): goal(Goal). search_df_loop([Current|Rest], Visited, Goal): children(Current, Children), add_df(Children, Rest, Visited, New. Agenda), search_df_loop(New. Agenda, [Current|Visited], Goal). add_df([], Agenda, Visited, Agenda). add_df([Child|Rest], Old. Agenda, Visited, [Child|New. Agenda]): not element(Child, Old. Agenda), not element(Child, Visited), add_df(Rest, Old. Agenda, Visited, New. Agenda). add_df([Child|Rest], Old. Agenda, Visited, New. Agenda): element(Child, Old. Agenda), add_df(Rest, Old. Agenda, Visited, New. Agenda). add_df([Child|Rest], Old. Agenda, Visited, New. Agenda): element(Child, Visited), add_df(Rest, Old. Agenda, Visited, New. Agenda). Loop detection © Peter Flach, 2000

Simply Logical – Chapter 5 p. 105 % depth-first search by means of backtracking

Simply Logical – Chapter 5 p. 105 % depth-first search by means of backtracking search_bt(Goal, Goal): goal(Goal). search_bt(Current, Goal): arc(Current, Child), search_bt(Child, Goal). % backtracking depth-first search with depth bound search_d(D, Goal): goal(Goal). search_d(D, Current, Goal): D>0, D 1 is D-1, arc(Current, Child), search_d(D 1, Child, Goal). Backtracking search © Peter Flach, 2000

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 106 search_id(First, Goal): search_id(1,

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 106 search_id(First, Goal): search_id(1, First, Goal). search_id(D, Current, Goal): search_d(D, Current, Goal). search_id(D, Current, Goal): D 1 is D+1, search_id(D 1, Current, Goal). % start with depth 1 % increase depth +combines advantages of breadth-first search (complete, shortest path) with those of depth-first search (memory-efficient) Iterative deepening

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 108 prove_df_a(Goal): prove_df_a([Goal]). prove(true):

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 108 prove_df_a(Goal): prove_df_a([Goal]). prove(true): -!. prove((A, B)): -!, clause(A, C), conj_append(C, B, D), prove(D). prove(A): clause(A, B), prove(B). prove_df_a([true|Agenda]). prove_df_a([(A, B)|Agenda]): -!, findall(D, (clause(A, C), conj_append(C, B, D)), Children), append(Children, Agenda, New. Agenda), prove_df_a(New. Agenda). prove_df_a([A|Agenda]): findall(B, clause(A, B), Children), append(Children, Agenda, New. Agenda), prove_df_a(New. Agenda). Agenda-based SLD-prover

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 109 refute((false: -true)). refute((A,

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 109 refute((false: -true)). refute((A, C)): cl(Cl), resolve(A, Cl, R), refute(R). % refute_bf(Clause) <- Clause is refuted by clauses % defined by cl/1 % (breadth-first search strategy) refute_bf_a(Clause): refute_bf_a([a(Clause, Clause)], Clause). refute_bf_a([a((false: -true), Clause)|Rest], Clause). refute_bf_a([a(A, C)|Rest], Clause): findall(a(R, C), (cl(Cl), resolve(A, Cl, R)), Children), append(Rest, Children, New. Agenda), % breadth-first refute_bf_a(New. Agenda, Clause). Refutation prover for clausal logic

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 111 % model(M) <-

Simply Logical – Chapter 5 © Peter Flach, 2000 p. 111 % model(M) <- M is a model of the clauses defined by cl/1 model(M): model([], M). model(M 0, M): is_violated(Head, M 0), !, disj_element(L, Head), model([L|M 0], M). model(M, M). is_violated(H, M): cl((H: -B)), satisfied_body(B, M), not satisfied_head(H, M). Forward chaining % instance of violated clause % L: ground literal from head % add L to the model % no more violated clauses % grounds the variables

Simply Logical – Chapter 5 © Peter Flach, 2000 married(X); bachelor(X): -man(X), adult(X). has_wife(X):

Simply Logical – Chapter 5 © Peter Flach, 2000 married(X); bachelor(X): -man(X), adult(X). has_wife(X): -married(X), man(X). man(paul). adult(paul). ? -model([], M) : -is_violated(Head, []), !, disj_element(L, Head), model([L], M). [] M=[] : -model([man(p)], M) : -model([adult(p), man(p)], M) : -model([married(p), adult(p), man(p)], M) : -model([has_wife(p), married(p), adult(p), man(p)], M) [] Forward chaining: example : -model([bachelor(p), adult(p), man(p)], M) []

Simply Logical – Chapter 5 p. 113 % model_d(D, M) <- M is a

Simply Logical – Chapter 5 p. 113 % model_d(D, M) <- M is a submodel of the clauses % defined by cl/1 model_d(D, M): model_d(D, [], M). model_d(0, M, M). model_d(D, M 0, M): D>0, D 1 is D-1, findall(H, is_violated(H, M 0), Heads), satisfy_clauses(Heads, M 0, M 1), model_d(D 1, M). satisfy_clauses([], M, M). satisfy_clauses([H|Hs], M 0, M): disj_element(L, H), satisfy_clauses(Hs, [L|M 0], M). Forward chaining with depth-bound © Peter Flach, 2000

Simply Logical – Chapter 6 p. 117 -8 search_bstf([Goal|Rest], Goal): goal(Goal). search_bstf([Current|Rest], Goal): children(Current,

Simply Logical – Chapter 6 p. 117 -8 search_bstf([Goal|Rest], Goal): goal(Goal). search_bstf([Current|Rest], Goal): children(Current, Children), add_bstf(Children, Rest, New. Agenda), search_bstf(New. Agenda, Goal). % add_bstf(A, B, C) <- C contains the elements of A and B % (B and C sorted according to eval/2) add_bstf([], Agenda). add_bstf([Child|Children], Old. Agenda, New. Agenda): add_one(Child, Old. Agenda, Tmp. Agenda), add_bstf(Children, Tmp. Agenda, New. Agenda). % add_one(S, A, B) <- B is A with S inserted acc. to eval/2 add_one(Child, Old. Agenda, New. Agenda): eval(Child, Value), add_one(Value, Child, Old. Agenda, New. Agenda). Best-first search © Peter Flach, 2000

Simply Logical – Chapter 6 p. 120 © Peter Flach, 2000 % tiles_a(A, M,

Simply Logical – Chapter 6 p. 120 © Peter Flach, 2000 % tiles_a(A, M, V 0, V) <- goal position can be reached from % one of the positions on A with last % move M (best-first strategy) tiles_a([v(V, Last. Move)|Rest], Last. Move, Visited): goal(Last. Move). tiles_a([v(V, Last. Move)|Rest], Goal, Visited 0, Visited): show_move(Last. Move, V), setof 0(v(Value, Next. Move), ( move(Last. Move, Next. Move), eval(Next. Move, Value) ), Children), % Children sorted on Value merge(Children, Rest, New. Agenda), % best-first tiles_a(New. Agenda, Goal, [Last. Move|Visited 0], Visited). Solving a puzzle

Simply Logical – Chapter 6 © Peter Flach, 2000 p. 123 out. Of. Place

Simply Logical – Chapter 6 © Peter Flach, 2000 p. 123 out. Of. Place b. Left. Ofw 0 9 0 12 0 18 1 9 1 10 1 15 2 8 3 9 3 13 4 7 4 11 5 7 6 8 6 6 8 4 7 7 8 4 9 4 8 7 9 4 11 3 9 6 10 3 12 2 10 6 12 2 14 0 12 2 13 1 13 2 15 0 Comparing heuristics

Simply Logical – Chapter 6 © Peter Flach, 2000 p. 124 + An A

Simply Logical – Chapter 6 © Peter Flach, 2000 p. 124 + An A algorithm is a best-first search algorithm that aims at minimising the total cost along a path from start to goal. f(n) = g(n) + h(n) estimate of total cost along path through n A algorithm actual cost to reach n estimate of cost to reach goal from n

Simply Logical – Chapter 6 © Peter Flach, 2000 p. 125 + A heuristic

Simply Logical – Chapter 6 © Peter Flach, 2000 p. 125 + A heuristic is (globally) optimistic or admissible if the estimated cost of reaching a goal is always less than the actual cost. h(n) ≤ h*(n) estimate of cost to reach goal from n actual (unknown) cost to reach goal from n + A heuristic is monotonic (locally optimistic) if the estimated cost of reaching any node is always less than the actual cost. h(n 1)– h(n 2)≤ h*(n 1)–h*(n 2) Global and local optimism

Simply Logical – Chapter 6 © Peter Flach, 2000 p. 126 start 3 1

Simply Logical – Chapter 6 © Peter Flach, 2000 p. 126 start 3 1 p 0 q r s 2 [start-3] 1 goal start 3 1 p r 0 p 1 r 0 2 goal q 1 goal p 0 q r 1 s goal start 3 p 1 p 0 q r 2 q 1 s goal start 3 p q goal 1 1 p 0 q r 2 s goal 1 [q-2, r-3] [r-3, s-4] 2 [s-3, s-4] 2 [goal-3, s-4] 1 start 3 [p-2, r-3] 2 1 start 3 s Non-monotonic heuristic 1 2 2 1 start 3 r 0 r goal s 1 q s q goal 0 start 3 r 0 p 1 1 2 2 1 start 3 s s r goal r 0 q start 3 s 1 0 s p q p 1 r 0 1 2 q s 1 start 3

Simply Logical – Chapter 6 p. 127 © Peter Flach, 2000 search_beam(Agenda, Goal): search_beam(1,

Simply Logical – Chapter 6 p. 127 © Peter Flach, 2000 search_beam(Agenda, Goal): search_beam(1, Agenda, [], Goal). search_beam(D, [], Next. Layer, Goal): D 1 is D+1, search_beam(D 1, Next. Layer, [], Goal). search_beam(D, [Goal|Rest], Next. Layer, Goal): goal(Goal). search_beam(D, [Current|Rest], Next. Layer, Goal): children(Current, Children), add_beam(D, Children, Next. Layer, New. Next. Layer), search_beam(D, Rest, New. Next. Layer, Goal). + Here, the number of children to be added to the beam is made dependent on the depth D of the node 3 in order to keep depth as a ‘global’ variable, search is layer-by-layer Beam search

Simply Logical – Chapter 6 © Peter Flach, 2000 p. 128 search_hc(Goal, Goal): goal(Goal).

Simply Logical – Chapter 6 © Peter Flach, 2000 p. 128 search_hc(Goal, Goal): goal(Goal). search_hc(Current, Goal): children(Current, Children), select_best(Children, Best), search_hc(Best, Goal). % hill_climbing as a variant of best-first search_hc([Goal|_], Goal): goal(Goal). search_hc([Current|_], Goal): children(Current, Children), add_bstf(Children, [], New. Agenda), search_hc(New. Agenda, Goal). Hill-climbing