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
0
votes
1 answer

Constraining domain for variable through list

I have a list defined in one part of my program as people([tom,betty,sue,fred,charles,chay]) I would like to constraint a variable to be N values in this domain. Something like : setup(GroupCount) :- length(Group, GroupCount), people(X), …
ZM-
  • 94
  • 1
  • 9
0
votes
0 answers

Prolog CLP(FD) Number of items in result

I'm now solving a scheduling problem and I'm a bit stuck on the way how to describe number or count of certain items in result. For example, the result is a list of numbers between 1..7 and I want to at least see or better to define constraints on…
Jan Drozen
  • 894
  • 2
  • 12
  • 28
0
votes
1 answer

Prolog Lists - Duplicate head

I am trying to make a list of 2 consecutive terms from a list of terms. So echo should return True if L1 and L2 are lists and L2 contains each element in L1 twice in a row. Without clpfd. I want echo to take in... echo([x,1,[b]], L). and…
Steve Freed
  • 91
  • 1
  • 11
0
votes
1 answer

ECLiPSe Prolog - IC library: Lists of integers as variables

In the ic library, one can create a variable with a domain like so: X #:: [1..10] % Variable X with domain the integers from 1 to 10. It is also possible to create 2 variables with the same domain like so: [X,Y] #:: [1..10] How can I create 2…
0
votes
1 answer

Prolog predicate select combinations not in 1000 combinations

I have this specific problem but i can't formulate in Prolog syntax. I am very new. my finite domain : all 10-balls combinations from 70 balls requested solution : 10-balls combinations constraints : i have a list of 20 balls set : set1, set2 ..…
Bodstwo
  • 1
  • 1
0
votes
0 answers

PROLOG , Operator expected when X in 1 .. 10

whenever I try to set a domain for a variable , an operator expected error appears , here is my short piece of code , I'm using SWI-Prolog Version 7.6 use_module(library(clpfd)). corn(L):- L=[M,W,C], M in 0..100, W in 0..100, …
0
votes
0 answers

including a predicate that ends with a fail, in search tree

In my program I have a number of rules, each "firing" based on a different precondition and current state. One such rule's purpose is to identify a special illegal condition, that is known to occur, and then explicitly invoke backtracking via a…
user2193970
  • 345
  • 2
  • 10
0
votes
1 answer

Prolog Sudoku Solver, Solve any quadratic Sudoku, elements not distinct

Solving any quadratic Sudoku, so Sudoku of sizes 4,9,16,25... without the need of hard-coding those "blocks", the sub-units of your normal Sudoku field. Using SWI-Prolog and the clp(FD) library. Sudokus given in a format like this (list of…
Tomas By
  • 1,396
  • 1
  • 11
  • 23
0
votes
2 answers

relational prolog and bit mask manipulations

I am trying my hand at relational prolog. Part of my program needs to deal with bitmasks. It however seems that prolog code handles bit makes, such as to set a bit or clear a bit doesn't work relationally -- i.e. it only works in setting a bit, but…
user2193970
  • 345
  • 2
  • 10
0
votes
3 answers

Solving a logic riddle in Prolog

I'm in the "early phase" of learning prolog and came across a logic riddle that seams easy to implement: Link to the riddle | Link to solution We are looking for the 10-digit number which satisfies the following conditions: All digits from 0-9…
Lerrrtaste
  • 35
  • 1
  • 7
0
votes
1 answer

What is bounds propagation

I am trying to figure out what is bounds propagation in clpfd, but cannot seem to find a good explanation anywhere. I am revising for Prolog and clpfd and came across this question, but looking at the lecture notes it does not make sense to me.…
LRAC
  • 31
  • 1
  • 11
0
votes
1 answer

Why does prolog not backtrack after receiving a False?

I have this code that evaluates a list of numbers and returns lists matching the pattern [G,A],[A,B],[B,C],[C,D],[D,E],[E,F],[F,G]. However, I want the numbers to only to be unique numbers. EG: ([0,2],[2,4],[4,19],[19,3],[3,5],[5,7],[7,0]). The…
Jordan.McBride
  • 267
  • 2
  • 7
  • 20
0
votes
3 answers

How to get the max distance between two airplanes

I hope that someone can help me with the problem i have. I have 3 Airplanes A, B, C The Time intervals are: A[0..12], B[7..18], C[11..23]. Now i need to find the optimal time for every Airplane to land one after another The result should be this: ?-…
twistedhat
  • 55
  • 10
0
votes
0 answers

SICStus Prolog: Convert domain to string

I am trying to solve the map colouring problem and below is the solution and it seems to work. However, the domains can be represented only in integer from 1 to 4. I would like to have them in strings though, e.g. colours: {blue, green, yellow,…
Enigma
  • 305
  • 2
  • 11
0
votes
3 answers

PROLOG Undefined procedure ERROR (Two parameters recursion)

count([], 0, 0). count([X|T], M, N) :- 1 is X, count(T, MRec, NRec), M is MRec, N is NRec+1. count([X|T], M, N) :- 0 is X, count(T, MRec, NRec), M is MRec+1, N is…