Prolog 脱离全局堆栈

发布于 2024-11-14 09:57:46 字数 1870 浏览 4 评论 0原文

大家好,我是 Prolog 新手。 我试图在不重复的情况下组合第 3 类中的 1000 个元素。 我已经完成了代码,但我遇到了堆栈外问题。

member(T, [T|R], R) :- !.
member(T, [_|L], R) :- member(T, L, R). 

last_element([H], H) :- !.
last_element([H|[X]], X) :- !.
last_element([H|T], R) :- last_element(T, R). 


    macronutrienti(1).
    macronutrienti(2).
    macronutrienti(3).
    and so on

percentuale_macronutrienti(Alimento, R) :-
    macronutrienti(Alimento),
    R = Alimento.

combina(NRAlimenti, RIS) :-
    findall(PM, percentuale_macronutrienti(Alimento, PM), PMR),
    f1(NRAlimenti, PMR, PMR, RIS), !.

f1(1, RParz, PMR, RParz) :- !.
f1(Index, RParz, PMR, RIS) :-
    Index1 is Index - 1,
    g2(RParz, PMR, RIS1, RIS1),
    f1(Index1, RIS1, PMR, RIS).
f1(Index, RParz, PMR, RIS) :-
    Index1 is Index - 1,
    g1(RParz, PMR, RIS1, RIS1),
    f1(Index1, RIS1, PMR, RIS).

g1([A|[TA]], A1, OldR, R) :- 
    q([A], L1, A1), 
    h1(A, L1, OldR, OldR), !.
g1([A|T], A1, Risultato,  R) :- 
    q([A], L1, A1), 
    h1(A, L1, OldR, OldR),
    append(OldR, TOldR,Risultato),
    g1(T, A1, TOldR, R).

q(A, R, A1) :- last_element(A, LastElement), member(LastElement, A1, R).

g2([A|[TA]], A1, OldR, R) :-
    q(A, L1, A1), 
    h2(A, L1, OldR, OldR), !.
g2([A|T], A1, Risultato, R) :-
    q(A, L1, A1),  
    h2(A, L1, OldR, OldR),
    append(OldR, TOldR, Risultato),
    g2(T, A1, TOldR, R).

h1(_, [], _,  _) :- !.
h1(Alimento, [Alimento1], [OldM],  R) :- append([Alimento], [Alimento1], OldM).
h1(Alimento, [Alimento1|T1], [OldM|TOldM], NewM) :- 
    append([Alimento], [Alimento1], OldM),
    h1(Alimento, T1, TOldM, NewM).

h2(_, [], _,  _) :- !.
h2(Alimento, [Alimento1], [OldM],  R) :- append(Alimento, [Alimento1], OldM).
h2(Alimento, [Alimento1|T1], [OldM|TOldM], NewM) :- 
    append(Alimento, [Alimento1], OldM),
    h2(Alimento, T1, TOldM, NewM).

:- combinew(3,R).

怎么了?先感谢您

Hi guys i'm new on Prolog.
I'm trying to combine 1000 elements in class 3 without repetition.
I have done the code, but i have out of stack problem.

member(T, [T|R], R) :- !.
member(T, [_|L], R) :- member(T, L, R). 

last_element([H], H) :- !.
last_element([H|[X]], X) :- !.
last_element([H|T], R) :- last_element(T, R). 


    macronutrienti(1).
    macronutrienti(2).
    macronutrienti(3).
    and so on

percentuale_macronutrienti(Alimento, R) :-
    macronutrienti(Alimento),
    R = Alimento.

combina(NRAlimenti, RIS) :-
    findall(PM, percentuale_macronutrienti(Alimento, PM), PMR),
    f1(NRAlimenti, PMR, PMR, RIS), !.

f1(1, RParz, PMR, RParz) :- !.
f1(Index, RParz, PMR, RIS) :-
    Index1 is Index - 1,
    g2(RParz, PMR, RIS1, RIS1),
    f1(Index1, RIS1, PMR, RIS).
