Skip to content

Commit

Permalink
use Scryer Prolog for CLP(B) examples
Browse files Browse the repository at this point in the history
  • Loading branch information
triska committed Apr 21, 2022
1 parent aa96103 commit 8a0717f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 40 deletions.
15 changes: 10 additions & 5 deletions clpb/independent.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
:- use_module(library(clpb)).
:- use_module(library(lists)).
:- use_module(library(assoc)).
:- use_module(library(pairs)).
:- use_module(library(time)).
:- use_module(library(format)).

:- include(edges).
:- use_module(edges).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Independent set:
Expand All @@ -9,13 +14,13 @@
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

%?- independent_set(Sat), time(sat_count(Sat, Count)).
%@ % 14,002,141 inferences, 1.563 CPU in 1.575 seconds (99% CPU, 8956017 Lips)
%@ Sat = *(...),
%@ Count = 211954906.
%@ % CPU time: 6.306s
%@ Sat = *(...), Count = 211954906

independent_set(*(NBs)) :-
findall(U-V, edge(U, V), Edges),
setof(U, V^(member(U-V, Edges);member(V-U, Edges)), Nodes),
findall(U, (member(U-V, Edges);member(V-U, Edges)), Nodes0),
sort(Nodes0, Nodes),
pairs_keys_values(Pairs, Nodes, _),
list_to_assoc(Pairs, Assoc),
maplist(not_both(Assoc), Edges, NBs).
Expand Down
55 changes: 36 additions & 19 deletions clpb/kernels.pl
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
:- use_module(library(clpb)).
:- use_module(library(clpfd)).
:- use_module(library(clpz)).
:- use_module(library(lists)).
:- use_module(library(dcgs)).
:- use_module(library(between)).
:- use_module(library(pairs)).
:- use_module(library(time)).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Independent sets, maximal independent sets (kernels) and maximal
independent sets with maximum weight of the Cycle graph C_100.
Example queries:
Example queries, tested with Scryer Prolog:
(1) Independent sets:
?- time((independent_set(I,_),sat_count(I,N))).
%@ % 2,101,289 inferences, 0.194 CPU
%@ I = *(...),
%@ N = 792070839848372253127.
%@ % CPU time: 0.999s
%@ I = *(....), N = 792070839848372253127
%@ ; % CPU time: 0.000s
%@ false.
(2) Maximal independent sets:
?- time((kernel(K,_),sat_count(K,N))).
%@ % 10,480,391 inferences, 1.210 CPU
%@ K = *...* *...,
%@ N = 1630580875002.
%@ % CPU time: 3.893s
%@ K = *(...)* *(...), N = 1630580875002
%@ ; % CPU time: 0.000s
%@ false.
(3) Maximal independent sets with maximum weight:
%?- time(maximum_thue_morse_kernel(Is, Negatives, Max)).
%@ % 28,998,719 inferences, 3.636 CPU
%@ Is = [1, 3, 6, 9, 12, 15, 18, 20, 23|...],
%@ Negatives = [1, 25, 41, 73, 97],
%@ Max = 28 .
%@ % CPU time: 122.740s
%@ Is = [1,3,6,9,12,15,18,20,23,25,27,30,33,36,39,41,43,46,48,51,...], Negatives = [1,25,41,73,97], Max = 28
%@ ; ... .
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

Expand All @@ -54,7 +60,7 @@
For example, the Thue-Morse weights of the integers 1,...,10 are:
?- thue_morse_weights(10, Ms).
%@ Ms = [-1, -1, 1, -1, 1, 1, -1, -1, 1|...].
%@ Ms = [-1,-1,1,-1,1,1,-1,-1,1,1].
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

Expand Down Expand Up @@ -101,11 +107,22 @@

key_one(1-_).

include(_, [], []).
include(Goal_1, [E|Es], Ls) :-
( call(Goal_1, E) ->
Ls = [E|Rs]
; Ls = Rs
),
include(Goal_1, Es, Rs).

