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
44
votes
6 answers

Tennis match scheduling

There are a limited number of players and a limited number of tennis courts. At each round, there can be at most as many matches as there are courts. Nobody plays 2 rounds without a break. Everyone plays a match against everyone else. Produce the…
Ingdas
  • 1,406
  • 10
  • 21
37
votes
5 answers

Prolog Constraint Processing : Packing Squares

I'm trying to solve a constraint processing problem in prolog. I need to pack 4 squares of 5x5,4x4,3x3 and 2x2 in a grid of 10x10. They may not overlap. My variables look like this: Name: SqX(i), i=1..10, domain: 1..10 Where X is either 5,4,3 or 2.…
Sven
  • 1,143
  • 1
  • 11
  • 19
25
votes
4 answers

Prolog - Arguments are not sufficiently instantiated

I am writing a little program which counts how many elements in a list are not numbers. Here is my code: not_number([],0). not_number([X|T],R):- not(number(X)), R1 is R+1, not_number(T,R1). not_number([_|Tail],Result):- …
Eddwhis
  • 984
  • 2
  • 14
  • 31
24
votes
9 answers

Solving N-Queens Problem... How far can we go?

The N-Queens Problem: This problem states that given a chess board of size N by N, find the different permutations in which N queens can be placed on the board without any one threatening each other. My question is: What is the maximum value of N…
user220751
22
votes
3 answers

Most general higher-order constraint describing a sequence of integers ordered with respect to a relation

In CLP(FD), we frequently need to state: "This is a list of integers and finite domain variables in (sometimes: strictly) ascending/descending order." Is there any CLP(FD) system that provides a general (parametrisable) built-in constraint for this…
mat
  • 39,707
  • 3
  • 42
  • 68
17
votes
5 answers

Programming for Young tableaux

A strange question follows: I'm doing a problem solving competition @ my school, and they allow us to use a computer. Since I'm the only one in the competition who knows how to code, I use C and Pascal programs to solve problems faster. I've done…
user2179983
  • 201
  • 2
  • 5
16
votes
4 answers

Reversible numerical calculations in Prolog

While reading SICP I came across logic programming chapter 4.4. Then I started looking into the Prolog programming language and tried to understand some simple assignments in Prolog. I found that Prolog seems to have troubles with numerical…
user8472
  • 3,001
  • 3
  • 30
  • 58
13
votes
6 answers

DCG and inversion of a list in Prolog

I'm trying to count the numer of inversions in a list. A predicate inversion(+L,-N) unifies N to the number of inversions in that list. A inversion is defined as X > Y and X appears before Y in the list (unless X or Y is 0). For example: ?-…
bli00
  • 1,624
  • 1
  • 9
  • 29
12
votes
3 answers

Optimizing pathfinding in Constraint Logic Programming with Prolog

I am working on a small prolog application to solve the Skyscrapers and Fences puzzle. An unsolved puzzle: A solved puzzle: When I pass the program already solved puzzles it is quick, almost instantaneous, to validate it for me. When I pass the…
F. P.
  • 4,708
  • 8
  • 49
  • 78
12
votes
2 answers

Remove incorrect subsequent solutions without once

I have a predicate that finds the correct solution but then goes on to find solutions which are not right. ?- data(D),data_threshold_nonredundantbumps(D,5,Bs),write(D). [3,6,7,8,2,4,5,6,9,4,7,3] D = [3, 6, 7, 8, 2, 4, 5, 6, 9|...], Bs = [bump([11],…
user27815
  • 4,687
  • 11
  • 28
12
votes
4 answers

Inverse factorial in Prolog

Can someone helping me to find a way to get the inverse factorial in Prolog... For example inverse_factorial(6,X) ===> X = 3. I have been working on it a lot of time. I currently have the factorial, but i have to make it reversible. Please help…
sonchi
  • 119
  • 3
11
votes
3 answers

Prolog Noob : Constraint Programming library or syntax issue in SWI-Prolog

I'm just trying to figure out constraint programming in SWI-Prolog, looking at this tutorial : http://en.wikibooks.org/wiki/Prolog/Constraint_Logic_Programming However I seem to be falling at the first hurdle. ?-…
interstar
  • 22,620
  • 31
  • 101
  • 161
11
votes
3 answers

Formulating Effect axiom

How to write correctly the effect axiom for empty(b,t)-action using the predicate contains(b,l,t) The predicate evaluates True , if the bucket b holds l liters of water at time t. empty(b,t): completely empties bucket b at time t. The effect of the…
Mensch
  • 419
  • 3
  • 16
11
votes
3 answers

List inequality constraint

I am trying to write a Prolog (CLP) predicate that would build a constraint constraining inequality of two lists. More formally, having two lists A=[A1,...,AN], B=[B1,...,BN] the constraint is defined as (A1 #\= B1) #\/ (A2 #\= B2) #\/ ... #\/ (AN…
mscavnicky
  • 145
  • 1
  • 7
11
votes
4 answers

I'm curious if Logic Programs can do algebra

I read a brief article about Prolog and Logic Programming. I'm curious if Logic Programs can do algebra. Like would you be able to ask what the Variable of X is in the equation 5+X = 7 and get an answer of -2?
James Scourtos
  • 701
  • 5
  • 7
1
2 3
32 33