Prolog findall无限循环

时间:2016-11-20 07:19:52

标签: prolog prolog-findall

我有一个判断东西的谓词。当我使用findall时,它会进入无限循环。

assertarv([N|R]):-
    assert(veiculos_troncos(N)),
    assertarv(R).

assertarv([]).

当我使用findall(A,veiculos_troncos(A),NTr)之后,在使用上述谓词来断言许多点(coordenates)之后。

这是我得到的(使用findall):

Exit: (22) veiculos_troncos((-7, 6)) ? creep
   Redo: (22) veiculos_troncos(_G6719) ? creep
   Exit: (22) veiculos_troncos((-6, 6)) ? creep
   Redo: (22) veiculos_troncos(_G6719) ? creep
   Exit: (22) veiculos_troncos((-4, 4)) ? creep
   Redo: (22) veiculos_troncos(_G6719) ? creep
   Exit: (22) veiculos_troncos((1, 4)) ? creep
   Redo: (22) veiculos_troncos(_G6719) ? creep
   Exit: (22) veiculos_troncos((-1, 4)) ? creep
   Redo: (22) veiculos_troncos(_G6719) ? creep
   Exit: (22) veiculos_troncos((-8, 6)) ? creep

等等......它继续四处走动直到再次出现

Exit: (22) veiculos_troncos((-7, 6)) ? creep
   Redo: (22) veiculos_troncos(_G6719) ? creep
   Exit: (22) veiculos_troncos((-6, 6)) ? creep
   Redo: (22) veiculos_troncos(_G6719) ? creep
   Exit: (22) veiculos_troncos((-4, 4)) ? creep
   Redo: (22) veiculos_troncos(_G6719) ? creep
   Exit: (22) veiculos_troncos((1, 4)) ? creep
   Redo: (22) veiculos_troncos(_G6719) ? creep
   Exit: (22) veiculos_troncos((-1, 4)) ? creep
   Redo: (22) veiculos_troncos(_G6719) ? creep
   Exit: (22) veiculos_troncos((-8, 6)) ? creep

依此类推......

我不明白为什么。

我的'断言'谓词运行正常(我认为),问题是当我使用findall时。它一遍又一遍地经历相同的解决方案。

示例:

assertv([(7,7),(0,5),(1,7),(-1,5),(-3,3),(-4,3),(8,3),(-7,7),(-8,7),(-5,3),(-2,3),(-8,3),(7,5),(8,7),(-2,5),(6,5),(3,7),(0,7),(-1,3),(2,7),(7,3),(3,4),(-5,6),(5,6),(-2,4),(-8,4),(-7,4),(4,6),(-4,6),(-6,4),(-1,4),(4,4),(1,4),(-5,4),(2,4),(-3,6),(-6,6)],[(3,8),(6,8),(-3,8),(0,8),(-6,8)]).

findall(A,veiculos_troncos(A),NTr).

我得到的是上面的。

