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

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,314
  • 8
  • 20
  • 36
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,563
  • 6
  • 29
  • 54
0
votes
2 answers

How to write kind of Conditional Planning in Prolog?

I tried to write a prolog code that can understand student program written in C#. Now I'm stuck in the process of recognizing the 'if' statement in student's program. For example: The following is the code that I expect from the student. int d =…
Budi Hartanto
  • 337
  • 3
  • 14
0
votes
2 answers

End View Puzzle, Prolog

I'm trying to write a solver for End View Puzzles, using CLPFD (for those who are unfamiliar, here's a simple description http://www.funwithpuzzles.com/2009/12/abcd-end-view-a1.html ). I'm working on the constraint that I'll apply to each…
user1257768
  • 149
  • 1
  • 7
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
user3153616
  • 49
  • 2
  • 7
-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
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,474
  • 7
  • 29
  • 47
-1
votes
1 answer

Second End View puzzle, Prolog

I write code in swi-prolog to solve Second End View Pazzles 7*7 (example http://www.funwithpuzzles.com/2009/10/abcd-second-end-view-ev4.html like this 5*5) for numbers 1-6 :- [library(clpfd)]. gen_row(Ls):-length(Ls, 7), Ls ins 0..6. abc_view…
Oona
  • 3
  • 4
-2
votes
1 answer

Implementing greater-than sudoku solver in Prolog

How to implement greater-than sudoku solver in Prolog using clpfd? I have trouble figuring out how to add those greater-than constraints to regular sudoku solver.
xemlmrx
  • 65
  • 6
-4
votes
2 answers

The Eight-Digit puzzle in prolog

I'm trying to figure how to approach the following puzzle in prolog. I need to create a predicate eight_digit_puzzle(Result). that suggests all possible solutions for the eight-digit puzzle which follows these rules: Each cell contains a number…
-4
votes
1 answer

EndView game on gnu Prolog

Problem in general : we have map 8*8 and we have to fill the empty squares with number from 1 to 6.But in each column and raw number should be met only 1 time.Two squares in each row and column are left empty.Numbers from both sides,up and down show…
Oona
  • 3
  • 4
1 2 3
34
35