Prolog findall infinite loop

左心房为你撑大大i 提交于 2019-12-13 22:06:13

问题


I have a predicate that asserts things. When I use a findall it goes into an infinite loop.

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

assertarv([]).

When I use findall(A,veiculos_troncos(A),NTr) , after having used the above predicate to assert many points (coordenates).

This is what I get(using 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

And etc... And it Keeps going around and around until it gives again

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

And so on and so on...

I don't undestand why.

My 'assertion' predicate works fine (I think), the problem is when I use the findall. It keeps going through the same solutions over and over.

Example:

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).

what I get is above.

until I get this error:

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

Here is the full programm(Source File):

:- 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 is L + 1.

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

NL is L - 1.

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

NC is C + 1.

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

NC is 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):-
!.

(end of source file)

Doing this:

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).

While get you the following error:

 ERROR: Out of global stack

^ Exception: (10) findall(_G16022412, user:member([_G16022412|_G16022413], [[ (-8, 3, 19, [...|...], 1), (-8, 2, 18, ..., ...)], [ (-8, 3, 19, ..., ...), (-8, 2, ..., ...)], [ (-8, 3, ..., ...), (-8, ..., ...)], [ (-8, ..., ...), (..., ...)], [ (..., ...)|...], [...|...]|...]), _G16022446) ?

来源:https://stackoverflow.com/questions/40701624/prolog-findall-infinite-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!