Questions tagged [clpfd]

CLP(FD), which stands for Constraint Logic Programming over Finite Domains, implements declarative integer arithmetic in Prolog systems. It is a pure and general replacement of lower-level arithmetic predicates and allows you to efficiently solve combinatorial problems such as planning, scheduling and allocation tasks.

CLP(FD) stands for Constraint Logic Programming over Finite Domains. Finite domain constraints are relations over integer expressions and can be used to model and solve a large variety of combinatorial problems such as planning, scheduling and allocation tasks. They are also used to obtain general and pure integer arithmetic in Prolog.

Almost all modern Prolog implementations include a CLP(FD) solver, which is either already an integral part of the system (examples: GNU Prolog and B-Prolog) or available as a library (examples: SICStus Prolog, SWI-Prolog, YAP).

The most basic use of CLP(FD) constraints is evaluation of integer expressions. For example:

?- X #= 3+5.
X = 8.

In contrast to lower-level predicates, CLP(FD) constraints can be used in all directions. For example:

?- 8 #= 3+X.
X = 5.

CLP(FD) constraints can also be used if no concrete value can yet be deduced. Here is an example of using finite domain constraints in SWI-Prolog, after loading library(clpfd) (by entering use_module(library(clpfd)). at the interactive toplevel). We ask for positive integers X and Y whose sum is 15:

?- X + Y #= 15, X #> 0, Y #> 0.

The constraint solver answers as follows:

X in 1..14,
X+Y#=15,
Y in 1..14.

In this case, the CLP(FD) solver has deduced that both variables must be integers between 1 and 14.

When binding one of the variables to concrete integers with the built-in predicate indomain/1, the constraint solver automatically deduces a binding for the other variable so that all constraints are satisified:

?- X + Y #= 15, X #> 0, Y #> 0, indomain(X).
X = 1, Y = 14 ;
X = 2, Y = 13 ;
X = 3, Y = 12 ;
etc.

To use it, you have to import this library (in SWI Prolog):

:- use_module(library(clpfd)).

Implementations

525 questions
5
votes
0 answers

How to dynamically rewrite CLP(FD) constraints to help reification

My question is linked to this (now 1-year-old) post regarding reification issues in a CLP(FD) program : link The prolog file I give to the SWI engine is programmatically written on-the-fly, based on some data that users can add/edit/remove with…
M.V.
  • 177
  • 9
5
votes
2 answers

Non-termination when generating lists of arbitrary length in CLP(FD)

Why does the following exits with a ERROR: Out of global stack after return the expected answers? ?- L #>= 0, L #=< 3, length(X, L). L = 0, X = [] ; L = 1, X = [_G1784] ; L = 2, X = [_G1784, _G1787] ; L = 3, X = [_G1784, _G1787, _G1790] ; ERROR:…
Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92
5
votes
2 answers

SWI-Prolog and constraints, library CLP(FD)

I'm playing around with constraints in (swi) prolog using the clpfd library. I'm trying to identify when one set of constraints encapsulates or subsumes the other, e.g. X<4 encapsulates X<7 as whenever the former is true, the latter is true. This…
Nir
  • 61
  • 6
5
votes
3 answers

what algorithm for a scheduling program

I have this problem of scheduling tasks. Each task has a suggested start time T (it needs to start at [T-10, T+10]), takes L minutes to complete and uses a number of resources [R1, R2,...]. When a resource is being used, no other task can use it.…
Martin08
  • 20,990
  • 22
  • 84
  • 93
5
votes
2 answers

Prolog program to get an (integer) number as the sum of two integer squares, why does it not work?

I'm starting learning Prolog and I want a program that given a integer P gives to integers A and B such that P = A² + B². If there aren't values of A and B that satisfy this equation, false should be returned For example: if P = 5, it should give A…
Kevin
  • 685
  • 1
  • 7
  • 16
5
votes
3 answers

How to create arithmetic and disequality constraints in Prolog

I'm brand new to Prolog, and I'm interested in converting the following word problem into (SWI) Prolog: There are 4 children: Abe, Dan, Mary, and Sue. Their ages, in no particular order, are 3, 5, 6, and 8. Abe is older than Dan. Sue is younger…
Brent
  • 4,153
  • 4
  • 30
  • 63
5
votes
0 answers

Assigning people to beds - approaches to automate

I help with a youth camp every year. Assigning attendees to bedrooms is a massive task - there are 92 bedrooms and the event runs for a week, with attendees staying for varying lengths of time, and beds needing to be reused. It currently takes a…
PeterB
  • 2,212
  • 2
  • 21
  • 33
5
votes
3 answers

Mandatory reification when using the 'mod' operator together with 'or'?

I have written a CSP program using CLP(FD) and SWI-Prolog. I think I need to improve my constraints' writing when I use the mod operator together with #\/ in my predicates. A short example : :- use_module(library(clpfd)). constr(X,Y,Z) :- X in…
M.V.
  • 177
  • 9
5
votes
3 answers

SWI Prolog Clpfd Library - Reification

I have an upcoming Logic exam and have been studying some past papers from my course. I've come across a question regarding reification and have posted it below; Illustrate reification by using it to express the property that a variable B…
user6066919
5
votes
2 answers

List of integers and infinite loop in Prolog CLPFD

Suppose I want to represent integers like so: integer:Sign:[FirstDigit,SecondDigit,...]. For instance, 42 would be represented as integer:positive:[4,2]. I need a predicate that generates the value of the integer based on this representation and…
Fatalize
  • 3,513
  • 15
  • 25
5
votes
4 answers

Prolog: Arrangements of k elements with sum of elements S

I am trying to compute arrangements of K elements in Prolog, where the sum of their elements is equal to a given S. So, I know that arrangements can be computed by finding the combinations and then permute them. I know how to compute combinations of…
Nelly
  • 299
  • 3
  • 5
  • 16
5
votes
3 answers

Is it possible to declare an ascending list?

I can make lists of ascending integer like so: ?- findall(L,between(1,5,L),List). I know I can also generate values using: ?- length(_,X). But I don't think I can use this in a findall, as things like the following loop: ?-…
user27815
  • 4,767
  • 14
  • 28
5
votes
2 answers

Prolog: calculating OEIS A031877 ("nontrivial reversal numbers") using clp(FD)

Browsing through the awesome On-Line Encyclopedia of Integer Sequences (cf. en.wikipedia.org), I stumbled upon the following integer sequence: A031877: Nontrivial reversal numbers (numbers which are integer multiples of their reversals), excluding…
repeat
  • 18,496
  • 4
  • 54
  • 166
5
votes
4 answers

Excluding a tuples_in list in prolog

This is the problem in short: 16 children are to be seated in a 4 x 4 array of chairs. The children are 8 girls (numbered 1..8) and 8 boys (numbered 9..16). 1,3,5,8 think boys are yucky 9,10,11,14 think girls are gross these pairs are…
Vikram Venkat
  • 663
  • 4
  • 16
5
votes
1 answer

Constraint not propagated upon instantiation of list members

I am building a parser and generator for dates and times. In an ordinary programming language these would be written separately. In Prolog+CLP(FD) I can write 1 predicate that does both :-) In my use case it often makes sense to parse a number of…
Wouter Beek
  • 3,307
  • 16
  • 29