Aggregate/3 in swi-prolog?

Use an existentially quantified variable, as you would with setof.

Aggregate(count, X^permutation(1,2,3,4, X), N). N = 24.

ЛИКОВ May 8 at 21:14 2 @garm0nboz1a: X^ means "there exists X", so the whole formula means something like "count the number of ways that permutation(1,2,3,4,X) succeeds for some X and call that number N. " – larsmans May 8 at 21:15.

Aggregate_all(count, permutation(1, 2, 3, 4, _), Total). Total = 24. However, as far as runtime and stack overflows are concerned it seems to perform equally well to your findall+length solution:?

- N is 10^7, time(aggregate_all(count, between(1, N, _), Total)). % 10,000,022 inferences, 5.075 CPU in 5.089 seconds (100% CPU, 1970306 Lips) N = Total, Total = 10000000.?- N is 10^7, time((findall(X, between(1, N, _), L), length(L, Total))). % 10,000,013 inferences, 4.489 CPU in 4.501 seconds (100% CPU, 2227879 Lips) N = 10000000, L = _G30000569, _G30000566, _G30000545|..., Total = 10000000.?- N is 10^8, aggregate_all(count, between(1, N, _), Total).

ERROR: Out of global stack? - N is 10^8, findall(X, between(1, N, _), L), length(L, Total). ERROR: Out of global stack You can count the solutions using assert/retract, this is quite slow but does avoid the "out of stack" problem:?

- assert(counter(0)), N is 10^8, between(1, N, _), retract(counter(C)), C1 is C + 1, assert(counter(C1)), fail ; retract(counter(C)). C = 100000000.

In SWI-Prolog there is a much more efficient version, that also avoid locking the global store. So simply using nb_setval and nb_getval you gain at least 3 times the performance (more on multithreading). Just little time ago another question was on counting solutions.

Being the base of aggregation it's an obvious stop point while learning Prolog. To evaluate the efficiency gain we get using these monothread semantically equivalent calls: count_solutions(Goal, N) :- assert(count_solutions_store(0)), repeat, ( call(Goal), retract(count_solutions_store(SoFar)), Updated is SoFar + 1, assert(count_solutions_store(Updated)), fail ; retract(count_solutions_store(N)) ),!. :- dynamic count_solutions_store/1.

% no declaration required here count_solutions_nb(Goal, N) :- nb_setval(count_solutions_store, 0), repeat, ( call(Goal), nb_getval(count_solutions_store, SoFar), Updated is SoFar + 1, nb_setval(count_solutions_store, Updated), fail ; nb_getval(count_solutions_store, N) ),!. Parent(jane,dick). Parent(michael,dick).

Parent(michael,asd). Numberofchildren(Parent, N) :- count_solutions_nb(parent(Parent, _), N). Many_solutions :- between(1, 500000, _).

Time_iso :- time(count_solutions(many_solutions, N)), write(N), nl. Time_swi :- time(count_solutions_nb(many_solutions, N)), writeln(N). On my system, I get?

- count_sol. % count_sol compiled 0,00 sec, 244 bytes true.?- time_iso. Tim% 1,000,006 inferences, 2,805 CPU in 2,805 seconds (100% CPU, 356510 Lips) 500000 true.?- time_swi.

% 2,000,010 inferences, 0,768 CPU in 0,768 seconds (100% CPU, 2603693 Lips) 500000 true.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions