1 / 14

Searching a graph or a state space

Searching a graph or a state space. Searching a tree. % ?- path(a,X). % X = a ; X = b ; X = e ; X = f ; X = c ; X = g ; X = h ; % X = d ; no % Order of generated answers is given by the SLD strategy, ie. „ depth first search “ h(a,b). h(a,c). h(a,d).

wyanet
Download Presentation

Searching a graph or a state space

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Searching a graphor a state space

  2. Searching a tree % ?- path(a,X). % X = a ; X = b ; X = e ; X = f ; X = c ; X = g ; X = h ; % X = d ; no % Order of generated answers is given by the SLD strategy, ie. „depth first search“ h(a,b). h(a,c). h(a,d). h(b,e). h(b,f). h(c,g). h(c,h). path(X,X). path(Z,D):-h(Z,N), path(N,D).

  3. Search through a general graph h(a,b). h(a,c). h(a,d). h(b,e). h(b,f). h(c,g). h(c,h). h(e,a). path(X,X). path(Z,D):-h(Z,N), path(N,D). % ?- path(a,X). % X = a ; X = b ; X = e ; X = a ; X = b ; X = e ; X = a ; ... Where is the problem? One should avoid repeated treatment of a node, which has been considered already.

  4. Searching a graph with history record /* Used notation:facts listing all the edgesh(Initial_node,End_node)and all the goal nodesg(Node) */ h(a,b). h(a,c). h(a,d). h(b,e). h(b,f).h(c,g). h(c,h). h(c,a). h(g,k). h(g,l).h(d,i). h(d,j). h(j,m). h(j,n). g(d). g(f). g(i). g(l). g(n). % c_hl(+Starting_node,-Goal_node,-Path) c_hl(X,X,[]) :- g(X). c_hl(X,Y,[M|L]) :- h(X,M),c_hl(M,Y,L).

  5. Searching a graph with check of cycles % more effectivesolution – elimination of cycles solve(FromN, Solution) :- dfs([],FromN,Solution). dfs(BeginnigPartS,Node,[Node| BeginnigPartS]):- g(Node). dfs(BeginnigPartS, Node, Solution):- h(Node,NodeNext), not member(NodeNext, BeginnigPartS), dfs([NodeNext| BeginnigPartS], NodeNext , Solution). % limited depth dfs % l_dfs(+FromNode, -Solution, +Max_depth) l_dfs(Node,[Node], _ ):- g(Node). l_dfs(Node,[Node|PartSolution], Max_depth ):- Max_Hl > 0,h(Node,NextN),Max1 is Max_Hl –1, l_dfs(NextN, PartSolution, Max1 ).

  6. 2. Order predicatessetof (+Z,+Goal(Z,X),-S) It creates the list S of all instances for Z, such that the goal Goal(Z, X) holds (value of X remains fixed) : • The list S is presented as an ordered set, ie. No repetition of elements • In the case there is no solution for Goal(Z,X), the answer of setof is false Příklad: h(a,b). h(a,c). h(a,d). h(b,e). h(b,f). h(c,g). h(c,h). h(c,a). ?- setof(Z,h(a,Z),S). S = [b,c,d] ?- setof(Z,h(X,Z),S). X = a, S = [b, c, d] ; More? X = b, S = [e, f] ; X = c, S = [a, g, h]

  7. 2. Order predicatesbagof (+Z,+Goal(Z,X),-S) The variables in the Goal +Goal(Z,X) can be existentially quantifiedusing the symbol^ h(a,b). h(a,c). h(a,d). h(b,e). h(b,f). h(c,g). h(c,h). h(c,a). ?- setof(Z,X^h(X,Z),S). S = [b, c, d, e, f, g, h, a] bagof(+Z,+Goal(Z,_),-S) finds the listS of all instances for Z solving the goal Goal(Z,_). An element can have several occurences in S (it is not a set) --> quicker thansetof ?- bagof(Z, h(a,Z), S). S = [ b, c, d ] % Caution ! There is no edge from d: ?- bagof(Z, h(d,Z), S). fail

  8. 2. Order Predicatesfindall (+Term(Z),+Goal(Z),-S) Simmilar to bagof, but it is ever successfull : It creates the list S (with repetition of elements) of instances of the term Term using all instances for Z which solve the Goal(Z) – If there is no solution for Goal(Z), the resulting S is [ ] Příklad: h(a,b). h(a,c). h(a,d). h(b,e). h(b,f). h(c,g). h(c,h). h(c,a). ?- findall(symetricky(X,Y), (h(X,Y),h(Y,X)) ,S). S = [symetricky(a,c)] ?- findall(symetricky(b,Y), (h(b,Y),h(Y,b)) ,S). S = [ ] ?- findall(Z, h(f,Z), S). S = [ ]

  9. Breadth first search add_back(+List,+Node,-Result) finds all direct successors of the nodeNodeandadds this list at the back ofthe List, the resulting list is denoted Result. wave(+List,Z) searches for a path, starting from the first element of List and leading to Z (simmilar to OPEN in procedural solution) bfs(X,X) :- goal(X). bfs(X,Y) :- wave([X],Y). wave([],_) :- write(‚No more goal nodes in the graph.'),nl. wave([H|T],H):- goal(H). wave([Y|L],Z):- add_back(L,Y,N),wave(N,Z). add_back(L,Y,N) :-direct_successors(Y,S),append(L,S,N). direct_successors(Y,S) :- bagof(Z,h(Y,Z),S),!. direct_successors(Y,[]).

  10. Searching a graph and means of Prolog • trees and acyclic graphs - pure Prolog • finite graphs where cycle can appear - Prolog + negation * • infinite (but localy finite) graphs - Prolog + negation* + 2nd order predicates ** * needed to check the cycle ** breadth first search

  11. Searching in the state space • State space = implicitely defined graph, its edges correspond to allowed moves. The problem is described using following predicates: • move(State, Aplicable_action), specifies all actions which can be accomplished in the given State • update(State, Aplicable_action, Resulting_state_after_action), generates new state • init_state(S), final_state(S) • Solution is very close to graph search.

  12. The problem g-w-c Reprezentation of states wgc(Pozition_boat, Who_left, Who_right) Reprezentation of actions: action is characterized by cargo of the boat % Description of the initial and goal state init_state(wgc, wgc(left,[w,g,c],[])). final_state(wgc, wgc(right,[],[w,g,c])). % Conditions specifying actions available in the given state move(wgc(left,L,R),Cargo):-element(Cargo,L). move(wgc(right,L,R),Cargo):-element(Cargo,R). move(wgc(B,L,R),alone).

  13. % State resulting from the considered action update(wgc(B,L,R),Cargo,wgc(B1,L1,R1)):- update_boat(B,B1), update_banks(Cargo,B,L,R,L1,R1). update_boat(left,right). update_boat(right,left). update_banks(alone,B,L,R,L,R). update_banks(Cargo,left,L,R,L1,R1):- select(Cargo,L,L1), insert(Cargo,R,R1). update_banks(Cargo,right,L,R,L1,R1):-… %Checking for danger legal(wgc(left,L,R)):- not illegal(R). … illegal(L):- member(w,L), member(g,L). …

  14. General programmefor state space problems % Alg. Generating and searching through the state space starting from initial_state % and aiming for final_stateapplying following description of the task: % move(State, Applicable_ation) % update(Stav, Applicable_action, State_Result_of_action) solve_dfs(State,History,[]):-final_state(State). solve_dfs(State,History,[Move|Moves]):- move(State,Move), update(State,Move,State1), legal(State1), not member(State1,History), solve_dfs(State1,[State1|History],Moves). %Testování na konkrétní úloze test_dfs(Problem,Moves):- initial_state(Problem,State), solve_dfs(State,[State],Moves).

More Related