直到我收到此错误:

 Exception: (13) setup_call_catcher_cleanup('$bags':'$new_findall_bag', '$bags':findall_loop

这是完整的程序(源文件):

:- dynamic(nenufares_livres/1).
:- dynamic(veiculos_troncos/1).


plano_rio(T,(X,Y),Troncos,Nenufares,largura,LimP,_,TempoEX,Vis,Exp,MemM,Plano):-
assertar(Nenufares),
Antes is cputime,
plcamOpt(X,Y,T,Troncos,LimP,Vis,Exp,MemM,Plano),
Depois is cputime,
TempoEX is Depois-Antes,
retractall(nenufares_livres(_)).

devolve(A,X,Y,T,V,P):-
A=(X,Y,T,V,P).

 plcamOpt(X,Y,T,Veic_Tronc,LimP,Vis,Exp,T_Frnt,Sol) :-
inicial(X,Y,T,Veic_Tronc,I),
plcamOpt(I,LimP,Vis,Exp,[I],T_Frnt,Sol).

plcamOpt(E,LimP,Vis,Exp,Frnt,T_Frnt,F) :-
pllcamOpt([[E]],[E],LimP,Vis,[],Exp,Frnt,1,T_Frnt,F).

pllcamOpt([[F|R]|_],Vs,_,Vis,Ex,Exp,_,T_Frnt,T_Frnt,CF) :-
final(F),
length(Vs,Vis),
length(Ex,Exp),
reverse([F|R],CF).


pllcamOpt([[X|RX]|R],Vs,LimP,Vis,Ex,Exp,Frnt,TFR,T_Frnt,F) :-
devolve(X,_,_,T,V,D),
suc1(T,V),
D1 is D+1,
D1=<LimP,
devolve(Y,A,B,T1,_,_),
findall([Y,X|RX],(suc(X,Y,_),\+ member((A,B,T1,_,_),Vs)),L1),
random_permutation(L1,L),
append(Ex,[X],Ex1),
delete(Frnt,X,Frnt1),
append(Frnt1,L,Frnt2),
length(Frnt2,TF),
TF1 is max(TF,TFR),
append(R,L,NR),
findall(P,member([P|_],L),Lp),
append(Lp,Vs,NVs),
pllcamOpt(NR,NVs,LimP,Vis,Ex1,Exp,Frnt2,TF1,T_Frnt,F).



suc((X,2,T,_,D),(X1,2,T1,NTr,D1),90):-
T1 is T+1,
D1 is D+1,
X1 is X+1,
findall(A,veiculos_troncos(A),NTr).

suc((X,2,T,_,D),(X1,2,T1,NTr,D1),270):-
T1 is T+1,
D1 is D+1,
X1 is X-1,
findall(A,veiculos_troncos(A),NTr).



suc((X,Y,T,_,D),(E,Y1,T1,NTr,D1),0):-
Y>0,
T1 is T+1,
(   Y<3, E=X,! ; actualizar(T1,[(X,Y)],[],[(E,_)])),
Y1 is Y+1,
D1 is D+1,
E<9,E> -9,Y<9,
findall(A,veiculos_troncos(A),NTr),
member((E,Y1),NTr).

suc((X,Y,T,_,D),(X1,Y,T1,NTr,D1),90):-
Y>0,
T1 is T+1,
( actualizar(T1,[(X,Y)],[],[(E,_)])),
X1 is E+1,
D1 is D+1,
X1<9,
findall(A,veiculos_troncos(A),NTr),
member((X1,Y),NTr).

suc((X,Y,T,_,D),(E,Y1,T1,NTr,D1),180):-
Y>0,
T1 is T+1,
( actualizar(T1,[(X,Y)],[],[(E,_)])),
Y1 is Y-1,
D1 is D+1,
E<9,E> -9,
findall(A,veiculos_troncos(A),NTr),
member((E,Y1),NTr).

suc((X,Y,T,_,D),(X1,Y,T1,NTr,D1),270):-
Y>0,
T1 is T+1,
( actualizar(T1,[(X,Y)],[],[(E,_)])),
X1 is E-1,
D1 is D+1,
E> -8,
findall(A,veiculos_troncos(A),NTr),
member((X1,Y),NTr).

suc((X,Y,T,_,D),(E,Y,T1,NTr,D1),1):-
Y>0,
T1 is T+1,
(actualizar(T1,[(X,Y)],[],[(E,_)])),
D1 is D+1,
E<8,E> -8,Y<8,
findall(A,veiculos_troncos(A),NTr).






















assertar([]).

assertar([N|R]):-
assert(nenufares_livres(N)),
assertar(R).










suc1(T,V):-
retractall(veiculos(_)),
T1 is T+1,
actualizar(T1,V,[],L),
assertarv(L).







actualizar(_,[],D, D):-
!.

actualizar(Tempo,[(X,Y)|RestoVeiculos],Acc, Res):-
linha(Y,Or,Vel),
H is mod(Tempo,Vel),
faz_andar(Or,(X,Y),C,H),
append(Acc,[C],D),
actualizar(Tempo,RestoVeiculos,D, Res).


vizinha((C,L),(C,NL),0) :-

NL是L + 1。

vizinha((C,L),(C,NL),180) :-

NL是L - 1。

vizinha((C,L),(NC,L),90) :-

NC是C + 1。

vizinha((C,L),(NC,L),270) :-

NC是C - 1。

reset :-
retractall(linha(_,_,_)),
retractall(limE(_)),
retractall(limT(_)),
retractall(limF(_)),
retractall(limD(_)),
retractall(margemEstrada(_)),
retractall(margemRio(_)).


setup(LimE,LimD,LimF,LimT,MargE,MargR,Linhas,Lamas,Mergs) :-
reset,
setupLims(LimE,LimD,LimF,LimT,MargE,MargR),
setupLinhas(Linhas),
setupLamas(Lamas),
setupMergulhadores(Mergs).

setupLims(E,D,F,T,ME,MR) :-
assert(limE(E)),
assert(limD(D)),
assert(limT(T)),
assert(limF(F)),
assert(margemEstrada(ME)),
assert(margemRio(MR)).

setupLinhas(Linhas) :-
member((Linha,Dir,Vel),Linhas),
assert(linha(Linha,Dir,Vel)),
fail.
setupLinhas(_).

setupLamas(Lamas) :-
member((X,Y,Vol),Lamas),
assert(lama(X,Y,Vol)),
fail.
setupLamas(_).


setupMergulhadores(Mergs) :-
member((X,Y,MovsSuperf,MovsFundo),Mergs),
assert(mergulhadora(X,Y,MovsSuperf,MovsFundo)),
fail.
setupMergulhadores(_).











final((_,0,_,_,_,_)).

final((X,8,_,_,_)):-
nenufares_livres((X,8)).


inicial(X,-6,T,V,(X,-6,T,0,V,0)).



inicial(X,2,T,Tr,(X,2,T,Tr1,0)):-
findall((A,B),(member((A,B),Tr),\+mergulhadora(A,B,_,_)),Tr1).


assertarv([N|R]):-
assert(veiculos_troncos(N)),
assertarv(R).

assertarv([]).





actualizar(Tempo,[(X,Y)|RestoVeiculos],Acc, Res):-
linha(Y,Or,Vel),
H is mod(Tempo,Vel),
faz_andar(Or,(X,Y),C,H),
append(Acc,[C],D),
actualizar(Tempo,RestoVeiculos,D, Res).

faz_andar(90,(X,Y),(X1,Y),0):-
(   (   X+1 ==9 ,X1 is -8),! ;X1 is X+1).

faz_andar(270,(X,Y),(X1,Y),0):-
(   (X-1 == -9, X1 is 8),! ;X1 is X-1).

faz_andar(_,(X,Y),(X,Y),C):-
C=\=0.


actualizar(_,[],D, D):-
!.

(源文件的结尾)

这样做:

setup(-8,8,-8,8,-6,2,[(-5,270,6),(-4,90,6),(-3,270,5),(-2,90,4),(-1,270,2),(3,90,6),(4,270,4),(5,90,7),(6,270,5),(7,90,3)],[(3,-5,3),(-1,-5,2),(-5,-5,2),(-4,-3,3),(6,-4,2),(7,-5,1),(-5,-1,3),(-8,-3,1),(-1,-1,2),(4,-5,1),(-2,-5,1),(-1,-4,1),(0,-2,3),(-7,-2,1)],[(7,4,1,1)]),plano_rio(18,(-8,2),[(7,7),(0,5),(1,7),(-1,5),(-3,3),(-4,3),(8,3),(-7,7),(-8,7),(-5,3),(-2,3),(-8,3),(7,5),(8,7),(-2,5),(6,5),(3,7),(0,7),(-1,3),(2,7),(7,3),(3,4),(-5,6),(5,6),(-2,4),(-8,4),(-7,4),(4,6),(-4,6),(-6,4),(-1,4),(4,4),(1,4),(-5,4),(2,4),(-3,6),(-6,6)],[(3,8),(6,8),(-3,8),(0,8),(-6,8)],largura,50,_,Tempo,Vis,Expan,MaxMem,Plano).

虽然会出现以下错误:

 ERROR: Out of global stack

^异常:(10)findall(_G16022412,user:member([_ G16022412 | _G16022413],[[( - 8,3,19,[... | ...],1),( - 8, 2,18,...,...)],[(-8,3,19,...,...),( - 8,2,...,...)],[( -8,3,...,...),( - 8,...,...)],[(-8,...,...),(...,... )],[(...,...)| ...],[... | ...] | ...]),_ G16022446)?

0 个答案:

没有答案