Did you know ... Search Documentation:
Pack modeling -- prolog/examplesFLOPS2024.pl
PublicShow source
author
- Francois Fages
version
- 1.1.3

Examples of use of SWI-Prolog pack modeling published in

  • F. Fages. A Constraint-based Mathematical Modeling Library in Prolog with Answer Constraint Semantics. 17th International Symposium on Functional and Logic Programming, FLOPS 2024. May 15, 2024 - May 17, 2024, Kumamoto, Japan. LNCS, Springer-Verlag.

Generation of constraints by comprehension on subscripted variables, in the spirit of MiniZinc modeling language:

queens(N, Queens):-
  int_array(Queens, [N], 1..N),
  for_all([I in 1..N-1, D in 1..N-I],
          (Queens[I] #\= Queens[I+D],
           Queens[I] #\= Queens[I+D]+D,
           Queens[I] #\= Queens[I+D]-D)),
  satisfy(Queens).

show_array(Queens):-
    array(Queens, [N]),
    for_all([I, J] in 1..N,
            let([QJ = Queens[J],
                 Q = if(QJ = I, 'Q', '.'),
                 B = if(J = N, '\n', ' ')],
                format("~w~w",[Q,B]))),
    nl.

?- queens(8,Queens), show_array(Queens).
Q . . . . . . .
. . . . . . Q .
. . . . Q . . .
. . . . . . . Q
. Q . . . . . .
. . . Q . . . .
. . . . . Q . .
. . Q . . . . .

Queens = array(1, 5, 8, 6, 3, 7, 2, 4) .

For some reason the computation times in SWI-prolog 9.2.6 seem 30% higher than in version 9.2.2 but still with no significant difference between the use of arrays or lists in these examples.

?- benchmark.
queens(100,_3382172)
% 4,536,855 inferences, 0.422 CPU in 0.491 seconds (86% CPU, 10754180 Lips)
queens_list(100,_5922054)
% 4,483,345 inferences, 0.422 CPU in 0.484 seconds (87% CPU, 10633742 Lips)
queens_distinct(100,_6780512)
% 64,402,829 inferences, 4.425 CPU in 4.848 seconds (91% CPU, 14553427 Lips)
queens_distinct_list(100,_2521402)
% 64,375,035 inferences, 4.359 CPU in 4.765 seconds (91% CPU, 14769059 Lips)
queens(8,_2681598),fail
% 763,684 inferences, 0.057 CPU in 0.065 seconds (88% CPU, 13352053 Lips)
queens_list(8,_2682252),fail
% 763,075 inferences, 0.063 CPU in 0.072 seconds (88% CPU, 12065381 Lips)
queens_distinct(8,_2682906),fail
% 4,220,968 inferences, 0.436 CPU in 0.493 seconds (88% CPU, 9686607 Lips)
queens_distinct_list(8,_8578),fail
% 4,218,651 inferences, 0.386 CPU in 0.438 seconds (88% CPU, 10931781 Lips)
queens_sym(8,_8496),fail
% 674,986 inferences, 0.062 CPU in 0.073 seconds (85% CPU, 10862693 Lips)
queens_sym_list(8,_9150),fail
% 670,198 inferences, 0.063 CPU in 0.073 seconds (87% CPU, 10623225 Lips)
queens_sym_distinct(8,_143930),fail
% 1,518,491 inferences, 0.151 CPU in 0.174 seconds (87% CPU, 10060163 Lips)
queens_sym_distinct_list(8,_33578),fail
% 1,565,125 inferences, 0.147 CPU in 0.167 seconds (88% CPU, 10642186 Lips)
fourier(2,_24502,_24504,1)
% 2,821 inferences, 0.001 CPU in 0.001 seconds (81% CPU, 3374402 Lips)
true.

Symbolic answer constraints not only ground solutions:

fourier(P, X, Y, F):-
    float_array(Force_Leg, [3], 0..F),
    {Force_Leg[1]+Force_Leg[2]+Force_Leg[3] = P,
     X*P = 20*Force_Leg[2],
     Y*P = 20*Force_Leg[3]}.

?- fourier(3, X, Y, 1).
X = Y, Y = 6.666666666666667.

?- fourier(3.1, X, Y, 1).
false.

?- fourier(2, X, Y, 1).
{Y=20.0-10.0*_A-10.0*_B, X=10.0*_B, _=2.0-_A-_B, _A+_B>=1.0, _B=<1.0, _A=<1.0}.
 show(+Queens)
pretty prints chessboard Queens (either array or list).
 benchmark
computation time benchmark of problems used in FLOPS paper for comparing bounded quantifiers on subscripted variables, to recursion on lists.

Undocumented predicates

The following predicates are exported, but not or incorrectly documented.

 queens(Arg1, Arg2)
 queens_distinct(Arg1, Arg2)
 queens_sym(Arg1, Arg2)
 sym_elim(Arg1, Arg2)
 queens_sym_distinct(Arg1, Arg2)
 queens_list(Arg1, Arg2)
 queens_distinct_list(Arg1, Arg2)
 queens_sym_list(Arg1, Arg2)
 sym_elim_list(Arg1, Arg2)
 queens_sym_distinct_list(Arg1, Arg2)
 fourier(Arg1, Arg2, Arg3, Arg4)