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

Faster implementation of verbal arithmetic in Prolog

I already made a working generalized verbal arithmetic solver in Prolog but it's too slow. It takes 8 minutes just to run the simple expression S E N D + M O R E = M O N E Y. Can someone help me make it run faster? /*…
6
votes
3 answers

Boolean assignment in Prolog

all. I want to assign a boolean value to a variable. I've tried stuff like. Diagonal is (XPiece = XFinal) Diagonal is (XPiece =:= XFinal) Diagonal is (XPiece is XFinal) None work... Any solutions?
F. P.
  • 5,018
  • 10
  • 55
  • 80
6
votes
1 answer

Optimizing CLPFD performance (cumulative, global_cardinality)

I've written some predicates to solve large scheduling problem and they work well but I'd like them to have some better performance. I've run profile/1 and what I see is that CLPFD-related predicates take 99% of the time. Especially garbage_collect…
Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40
6
votes
2 answers

Cryptogram Puzzle with Prolog CLPFD

I recently found a small game on the Google Play app store called Cryptogram. There are dozens of apps similar to this one. The idea is to match the number to the colors such that all of the equations sound true. I was able to get through problems…
bananafacts
  • 163
  • 6
6
votes
1 answer

Get one of many possible solutions in Prolog

I'm trying to learn Prolog. I looked at this script: :- use_module(library(clpfd)). puzzle([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]) :- Vars = [S,E,N,D,M,O,R,Y], Vars ins 0..9, all_different(Vars), S*1000 + E*100 + N*10 + D + M*1000 + O*100…
Jesse Aldridge
  • 7,991
  • 9
  • 48
  • 75
6
votes
1 answer

Why no division in CLP(FD) in Prolog?

I could not find division (/) symbol on this page of CLP(FD): http://www.swi-prolog.org/man/clpfd.html Also this simple code give error: :-use_module(library(clpfd)). afn(A,B,C):- C #= B / A. ?- afn(23, 56, C). ERROR: Domain error:…
rnso
  • 23,686
  • 25
  • 112
  • 234
6
votes
4 answers

Creating a predicate in Prolog that sums the squares of only the even numbers in a list

I'm trying to figure out how to create a predicate in prolog that sums the squares of only the even numbers in a given list. Expected output: ?- sumsq_even([1,3,5,2,-4,6,8,-7], Sum). Sum = 120 ; false. What I know how to do is to remove all the…
Robert Hung
  • 165
  • 13
6
votes
1 answer

Counting the number of elements in a list: how affectation works

I was working on a Prolog problem consisting in counting the number of elements of a list: count([], 0). count([H|T], N) :- count(T, X), N is X+1, N > 0. I can understand why it's written like that but I don't understand why we can't…
azekirel555
  • 577
  • 2
  • 8
  • 25
6
votes
2 answers

Prolog - arguments are not instantiated

I'm trying to understand how Prolog works. I'm using SWI-Prolog. Here's a bit of code: forall(C1,C2) :- \+ (C1, \+ C2). foo(N) :- N < 10. bar(N) :- N > 5. foobar(N) :- forall(foo(N),bar(N)). It produces desired output if I do something like: ?-…
akalikin
  • 1,071
  • 12
  • 35
6
votes
1 answer

Trying to write a tree-height predicate - do I need Peano-style natural numbers?

As a basic Prolog exercise, I set myself the task of writing a binary tree height predicate that would work forwards and "backwards" - that is, as well as determining the height of a known binary tree, it should be able to find all binary trees…
user180247
6
votes
2 answers

Formulating quadratic equations in clpfd

CLPFD-systems are not primarily targeted to handle quadratic equations efficiently, nevertheless, are there better ways to formulate problems like the following? It seems the problem boils down to equations like the following. SWI with…
false
  • 10,264
  • 13
  • 101
  • 209
6
votes
2 answers

PROLOG CLPFD How to express this via constraints?

Given the following sample of code: example(Ls) :- Ls = [X,Y], Ls ins 1..2, Cost #= max((X #= 1)*3 + (Y #= 1)*5, (X #= 2)*3 + (Y #= 2)*5), labeling([minimize(Cost)], Ls). The idea is to find the assignment to…
user2460978
  • 735
  • 6
  • 19
6
votes
2 answers

Solving the Zebra puzzle (aka. Einstein puzzle) using the clpfd Prolog library

I have been given an exercise to solve the zebra puzzle using a constraint solver of my choice, and I tried it using the Prolog clpfd library. I am aware that there are other more idiomatic ways to solve this problem in Prolog, but this question is…
fresskoma
  • 25,481
  • 10
  • 85
  • 128
5
votes
2 answers

Why is this CLP(FD) constraint solving slowly and how do I debug it?

I'm learning Prolog by doing the Advent of Code challenges. Spoilers for Advent of Code 2021 day 7 below: The objective is: given a list of natural numbers n_1,..., n_k, find min_(x\in \N) \sum_i=0^k |x - n_i| In the below code norm1 computes the…
Tjaden Hess
  • 291
  • 2
  • 11
5
votes
2 answers

Prolog Cryptarithmetic Puzzle

I have been asked to solve a Cryptarithmetic Puzzle using Prolog: GIVE * ME ------ MONEY The above is the puzzle, I cannot figure out where is the problem, the result always returns false. Plus I am not allowed to use any library in…
kyo
  • 53
  • 4