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
8
votes
3 answers

Example channelling constraints ECLiPSe

Can someone provide a simple example of channelling constraints? Channelling constraints are used to combine viewpoints of a constraint problem. Handbook of Constraint Programming gives a good explanation of how it works and why it can be…
Stanko
  • 4,275
  • 3
  • 23
  • 51
8
votes
2 answers

CLPFD and infinite countable domains

I #> 0, I #< 10, indomain(I). The previous code obviously does the following: I = 1 ; I = 2 ; I = 3 ; I = 4 ; I = 5 ; I = 6 ; I = 7 ; I = 8 ; I = 9. The following code does not work (arguments are not sufficiently instantiated): I #> 0,…
Fatalize
  • 3,513
  • 15
  • 25
8
votes
2 answers

I don't understand what label does in Prolog

I went through the manual and documentation but still don't understand. I'm trying to implement a sudoku solution where after writing out all the other rules of the game, I've added label(Board) according to my teacher's instructions. However I…
NewGuy
  • 89
  • 1
  • 5
8
votes
3 answers

Board Assembly with constraints

I am doing this problem but I am completely new to Prolog and I have no idea how to do it. Nine parts of an electronic board have square shape, the same size and each edge of every part is marked with a letter and a plus or minus sign. The parts are…
Wajahat
  • 1,593
  • 3
  • 20
  • 47
8
votes
3 answers

Prolog : Learning by example

I am trying to learn a little bit about swi-prolog (beyond the basic, useless programs). Can anyone explain (perhaps in pseudocode) what this sudoku solver and the related functions are doing? If you need more reference it is found in the CLP(FD)…
Ahmad P.
  • 89
  • 1
  • 2
7
votes
4 answers

Splitting a list of integers into a list of positive integers and a list of negative integers

I've been trying to create a predicate in Prolog which splits a list of integers into a list of positive integers and into a list of negative integers. Sample query with expected result: ?- split([1,-2,3,4,-8],X,Y). X = [1,3,4], Y = [-2,-8]. This…
Martin
  • 202
  • 3
  • 7
7
votes
2 answers

Square Puzzle Problem Solution with Constraint Programming

Question: Fill in the grid with squares (of any size) that do not touch or overlap, even at the corners. The numbers below and at the right indicate the number of grid squares that are filled in the corresponding column / row. To solve this…
Educorreia
  • 375
  • 5
  • 21
7
votes
1 answer

Mutual exclusivity in CLP(FD)

I am writing a Prolog program using clp(fd) and am having difficultly implementing one of my desired constraints. The output is a list of integers (the length is dependent on the input to another part of the program), where there are certain pairs…
maxdh
  • 91
  • 8
7
votes
2 answers

convert float to integer in prolog

How to convert float to integer in prolog? I tried: ?- integer(truncate(sqrt(9))). false. ?- integer(round(sqrt(9))). false.
Martynas
  • 2,545
  • 7
  • 30
  • 36
7
votes
1 answer

What is #= in Prolog

The operator #= is mentioned on some page, e.g. https://www.metalevel.at/prolog but not on most other pages, e.g.: http://www.swi-prolog.org/pldoc/man?section=operators What does this operator mean?
rnso
  • 23,686
  • 25
  • 112
  • 234
7
votes
4 answers

Solving Tower of Hanoi declaratively (Prolog)

My professor gave this as an example of Prolog. It is a program that solves the Tower of Hanoi puzzle, where you have to move a stack of disks to another peg by moving one disk after the other, without putting a bigger disk on top of a smaller disk.…
Bart Louwers
  • 873
  • 9
  • 28
7
votes
1 answer

Solving Kakuro puzzle (5x5) in Prolog

Assuming that: A+B+C=24 E+F+G=11 J+K+L=22 N+O+P=14 A+E=17 B+F+J+N=26 C+G+K+O=15 L+P=13 How could i find a possible solution to the problem, given the constraints above, using the predicate solve/1? My first attempt was below, with no result.…
Alex Nikas
  • 83
  • 8
7
votes
3 answers

PROLOG all different

I have a very weird problem with PROLOG. I have used it before, but it's been a while and I'm rusty. I have a list of variables and I need to ensure that none of them are the same. I have tried: use_module(library(bounds)). all_different(A, B, C,…
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
7
votes
1 answer

Expressing setup time with cumulatives

There are many families of scheduling problems. I'm looking into a problem where I have families of jobs/tasks where the transition from one family to another family require reconfiguring the machine (setup time). I'm using cumulatives[2/3] to solve…
7
votes
2 answers

Implementing XOR function with Prolog CLPFD for 32-bit numbers

I try to implement efficient exclusive-or (XOR) in Prolog CLPFD. This should be simple predicate like: xor(A, B, AxorB). A, B, AxorB are natural numbers (with 0) and AxorB is a result of A xor B. My main problem is with efficiency. Firstly, I…
Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40
1 2
3
34 35