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

Solving Euler 4 with CLP

I've got this very slow solution to Project Euler 4, :- use_module(library(clpfd)). euler_004(P) :- A in 1..9, B in 0..9, C in 0..9, P #= A * 100001 + B * 10010 + C * 1100, D in 100..999, E in 100..999, E #>= D, P #= D * E, …
vasily
  • 2,850
  • 1
  • 24
  • 40
0
votes
1 answer

How to sum the results without findall

Is there an efficient and generic way of summing up results without generating an intermediate list: main(A) :- B #< 4000000, B mod 2 #= 0, findall(B, fibonacci(_, B), Bs), sum_list(Bs, A). Note: this is Project Euler #2.
vasily
  • 2,850
  • 1
  • 24
  • 40
0
votes
1 answer

Prolog pythagorean triplet

I'm trying to solve project Euler problem 9 using prolog. I'm a 100% n00b with prolog and I'm just trying to understand the basics right now. I'm trying to use findall to get all triplets that would add up to 1000, but I can't really figure out the…
scl
  • 93
  • 9
0
votes
1 answer

Graph Coloring with CLP(FD)

I am trying to solve the map/graph coloring problem using CLP(FD) in Prolog. I took the predicates from the paper "A comparison of CLP(FD) and ASP solutions to NP-complete problems" and I'm trying with the following example: coloring(K, Output) :-…
Fann Wong
  • 21
  • 2
0
votes
1 answer

Prolog: generate solution according to the original list

I am a beginner in Prolog, and I've searched a lot but cannot solve the problem. The question is giving me a list given the head of the list, such as [20,_,_,_]. The length of the list is unknown. For example, it could be [30,_,_,_,_,_]. the _ is…
Woden
  • 1,054
  • 2
  • 13
  • 26
0
votes
3 answers

Instantiating integer variables in Prolog

As my first Prolog challenge I'm trying to solve this puzzle: There are eight floors in an apartment building. The fifth floor has the only apartment with two bedrooms. Mrs Barber has a baby and cannot carry a pram upstairs when the lift is out of…
Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75
0
votes
1 answer

Access all posted clpfd constraints SWI vs Sicstus Prolog

I am trying to work out a problem. I am writing a program for use with Sicstus-Prolog and I need a functionality I have only figured out in SWI-Prolog. Specifically I am trying to get all clp(fd) constraints, that are posted when running the…
Marco
  • 50
  • 5
0
votes
1 answer

prolog lists of lists domain and labeling

L=[[X,Y,Z],[1,A,B],[2,C,D]], L ins 1..3, all_different(L), label(L). I just want to fill the variables in the lists of the list with values. Is there any solution to get the elements of the list (which are lists) in an easier way than…
viktor
  • 51
  • 3
0
votes
2 answers

Prolog office puzzle

I'm attempting to do a Prolog assignment for school, and basically it's trying to find out who has what office. The question is: Hunter, Laura, Jim, Sally, and Jack work in the same building with five adjacent offices. Hunter doesn’t work in the 5th…
0
votes
1 answer

Solving Instant Insanity in PROLOG but i always get "NO"

i currently write a Solver for the Basic Instant-Insanity. My program tells me alltime "NO" as it cant find a solution for my problem and i am to confused to find a fail. Can anyone provide me some help? Even a simple tip can be enough. Thanks…
Gernhard
  • 13
  • 5
0
votes
0 answers

Is there integer quadratic programming or constraint programming solver supporting integer values about 10^54?

I already have tried IBM constraint programming optimizer, but there are only 64-bit integers.
0
votes
0 answers

Restrict automata counter prolog sicstus

I am working on a university project in Programming in Logic. I have to develop an automaton that performs a series of restrictions. One of which is to guarantee that the number 2 has a 4 to it's left and right (only 2) Also, the 4's must be at the…
user12205212
0
votes
1 answer

Creating a bidirectional rule in CHR and CLP/FD

As an example. Suppose that everything which is red has value 5. I write the following: :- use_module(library(chr)). :- use_module(library(clpfd)). :- chr_type color ---> red ; blue. :- chr_constraint hasColor(?any, ?color). :- chr_constraint…
Mark Green
  • 1,310
  • 12
  • 19
0
votes
0 answers

how to optimize this code so that it executes faster

I am trying to code "Peaceable armies of queens" problem. The goal is to put possible maximum equal no. of black and white queens on the chessboard so that they dont attack each other but they can attack within same color. I have implemented and…
filip
  • 1
  • 1
0
votes
1 answer

How to write well predicate(not between)

I write code follwing testb :- X::1..10, V1 = 3, V2 = 6, testbb(X,V1,V2), writeln(X). testbb(X,V1,V2) :- ( count(I,V1,V2),param(X,V1,V2) do X#\=I ). ?- testb. Yes (0.00s cpu) _385{[1, 2, 7 .. 10]} It…
funlive
  • 61
  • 3
  • 10