我如何偏离回溯?

时间:2014-02-26 20:17:03

标签: prolog backtracking

我有3,4个这样的规则:

pagaTot(X,Acc,PagaMens):-   giorno(d(X,_,14),[iniziato(I,_,_),_,ListServ|L]),!,
                            length(ListaServ,N),N<2,
                            member(finito(F),L),
                            I>=6, F=<24,
                            I1 is round(I)+round((I-round(I))*100)/60,
                            F1 is round(F)+round((F-round(F))*100)/60,
                            OreTot is F1-I1,
                            OreTot =<8,
                            Paga is (F1-I1)*6,
                            Acc1 is Paga+Acc,X1 is X+1,
                            pagaTot(X1,Acc1,PagaMens).



pagaTot(X,Acc,PagaMens):-   giorno(d(X,_,14),[iniziato(I,_,_),_,ListServ|L]),!,
                        length(ListaServ,N),N<2,
                        member(finito(F),L),
                        I>=6, F=<24,
                        I1 is round(I)+round((I-round(I))*100)/60,
                        F1 is round(F)+round((F-round(F))*100)/60,
                        OreTot is F1-I1,
                        OreTot >8,
                        OreDa7 is OreTot-8,
                        OreDa6 is OreTot-OreDa7, 
                        Paga is OreDa6*6+OreDa7*7,
                        Acc1 is Paga+Acc,X1 is X+1,
                        pagaTot(X1,Acc1,PagaMens).

当系统发现它是错误的选择时,它会回溯。我不希望它再次检查第一个子目标中的其他选择 - 'giorno(d(...'。它更好地进入下一个'pagaTot(X ......'这样的规则将更有效率每个规则大约有3个,4个调用。我试图在第一个子目标_,ListServ | L),!,的末尾加上'!'但是这个剪切不会让它去下一个规则。它完全失败了。 我不知道这应该放在哪里。有解决方案吗?

1 个答案:

答案 0 :(得分:0)

我会将公共部分分解,然后用if / then / else区分变化点:

pagaTot(X,Acc,PagaMens):- giorno(d(X,_,14),[iniziato(I,_,_),_,ListServ|L]),
                        length(ListaServ,N),N<2,
                        member(finito(F),L),
                        I>=6, F=<24,
                        I1 is round(I)+round((I-round(I))*100)/60,
                        F1 is round(F)+round((F-round(F))*100)/60,
                        OreTot is F1-I1,
                        (  OreTot =<8
                        -> Paga is (F1-I1)*6
                        ;  OreDa7 is OreTot-8,
                           OreDa6 is OreTot-OreDa7, 
                           Paga is OreDa6*6+OreDa7*7
                        ),
                        Acc1 is Paga+Acc,X1 is X+1
                        pagaTot(X1,Acc1,PagaMens).

希望我对cut'n'paste没有任何错误......