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
5
votes
2 answers

Use reified constraints to make 3 numbers consecutive

Here's an outline of my SWI-Prolog program: :- use_module(library(clpfd)). consec1(L) :- L=[L1,L2,L3,L4,L5,L6,L7,L8,L9], L ins 1..9, ..., abs(L5-L4)#=1, all_different(L), labeling([],L) abs(L5-L4)#=1 makes L5 and L4 next to each…
mmgro27
  • 475
  • 1
  • 8
  • 18
5
votes
3 answers

Prolog Beginner: How to unify with arithmetic comparison operators or how to get a set var to range of values

I am new to Prolog. I need to write an integer adder that will add numbers between 0-9 to other numbers 0-9 and produce a solution 0-18. This is what I want to do: % pseudo code add(in1, in2, out) :- in1 < 10, in2 < 10, out < 18. I…
sixtyfootersdude
  • 25,859
  • 43
  • 145
  • 213
5
votes
3 answers

Create combinations of numbers within list prolog

I am developing a program in PROLOG (with restrictions) which is supposed to output a combination of 6 numbers within certain restrictions. The list must have the numbers from 1 to 4 and will, consequently, repeat 2 other numbers. It is not possible…
GRoutar
  • 1,311
  • 1
  • 15
  • 38
5
votes
2 answers

Prolog: foreach or forall for constraint solving?

I'm attempting project scheduling with SWI prolog and CLP. I managed to support sequential dependencies but I'm struggling with avoiding double booking people. I have a list called Schedule containing elements like [taskname, starttime] where…
5
votes
1 answer

Prolog seating constraints

I have to solve the following problem in Prolog. Alex, Fred and Jane are 3 children that have made a seesaw out of a plank that is 10 meters long. They mark 11 seating positions along the plank, each one meter apart. The number the seating positions…
CompilerSaysNo
  • 415
  • 3
  • 14
5
votes
2 answers

Use of cumulatives

I'm working on a problem where I use the cumulatives/[2,3] predicate. But I get very bad performance when I try to combine this with minimize in labeling I have the following demo. 10 task, all with duration 1, 4 machines, all with capacity=1. My…
MortenM
  • 522
  • 2
  • 12
5
votes
3 answers

Memory leak Sicstus Prolog

This question is a followup from this question. I'm running a large number of tests in Sicstus prolog: runtest:- t1, t2, t3, . . t100. Each test is standalone and will print its result to the screen. There is no releation between the tests,…
MortenM
  • 522
  • 2
  • 12
5
votes
3 answers

Prevent backtracking after first solution to Fibonacci pair

The term fib(N,F) is true when F is the Nth Fibonacci number. The following Prolog code is generally working for me: :-use_module(library(clpfd)). fib(0,0). fib(1,1). fib(N,F) :- N #> 1, N #=< F + 1, F #>= N - 1, F #> 0, N1 #= N - 1, …
Martin Klinke
  • 7,294
  • 5
  • 42
  • 64
5
votes
3 answers

Prolog arithmetic syntax

How to define a as a integer/float number ? I want to find the results of a+b+c+d=10 where a,b,c,d is integer and >=0.
user198729
  • 61,774
  • 108
  • 250
  • 348
5
votes
1 answer

Prolog fd_domain is being undefined

OK, so I am trying to code this simple sudoku solver for a school project. I am using SWI - Prolog, and I am using library clpfd. The problem arises when I use domain/3 predicate. It is giving no syntax errors whatsover, only the program is…
Cuta
  • 71
  • 2
  • 9
5
votes
6 answers

Prolog converting integer to a list of digit

I want to write a predicate that an integer and a list of digits, and succeed if Digits contain the digits of the integer in the proper order, i.e: ?-digit_lists( Num, [1,2,3,4] ). [Num == 1234]. Here is what I have so far: my_digits( 0, []…
Duc Hai
  • 51
  • 1
  • 3
5
votes
2 answers

Reversible tree length relation

I'm trying to write reversible relations in "pure" Prolog (no is, cut, or similar stuff. Yes it's homework), and I must admit I don't have a clue how. I don't see any process to create such a thing. We are given "unpure" but reversible arithmetic…
Manux
  • 3,643
  • 4
  • 30
  • 42
5
votes
4 answers

How to Solve Cryptarithmetic Puzzle in Prolog

I have to write a Prolog program for solving a cryptarithmetic puzzle. I need to write a function solve([A, M, P, D, Y]) which assigns the variables [A, M, P, D, Y] to values from 0 to 9 so that it satisfies the equation AM+PM=DAY. Each variable is…
codergirl
  • 53
  • 1
  • 4
5
votes
2 answers

SWI Prolog does not terminate

:- use_module(library(clpfd)). fact(treated=A) :- A in 0..1. fact(numYears=B) :- B in 0..sup. fact(numDrugs=C) :- C in 0..sup. fact(treated2=D) :- D in 0..1. fact(cParam=E) :- E in 0..4. is_differentfact(X,X) :-…
ri5b6
  • 370
  • 1
  • 10
5
votes
2 answers

prolog convert numbers into roman numerals

i have this code that converts integers into roman numerals i need to add a function that compares an integer with a roman numeral input and show if it's try or false, for example: roman(v,5). true toroman(0). toroman(N) :- N < 4, put("I"), M is N…
Muhsag
  • 155
  • 3
  • 11