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
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
  • 562
  • 2
  • 12
0
votes
1 answer

Creating a list of lists in Prolog

I want to have a list of lists with constraints, here's my code written in SWI-Prolog: List = [L1,L2,L3], L1 = [X1, X2], L1 ins 1..4, L2 = [Y1, Y2], L2 ins 1..4, L3 = [Z1, Z2], L3 ins 1..4. But, it gives ERROR:Type Error: integer expected.
dev
  • 2,274
  • 7
  • 23
  • 46
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
  • 562
  • 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,097
  • 1
  • 15
  • 36
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,213
  • 1
  • 15
  • 22
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
  • 633
  • 9
  • 13
0
votes
1 answer

optimised minimum value using findall/3

I am trying to optimise and find the minimum Cost of a function. The program below uses findall/3 to iterate over all possible options of values which are generated from using the clpfd library provided by SWI-Prolog. There are several Cost values…
Namit
  • 1,264
  • 7
  • 18
  • 34
0
votes
1 answer

Porting ECLiPSe to Prolog

I've solved a problem about the assignment of the articles in a conference, using ECLiPSe. The goal is: similar articles should be in the same sessions. This is the solution that works in ECLiPSe: :- lib(fd). paper(1, 'An Empirical Study on…
user840718
  • 1,315
  • 6
  • 21
  • 45
0
votes
1 answer

Generating sudoku 'boxes' from rows - prolog

I'm trying to write a simple program that just checks whether or not an input sudoku board is currently incorrect; i.e. it has two of the same numbers in a row, column or 'box'. I haven't run into any trouble with the rows and columns part - a…
user1257768
  • 149
  • 1
  • 7
-1
votes
1 answer

Is there a possible implementation for this problem?

Having a bit of trouble doing research and trying to solve this problem in PROLOG. I have to implement a cryptarithmetic-puzzle solver able to solve any possible cryptarithmetic-puzzle. I'll explain in a bit more detail below. Given as input a list…
-1
votes
2 answers

Checking equal digits of numbers between lists?

What would be an efficient way to check how many equal ending digits have numbers between lists in prolog? For example we have Lista = [4432,2345,3243] and Listb = [3345,3232]. In these two lists we have 4432 and 3232 which have 2 same ending…
user11584054
-1
votes
3 answers

How to sum every second digit in list - Prolog?

Given some list of integers, I want to calculate the sum of every second element in the list using Prolog ? E.g.: [1,2,3,4] => [2+4] = 6
-1
votes
2 answers

Prolog constraint

I have 3 Constraints for a List: list ins 1..9 all_different(list) lists in the list --> I get some lists from the list. every list from the list has to fulfill the constraint to be gapless. for example: List1 = [1,3,2,4]; List2=[3,2,1]; List3=…
Hans
  • 119
  • 1
  • 12
-1
votes
1 answer

Prolog: "false" with query

I'm trying to solve a 9x9 sudoku puzzle in SWI-Prolog, I've given the query which is the empty sudoku, but it doesn't give any errors it simply states "false". The code also compiles correctly. My code is shown below: :-…
marcusvb
  • 51
  • 8
1 2 3
32
33