# Permutations - Infinite Loop Error

**URL:** <https://swi-prolog.discourse.group/t/permutations-infinite-loop-error/5992>\
**Category:** Help!\
**Tags:** discussion, how-to\
**Created:** [December 2, 2022, 3:39am UTC](https://swi-prolog.discourse.group/t/permutations-infinite-loop-error/5992 "2022-12-02T03:39:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tripsytina](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@tripsytina](https://swi-prolog.discourse.group/u/tripsytina)\
**Post date:** [December 2, 2022, 3:39am UTC](https://swi-prolog.discourse.group/t/permutations-infinite-loop-error/5992/1 "2022-12-02T03:39:07Z")

</div>

I’m using: SWI-Prolog version 8.4.3

I want the code to: Generate the most time-efficient solution through permutations of a list.

But what I’m getting is: An infinite loop after doing the first permutated list.

My code looks like this:

```prolog
% your code here
%a armazensEntregas
armazens_entregas(L):- findall(K,entrega(_,_,_,K,_,_),L).

%b addMatosinhos
add_matosinhos(L,LB):-append([5],L,LA),append(LA,[5],LB).

:-dynamic custo_min/2.

%c calcula_tempo
calcula_tempo(LC,Final):-
        bateria_camiao(LC,CarM,TempR),
        peso_camiao(3,PesoC),
        tempo_recarga(LC,CarM,TempR,RecargaC,PesoC),
        tempo_extra(LC,ExtraC),
        tempo_entregas(LC,TempoE),
        tempo(LC,Custo,PesoC),
        somar_tudo(RecargaC,ExtraC,TempoE,Custo,Final).

%custo
tempo([_],0,_).
tempo([C1,C2|LC],Custo,PesoC):-
    entregas_peso(C1,LE),
    peso_entregas(LE,PesoE),
    PesoC1 is PesoC - PesoE,
    tempo([C2|LC],Custo1,PesoC1),
    (dadosCam_t_e_ta(_,C1,C2,Tempo,_,_)
    ;dadosCam_t_e_ta(_,C2,C1,Tempo,_,_)),
    peso_camiao(C1,PesoB),
    Tempo1 is (PesoC1*Tempo)/PesoB,
    Custo is Custo1+Tempo1.

%d bateria_camiao (assumindo que o máximo de carga em % é 80%)
bateria_camiao([HC1|_],CarM1,TempR):- dadosCam_t_e_ta(Nome,HC1,_,_,_,_),carateristicasCam(Nome,_,_,CarM,_,TempR),CarM1 is CarM*(0.8).

%e peso do camiao (assumindo que possui carga máxima)
peso_camiao(C1,PesoC):- dadosCam_t_e_ta(Nome,C1,_,_,_,_),carateristicasCam(Nome,Tara,CapC,_,_,_), PesoC is Tara + CapC.

%e tempo_extra
tempo_extra([_],0).
tempo_extra([C1,C2|LC],ExtraC):-
        tempo_extra([C2|LC],ExtraC1),
        (dadosCam_t_e_ta(_,C1,C2,_,_,Extra);dadosCam_t_e_ta(_,C2,C1,_,_,Extra)),
        ExtraC is ExtraC1 + Extra.

%f tempo_recarga
tempo_recarga([_],_,_,0,_).
tempo_recarga([C1,C2|LC],CarM,TempR,RecargaC,PesoC):-
        entregas_peso(C1,LE),
        peso_entregas(LE,PesoE),
        PesoC1 is PesoC - PesoE,
        tempo_recarga([C2|LC],CarM,TempR,RecargaC1,PesoC1),
        (dadosCam_t_e_ta(_,C1,C2,_,Energia,_);
        dadosCam_t_e_ta(_,C2,C1,_,Energia,_)),
        peso_camiao(C1,PesoB),
        Energia1 is (PesoC1*Energia)/PesoB,
        ((CarM - Energia1 < 20, RecargaC is RecargaC1 + ((80 - (CarM - Energia1))*TempR)/60);RecargaC is RecargaC1 + 0).

%g entregas_peso
entregas_peso(C1,LE):- findall(M,entrega(_,_,M,C1,_,_),LE).

%g tempo_entregas
tempo_entregas([],0).
tempo_entregas([HC|LC],TempoE):-
        tempo_entregas(LC,TempoE1),
        buscar_entregas(HC,LI),
        entregas_tempo(LI,TempoT),
        TempoE is TempoE1 + TempoT.

%k buscar_entregas
buscar_entregas(HC,LI):-findall(Id,entrega(Id,_,_,HC,_,_),LI).

%j entregas_tempo
entregas_tempo([],0).
entregas_tempo([HI|LI],TempoT):-
        entregas_tempo(LI,TempoT1),
        entrega(HI,_,_,_,T,TS),
        TempoT is TempoT1 + (T + TS).

%h peso_entregas
peso_entregas([],0).
peso_entregas([HC1|LC1],PesoE):-
        peso_entregas(LC1,PesoE1), PesoE is PesoE1 + HC1.

%i somar_tudo
somar_tudo(RecargaC,ExtraC,TempoE,Custo,Final):- Final is (RecargaC + ExtraC + TempoE + Custo).

%d seq_custo_min

seq_custo_min(LC,Final):-(run;true),custo_min(LC,Final).

run:-
    retractall(custo_min(_,_)),
    assertz(custo_min(_,100000)),
    armazens_entregas(LC),
    permutation(LC,LCPerm),
    add_matosinhos(LCPerm,LCPerm1),
    calcula_tempo(LCPerm1,Final),
    atualiza(LCPerm1,Final),
    fail.

atualiza(LCPerm1,Final):-
  custo_min(_,FinalMin),
  ((Final<FinalMin,!,retract(custo_min(_,_)),assertz(custo_min(LCPerm1,Final)),
    write(Final),nl)
    ;true).

```

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [December 2, 2022, 10:07am UTC](https://swi-prolog.discourse.group/t/permutations-infinite-loop-error/5992/2 "2022-12-02T10:07:23Z")

</div>

Here’s a safe and fast alternative to permutation/2 : [list - Prolog performance and recursion type - Stack Overflow](https://stackoverflow.com/a/74655536/)

---

<div class="post-metadata">

**Author:** ![tripsytina](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@tripsytina](https://swi-prolog.discourse.group/u/tripsytina)\
**Post date:** [December 2, 2022, 2:30pm UTC](https://swi-prolog.discourse.group/t/permutations-infinite-loop-error/5992/4 "2022-12-02T14:30:27Z")

</div>

It didn’t work. After the first list it gets stuck on atualiza(LCPerm1,Final).
