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

484 questions
10
votes
5 answers

Simple prolog program. Getting error: >/2: Arguments are not sufficiently instantiated

I made a Prolog predicate posAt(List1,P,List2) that tests whether the element at position P of List1 and List2 are equal: posAt([X|Z], 1, [Y|W]) :- X = Y. posAt([Z|X], K, [W|Y]) :- K > 1, Kr is K - 1, posAt(X, Kr, Y). When testing: …
user1279812
  • 125
  • 2
  • 2
  • 7
10
votes
2 answers

NP-complete knapsack

I saw this ECLiPSe solution to the problem mentioned in this XKCD comic. I tried to convert this to pure Prolog. go:- Total = 1505, Prices = [215, 275, 335, 355, 420, 580], length(Prices, N), length(Amounts, N), …
Ashley
  • 833
  • 1
  • 5
  • 15
10
votes
4 answers

reversible "binary to number" predicate

What is the best way to convert binary bits (it might be a list of 0/1, for example) into numbers in a reversible way. I've written a native predicate in swi, but is there better solution ? Best regards
10
votes
2 answers

SWI-Prolog CLPFD

I'm new to prolog for constraint programming. I have an issue with CLPFD not reducing a domain as I expect it to. This is probably really simple. [A,B] ins 1..5,A*B#=5. I expect it to reduce the domain of A and B to 1\/5 But it just gives A in…
ecky
  • 119
  • 3
10
votes
4 answers

Using a constrained variable with `length/2`

Here is the problem: $ swipl Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.6-5-g5aeabd5) Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are…
user1812457
10
votes
6 answers

Counter-intuitive behavior of min_member/2

min_member(-Min, +List) True when Min is the smallest member in the standard order of terms. Fails if List is empty. ?- min_member(3, [1,2,X]). X = 3. The explanation is of course that variables come before all other terms in the standard order of…
user1812457
10
votes
6 answers

How to duplicate the behavior of predefined length/2 in SWI-Prolog?

I'm trying to duplicate the behavior of the standard length/2 predicate. In particular, I want my predicate to work for bounded and unbounded arguments, like in the example below: % Case 1 ?- length(X, Y). X = [], Y = 0 ; X = [_G4326], Y = 1 ; X =…
damisan
  • 977
  • 4
  • 16
10
votes
2 answers

How do I get the sum of given numbers in prolog?

I'm new to prolog and I'm doing some exercises for practice. So I'm trying to get the sum of the given numbers in a list. I'm trying to use this: my_last(X, [X]). my_last(X, [_|L]) :- my_last(X, L). (from here) as my guide. So this is my code for…
Zik
  • 1,376
  • 6
  • 20
  • 31
9
votes
4 answers

Prolog: Random Labeling

I have a program written in Sicstus Prolog using constraints. My goal is to use labeling/2 and some other method to obtain a random instantiation of my variables. Example: X #> 2, Y #= 2*X, Z #<10 If I use List = [X,Y,Z], labeling([], List) The…
ecc
  • 1,409
  • 16
  • 37
9
votes
5 answers

Prolog; try to make fibonacci more effective?

This logic programming is really making a lap dance on my imperative programming skills. This is homework, so please just don't drop me the answer. This is what I have: fibo(N,1) :- N < 2, !. fibo(N,R) :- N1 is N-1, N2 is N-2, …
Algific
  • 1,300
  • 2
  • 17
  • 32
9
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
  • 3,209
  • 3
  • 16
  • 45
9
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,433
  • 13
  • 23
9
votes
1 answer

CLP(FD)-ying Simultaneous Recursion for Fibonacci Lukas Numbers Possible?

There are some instances where recursive predicates can be CLP(FD)-fied with the benefit that the predicate turns bidirectional. What are the limits of this method? For example can the following computation CLP(FD)-fied: Fn: n-th Fibonacci…
Mostowski Collapse
  • 12,118
  • 3
  • 34
  • 78
9
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
  • 99
  • 1
  • 5
9
votes
4 answers

Prolog - handling binary data with DCGs

It seems to me one ought to be able to process binary data with DCGs on a list of bytes. To make it work generally, though, one has to use bitwise operations which means is/2 is involved, which means instantiation order is an issue, which may…
Daniel Lyons
  • 21,545
  • 2
  • 48
  • 73
1
2
3
32 33