## Iterative search using Prolog

Question

search.pl

``````solve(P) :-
start(Start),
search(Start,[Start],Q),
reverse(Q,P).

search(S,P,P) :- goal(S), !.         /* done                  */
search(S,Visited,P) :-
next_state(S,Nxt),              /* generate next state   */
safe_state(Nxt),                /* check safety          */
no_loop(Nxt,Visited),           /* check for loop        */
search(Nxt,[Nxt|Visited],P).    /* continue searching... */

no_loop(Nxt,Visited) :-
\+member(Nxt,Visited).
``````

queen.pl

``````start([]).
goal(S) :- length(S,8).

next_state(S,[C|S]) :- member(C,[1,2,3,4,5,6,7,8]),notmember(C,S).
safe_state([C|S]) :- length(S,L),
Sum is C+L+1, Diff is C-L-1,
safe_state(S,Sum,Diff).

safe_state([],_,_) :- !.
safe_state([F|R],Sm,Df) :- length(R,L),
X is F+L+1,
X \= Sm,
Y is F-L-1,
Y \= Df,
safe_state(R,Sm,Df).
``````

how to run this ??? im not getting how to work on this code . it is for iterative search using prolog. concept of search and how it can be implemented in prolog through graph method. Implementation of basic search for finding next state is done in this experiment.

Show source
2017-09-05 20:09 0 Answers