f1(Index, RParz, PMR, RIS) :-
    Index1 is Index - 1,
    g1(RParz, PMR, RIS1, RIS1),
    f1(Index1, RIS1, PMR, RIS).

g1([A|[TA]], A1, OldR, R) :- 
    q([A], L1, A1), 
    h1(A, L1, OldR, OldR), !.
g1([A|T], A1, Risultato,  R) :- 
    q([A], L1, A1), 
    h1(A, L1, OldR, OldR),
    append(OldR, TOldR,Risultato),
    g1(T, A1, TOldR, R).

q(A, R, A1) :- last_element(A, LastElement), member(LastElement, A1, R).

g2([A|[TA]], A1, OldR, R) :-
    q(A, L1, A1), 
    h2(A, L1, OldR, OldR), !.
g2([A|T], A1, Risultato, R) :-
    q(A, L1, A1),  
    h2(A, L1, OldR, OldR),
    append(OldR, TOldR, Risultato),
    g2(T, A1, TOldR, R).

h1(_, [], _,  _) :- !.
h1(Alimento, [Alimento1], [OldM],  R) :- append([Alimento], [Alimento1], OldM).
h1(Alimento, [Alimento1|T1], [OldM|TOldM], NewM) :- 
    append([Alimento], [Alimento1], OldM),
    h1(Alimento, T1, TOldM, NewM).

h2(_, [], _,  _) :- !.
h2(Alimento, [Alimento1], [OldM],  R) :- append(Alimento, [Alimento1], OldM).
h2(Alimento, [Alimento1|T1], [OldM|TOldM], NewM) :- 
    append(Alimento, [Alimento1], OldM),
    h2(Alimento, T1, TOldM, NewM).

:- combinew(3,R).

What is wrong? Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩饰不了的爱 2024-11-21 09:57:46

这是一个更短的代码(我认为)与您的代码相同:(

combine(N,L) :-
    findall(PM, macronutrienti(PM), PMR),
    findall(L0, combine0(N, PMR, L0), LL).

combine0(0, _, []) :- !.
combine0(I, PMR, [X|LR]) :-
    member(X, PMR, PMRR),
    I1 is I-1,
    combine0(I1, PMRR, LR).

member(T, [T|R], R).
member(T, [_|L], R) :- member(T, L, R). 

我稍微更改了您的 member/3 谓词)

但是,这仍然无法解决您的堆栈溢出问题。基本上,如果我正确理解你的问题,你想从 1000 个元素的集合中提取长度为 3 的所有排序子集。您正在查看的集合数量是 1000!/(1000-3)!,即 997.002.000 个长度为 3 的列表。这相当多,所以如果您需要一个巨大的堆栈。

如果您不需要完整列表才能继续,请更改您的工作流程以一次生成一个此类项目,然后立即处理它,并继续下一个。

Here's a much shorter code that (I think) does the same as yours:

combine(N,L) :-
    findall(PM, macronutrienti(PM), PMR),
    findall(L0, combine0(N, PMR, L0), LL).

combine0(0, _, []) :- !.
combine0(I, PMR, [X|LR]) :-
    member(X, PMR, PMRR),
    I1 is I-1,
    combine0(I1, PMRR, LR).

member(T, [T|R], R).
member(T, [_|L], R) :- member(T, L, R). 

( I slightly changed your member/3 predicate)

However, this still does not resolve your stack overflow problem. Basically, if I understood your question correctly, you want to extract all sorted subsets of length 3 from the set of 1000 elements. The number of sets you are looking at is 1000!/(1000-3)!, i.e., 997.002.000 lists of length 3. That's quite a lot, so if you'll need a huge stack.

If you don't need the complete list to continue, change your workflow to generate one such item at a time, then process it immediately, and continue with the next.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文