prolog,constraint-programming,clpfd,gnu-prolog

There is a library for Constrained Logic Programming in SWI-Prolog. It's called clpfd. :-use_module(library(clpfd)). Let's say that you'll have a variable for the length of the subsequence. Its domain goes from zero (corresponding to the empty subsequence) to the length of the list. In order to get the longest sequence...

prolog,swi-prolog,clpfd,declarative-programming

What you are doing is that you are making a list of variables, Solutions, and then Solutions ins 1..9 makes each variable an integer between 1 and 9. all_distinct/1 expects a list, not an integer. So, if you want a list of 9 distinct integers: ?- Solutions = [A,B,C,D,E,F,G,H,I], Solutions...

Your basic idea is sound. You indeed only need all_different/1 constraints. The interesting thing about this puzzle is how to represent the cubes. I shall take a straight-forward approach and represent the cubes in almost exactly the same way as given on the page you link to. For example, I...

By using the task_intervals(true) option on the cumulativespredicate the speed is really improved: cumulatives(Tasks, Machines, [bound(upper),task_intervals(true)] ) gives solvingtime of 2ms to the code in the original question without changing anyting else....

algorithm,prolog,constraints,clpfd

this query should satisfy your requirements ?- Vs = [_,_,_,_,_,_], Vs ins 1..4, [A,B,C,D] ins 1..2, global_cardinality(Vs, [1-A,2-B,3-C,4-D]), label(Vs). Vs = [1, 1, 2, 2, 3, 4], A = B, B = 2, C = D, D = 1 ; Vs = [1, 1, 2, 2, 4, 3], A =...

Let me first make the program easier to understand and also more general by using true arithmetic relations between integers instead of low-level arithmetic: :- use_module(library(clpfd)). fib(0, 0). fib(1, 1). fib(V, VF) :- V #> 1, B #= V - 1, C #= V - 2, VF #= BF +...

In several systems you need to put at the beginning of your file/module: :- use_module(library(clpfd)). And as a tiny improvement you can introduce the sum S: S #= A+B+C, S #= D+E+F, % this too! S #= G+H+I, .... And even better, you can now calculate the sum with is...

prolog,clpfd,failure-slice,prolog-dif

Tiny issue to start with — the name: sequences/1 suggests a list of sequences (whatever a sequence is), it should be rather sequence/1. You are demanding quite a lot of a poor Prolog system: You are demanding stronger consistency. At any price, I presume. My immediate reactio (use library(clpfd)!) does...

In the original, binary_number([], _, N, N)., the _ means you don't care what the value of the variable is. If you used, binary_number([], X, N, N). (not caring what X is), Prolog would issue a singleton variable warning. Also, what this predicate clause says is that when the first...

If you use the cumulatives/[2,3] constraint instead of the cumulative/1 constraint, then you will get the assigned machine for 'free'. By use of cumulatives each single machine can be given individual resource capacity. This shows your problem solved by use of cumulatives: :- use_module(library(clpfd)). :- use_module(library(lists)). go( Ss, Es, Ms)...

Think maybe you were too fast on your copy/paste in the ColX's. The first and second digits were switched in many cases. Try this: Col1 = [S11, S21, S31, S41, S51, S61, S71, S81, S91], Col2 = [S12, S22, S32, S42, S52, S62, S72, S82, S92], Col3 = [S13, S23,...

prolog,counter,clpfd,restrictions,labeling

For your question #1 you can use the findall/3 predicate: Ex: | ?- X=[X1,X2], domain(X,0,2), findall(X, labeling([],X), Xl ), length(Xl,L). X = [X1,X2], Xl = [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]], L = 9, X1 in 0..2, X2 in 0..2 ? yes | ?- ...

If you want to learn Prolog and CLP(FD) rapidly, use Prolog's top level shell to play around until you get comfortable with it. In fact, everything you need to know about CLP(FD) and Prolog can be explained there ; or almost. No need to write (what's their name?) files, everything...

This implements consecutive in the sense you gave in the comments. For a list of N values, we need space enough to make all the values fit in between, and all values need to be different. consecutive([]). % debatable case consecutive(Xs) :- Xs = [_|_], length(Xs, N), all_different(Xs), max_of(Max, Xs),...

Here is one way of expressing it. The only relevant global constraint that comes to mind is maximum/2. | ?- makespans([1,2,3,3,1,2,3], [7,7,4,11,12,15,15], [M1,M2,M3]). M1 = 12, M2 = 15, M3 = 15 ? % makespans(+Machines, +EndTimes, +MakeSpans) :- % given N tasks assigned to Machines and finishing at EndTimes, %...

Just answering, so that the question goes off the list of unanswered questions: The error could be also the (:-)/2, not only the missing (=)/2. So the following session wurks: Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.11) Copyright (c) 1990-2014 University of Amsterdam, VU Amsterdam ?- use_module(library(clpfd)). % library(occurs)...

+1 for using CLP(FD) constraints for this task! forall/2 and constraints do not mix very well, since backtracking revokes posted constraints. Your example works as expected with: flip_init(Prop, D) :- clpfd:init_propagator(D, Prop). and using maplist(flip_init(Prop), Ds) instead of forall/2. The next problem is then that digits_to_nonneg([1,2], N) simply fails, but...

I would refine your code, just to make it generic not_enemy(A, B) :- maplist(not_enemy(A,B), [[1,2], [4,6], [4,7], [4,9], [9,11], [12,14], [14,16]]). not_enemy(A,B,[X,Y]) :- X #= A #==> Y #\= B. I cannot find an appropriate way to use tuples_in to solve this problem....

prolog,swi-prolog,constraint-programming,clpfd

There is no constraint to express maximum difference. Nor any constraint to express a maximum1. To permit such a construct some form of quantification would be necessary. And/or problems with monotonicity would arise. However, there is something related: you might want to generate values such that the largest value is...

This problem naturally leads to an implementation using library(clpfd): :- use_module(library(clpfd)). digitsum(Z,S) :- Z #= 0, S #= 0. digitsum(Z0, S0) :- Z0 #> 0, S0 #> 0, S0 #= Z0 mod 10 + S1, Z1 #= Z0 // 10, % should be rather div digitsum(Z1, S1). ?- digitsum(D,S). D...

I think this is along the lines of what you're looking for... :- use_module(library(clpfd)). foo(L) :- Len in 1..9, % lists of length 1 through 9 length(L, Len), L ins 1..9, label(L), msort(L, LS), series(LS). series([_]). series([X,Y|T]) :- Y #= X + 1, series([Y|T]). Since you are already constraining the...

prolog,constraint-programming,clpfd

What I understand, your want to get concrete solutions (and not the domains). For this, use label/1 or labeling/2, which will give all the explicit solutions (via backtracking). In SWI-Prolog, these predicates are documented here: labeling/2 . label(List) is equivalent to labeling([],List). For this simple example, label(List) would suffice: ?-...