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

Custom heuristic in ECLiPSe CLP

Consider the following puzzle: A cell is either marked or unmarked. Numbers along the right and bottom side of the puzzle denote the total sum for a certain row or column. Cells contribute (if marked) to the sum in its row and column: a cell in…
svdc
  • 154
  • 2
  • 10
0
votes
2 answers

Prolog 'how to return' a value

I known prolog doesn't return values but i need to update the value of some variable and get it in console with this procedure: max(A,B,C) :- (A>B -> C is A ; C is B). …
colymore
  • 11,776
  • 13
  • 48
  • 90
0
votes
3 answers

clpfd - generate the list of all integers between 5 and 10

I'm working with SWI-Prolog to get clpfd generate the list of all distinct integers between 5 and 10: q1(Answer) :- length(Xs, Answer), Xs ins 0..20, chain(Xs, #<), maplist(q1constraints, Xs). q1constraints(X) :- X #>= 5, X #=<…
0
votes
0 answers

Different variables - prolog puzzle solver

I'm developing a solution based on prolog restrictions. Basically I got to rearrange 9 pieces with dots in it to make a dice. I have 9 pieces and 7 are all different but 2 are equal. I want to make a restriction that says it and I got this: A #=…
Perseverance
  • 337
  • 5
  • 18
0
votes
1 answer

Arguments are not sufficiently instantiated on list sum

I am trying to constraint the sum of a list, but my code fails at label(). .pl: :- use_module(library(clpfd)). solve(L, Dim) :- length(L, 5), % define 5 diagonals SkipVars is Dim - 2, % to skip variables init_sublists_above_center(L,…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0
votes
1 answer

How can I prevent this Prolog code to give multiple duplicate answer?

I am learning Prolog. I am working on an assignment. I have already produced some code, which is partially working. But, somehow it is giving multiple duplicate answers. The question is: % Every letter represents a Digit (0,...,9). % Leading digits…
Srijani Ghosh
  • 3,935
  • 7
  • 37
  • 68
0
votes
1 answer

Not sufficiently instantiated for maplist(all_distinct, list)

I'm not able to run this code, what exactly do I have to say about the list to allow maplist/2 to run all_distinct/1? Solution = [A, B, C, D, E, F, G, H, I], Solution ins 1..9, maplist(all_distinct, Solution). I get ERROR: Arguments are not…
jboulter11
  • 152
  • 1
  • 2
  • 12
0
votes
1 answer

Manipulate labeling/2 output PROLOG

I'm developing a PROLOG program with restrictions to find the solutions for a certain game. Question#1: For debugging purposes, I want to count the number of possible solutions of the game. Is there any way to do this via the labeling/2…
GRoutar
  • 1,311
  • 1
  • 15
  • 38
0
votes
1 answer

Prolog syntax error on format

I am getting a syntax error on my format line and i have no idea why! magic3(Variables):- Variables[A,B,C,D,E,F,G,H,I], fd_domain(Variables,1,9), fd_all_different(Variables), A+B+C #= A+D+G, A+B+C #= A+E+I, A+B+C #= C+F+I, A+B+C…
0
votes
1 answer

Endtime for machine in cumulatives

I'm working on a scheduling problem which I have used cumulatives/3 to model. Ex: s1(Ss, Es, Ms ) :- Ss = [S1,S2,S3,S4,S5,S6,S7], Es = [E1,E2,E3,E4,E5,E6,E7], Ms = [M1,M2,M3,M4,M5,M6,M7], domain(Ss, 1, 30), domain(Es, 1, 30), …
MortenM
  • 522
  • 2
  • 12
0
votes
2 answers

Prolog, testing labeling heuristics

I'm working on some experiments for comparing different labeling heuristics in Sicstus Prolog. But I keep getting into 'Resource error: insufficient memory'. I'm pretty sure I'm doing something wrong in my testcode. The following code will replicate…
MortenM
  • 522
  • 2
  • 12
0
votes
1 answer

Difference between matrix column and use of all_different

I'm trying to express a relation on transition from one element in a list-of-lists, to another element. What I want to be able to do, is to say that there should exist a certain difference between two arbitrary elements. If we have the list …
MortenM
  • 522
  • 2
  • 12
0
votes
1 answer

How does `random_variable `random_value` work in SWI-Prolog's labeling/2?

I've seen it's possible to label cplfd variables using a random method by adding the following options to labeling/2: labeling([random_variable(N),random_value(M)],List). Where M and N are supposed to be integers, I think. However I am not able to…
Carles Araguz
  • 1,157
  • 1
  • 17
  • 37
0
votes
1 answer

Convert variable in Prolog

I have a program which result is a term equal(add(num(2),var(x)),num(3)). Basically that term is a conversion from statement 2+x = 3. I wish to parse that term to a CLP term so I can have result like x = 1. How do I assign variable to term var, such…
IllSc
  • 1,419
  • 3
  • 17
  • 24
0
votes
1 answer

Restricting Variable Domain without CLPFD Library

I have a list of variables in my program, say A, B, C, D, ..., J, and I need to restrict the domain of each of the variables to the same set of integers, say 1, 2, ... 10. I know of a few different ways to do this, but all of them use at least one…
Free
  • 663
  • 9
  • 13
1 2 3
34
35