%?- time(maximum_thue_morse_kernel(Is, Negatives, Max)).
%@ % 32,886,119 inferences, 6.049 CPU in 6.068 seconds (100% CPU, 5436963 Lips)
%@ Is = [1, 3, 6, 9, 12, 15, 18, 20, 23|...],
%@ Negatives = [1, 25, 41, 73, 97],
%@ Max = 28 .
%@ % CPU time: 124.214s
%@ Is = [1,3,6,9,12,15,18,20,23,25,27,30,33,36,39,41,43,46,48,51,...], Negatives = [1,25,41,73,97], Max = 28
%@ ; % CPU time: 0.039s
%@ Is = [1,3,6,9,12,15,18,20,23,25,27,30,33,36,39,41,43,46,48,51,...], Negatives = [1,25,41,73,94], Max = 28
%@ ; % CPU time: 1.020s
%@ Is = [1,3,6,9,12,15,18,20,23,25,27,30,33,36,39,41,43,46,48,51,...], Negatives = [1,25,41,73,97], Max = 28
%@ ; ... .

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Independent set:
Expand Down Expand Up @@ -148,5 +165,5 @@
u_to_var(Assoc, Node, Var) :- get_assoc(Node, Assoc, Var).

%?- kernel(Sat, _), sat_count(Sat, C).
%@ Sat = ~ (... + ... + ... * ... + _G117*_G118+_G118*_G117+_G118*_G119+_G119*_G118+_G20*_G119+_G119*_G20)* (... * ... * (... + ...)* (_G113+ (... + ...))* (_G114+ (... + ... + _G115))* (_G115+ (0+_G114+_G116))* (_G116+ (0+_G115+_G117))* (_G117+ (0+_G116+_G118))* (_G118+ (0+_G117+_G119))* (_G119+ (0+_G20+_G118))),
%@ C = 1630580875002.
%@ Sat = *(...)* *(...), C = 1630580875002
%@ ; ... .
37 changes: 21 additions & 16 deletions clpb/matchsticks.pl
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
:- use_module(library(clpfd)).
:- use_module(library(clpz)).
:- use_module(library(clpb)).
:- use_module(library(between)).
:- use_module(library(pairs)).
:- use_module(library(format)).
:- use_module(library(lists)).
:- use_module(library(dcgs)).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Task: Remove minimum number of matchsticks so that no subsquare remains.
Task: Remove minimum number of matchsticks so that no subsquare remains.
Tested with Scryer Prolog.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -26,20 +33,18 @@
?- matchsticks(L, Vs),
same_length(Vs, Coeffs), maplist(=(1), Coeffs),
weighted_maximum(Coeffs, Vs, Maximum),
maplist(writeln, L).
%@ [x,1,x,0,x,1,x,1,x]
%@ [1,o,1,o,1,o,1,o,1]
%@ [x,0,x,1,x,0,x,0,x]
%@ [1,o,1,o,1,o,1,o,1]
%@ [x,1,x,0,x,0,x,1,x]
%@ [1,o,1,o,1,o,1,o,1]
%@ [x,0,x,1,x,1,x,0,x]
%@ [1,o,1,o,0,o,1,o,1]
%@ [x,1,x,1,x,1,x,1,x]
%@ L = [[x, 1, x, 0, x, 1, x, 1|...], ..., [...|...]],
%@ Vs = [1, 1, 0, 1, 0, 1, 1, 1, 0|...],
%@ Coeffs = [1, 1, 1, 1, 1, 1, 1, 1, 1|...],
%@ Maximum = 31 .
maplist(portray_clause, L).
%@ [x,1,x,0,x,1,x,1,x].
%@ [1,o,1,o,1,o,1,o,1].
%@ [x,0,x,1,x,0,x,0,x].
%@ [1,o,1,o,1,o,1,o,1].
%@ [x,1,x,0,x,0,x,1,x].
%@ [1,o,1,o,1,o,1,o,1].
%@ [x,0,x,1,x,1,x,0,x].
%@ [1,o,1,o,0,o,1,o,1].
%@ [x,1,x,1,x,1,x,1,x].
%@ L = [[x,1,x,0,x,1,x,1,x]|...], Vs = [1,1,0,1,0,1,1,1,0,1,1,0,1,1,1,1,0,1,0,1,...], Coeffs = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,...], Maximum = 31
%@ ; ... .
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

matchsticks(L, Vs) :-
Expand Down

0 comments on commit 8a0717f

Please sign in to comment.